@@ -13,6 +13,13 @@ export interface ListRunnerFilters {
13
13
environment : string | undefined ;
14
14
}
15
15
16
+ export interface RunnerInputParameters {
17
+ runnerServiceConfig : string ;
18
+ environment : string ;
19
+ runnerType : 'Org' | 'Repo' ;
20
+ runnerOwner : string ;
21
+ }
22
+
16
23
export async function listRunners ( filters : ListRunnerFilters | undefined = undefined ) : Promise < RunnerInfo [ ] > {
17
24
const ec2 = new EC2 ( ) ;
18
25
const ec2Filters = [
@@ -46,13 +53,6 @@ export async function listRunners(filters: ListRunnerFilters | undefined = undef
46
53
return runners ;
47
54
}
48
55
49
- export interface RunnerInputParameters {
50
- runnerConfig : string ;
51
- environment : string ;
52
- runnerType : 'Org' | 'Repo' ;
53
- runnerOwner : string ;
54
- }
55
-
56
56
export async function terminateRunner ( runner : RunnerInfo ) : Promise < void > {
57
57
const ec2 = new EC2 ( ) ;
58
58
await ec2
@@ -63,47 +63,53 @@ export async function terminateRunner(runner: RunnerInfo): Promise<void> {
63
63
console . debug ( 'Runner terminated.' + runner . instanceId ) ;
64
64
}
65
65
66
- export async function createRunner ( runnerParameters : RunnerInputParameters ) : Promise < void > {
67
- const launchTemplateName = process . env . LAUNCH_TEMPLATE_NAME as string ;
68
- const launchTemplateVersion = process . env . LAUNCH_TEMPLATE_VERSION as string ;
69
-
70
- const subnets = ( process . env . SUBNET_IDS as string ) . split ( ',' ) ;
71
- const randomSubnet = subnets [ Math . floor ( Math . random ( ) * subnets . length ) ] ;
66
+ export async function createRunner ( runnerParameters : RunnerInputParameters , launchTemplateName : string ) : Promise < void > {
72
67
console . debug ( 'Runner configuration: ' + JSON . stringify ( runnerParameters ) ) ;
73
68
const ec2 = new EC2 ( ) ;
74
69
const runInstancesResponse = await ec2
75
- . runInstances ( {
76
- MaxCount : 1 ,
77
- MinCount : 1 ,
78
- LaunchTemplate : {
79
- LaunchTemplateName : launchTemplateName ,
80
- Version : launchTemplateVersion ,
81
- } ,
82
- SubnetId : randomSubnet ,
83
- TagSpecifications : [
84
- {
85
- ResourceType : 'instance' ,
86
- Tags : [
87
- { Key : 'Application' , Value : 'github-action-runner' } ,
88
- {
89
- Key : runnerParameters . runnerType ,
90
- Value : runnerParameters . runnerOwner
91
- } ,
92
- ] ,
93
- } ,
94
- ] ,
95
- } )
70
+ . runInstances ( getInstanceParams ( launchTemplateName , runnerParameters ) )
96
71
. promise ( ) ;
97
72
console . info ( 'Created instance(s): ' , runInstancesResponse . Instances ?. map ( ( i ) => i . InstanceId ) . join ( ',' ) ) ;
98
-
99
73
const ssm = new SSM ( ) ;
100
74
runInstancesResponse . Instances ?. forEach ( async ( i : EC2 . Instance ) => {
101
75
await ssm
102
76
. putParameter ( {
103
77
Name : runnerParameters . environment + '-' + ( i . InstanceId as string ) ,
104
- Value : runnerParameters . runnerConfig ,
78
+ Value : runnerParameters . runnerServiceConfig ,
105
79
Type : 'SecureString' ,
106
80
} )
107
81
. promise ( ) ;
108
82
} ) ;
109
83
}
84
+
85
+ function getInstanceParams (
86
+ launchTemplateName : string ,
87
+ runnerParameters : RunnerInputParameters
88
+ ) : EC2 . RunInstancesRequest {
89
+ return {
90
+ MaxCount : 1 ,
91
+ MinCount : 1 ,
92
+ LaunchTemplate : {
93
+ LaunchTemplateName : launchTemplateName ,
94
+ Version : '$Default' ,
95
+ } ,
96
+ SubnetId : getSubnet ( ) ,
97
+ TagSpecifications : [
98
+ {
99
+ ResourceType : 'instance' ,
100
+ Tags : [
101
+ { Key : 'Application' , Value : 'github-action-runner' } ,
102
+ {
103
+ Key : runnerParameters . runnerType ,
104
+ Value : runnerParameters . runnerOwner
105
+ } ,
106
+ ] ,
107
+ } ,
108
+ ] ,
109
+ } ;
110
+ }
111
+
112
+ function getSubnet ( ) : string {
113
+ const subnets = ( process . env . SUBNET_IDS as string ) . split ( ',' ) ;
114
+ return subnets [ Math . floor ( Math . random ( ) * subnets . length ) ] ;
115
+ }
0 commit comments