@@ -15,38 +15,44 @@ export interface ActionRequestMessage {
15
15
installationId : number ;
16
16
}
17
17
18
- function generateRunnerServiceConfig (
19
- runnerExtraLabels : string | undefined ,
20
- runnerGroup : string | undefined ,
21
- ghesBaseUrl : string ,
22
- ephemeral : boolean ,
23
- token : any ,
24
- runnerType : 'Org' | 'Repo' ,
25
- payload : ActionRequestMessage ,
26
- ) {
27
- const labelsArgument = runnerExtraLabels !== undefined ? `--labels ${ runnerExtraLabels } ` : '' ;
28
- const runnerGroupArgument = runnerGroup !== undefined ? `--runnergroup ${ runnerGroup } ` : '' ;
29
- const configBaseUrl = ghesBaseUrl ? ghesBaseUrl : 'https://github.com' ;
30
- const ephemeralArgument = ephemeral ? '--ephemeral ' : '' ;
18
+ interface CreateGitHubRunnerConfig {
19
+ ephemeral : boolean ;
20
+ ghesBaseUrl : string ;
21
+ runnerExtraLabels : string | undefined ;
22
+ runnerGroup : string | undefined ;
23
+ runnerOwner : string ;
24
+ runnerType : 'Org' | 'Repo' ;
25
+ }
26
+
27
+ interface CreateEC2RunnerConfig {
28
+ environment : string ;
29
+ subnets : string [ ] ;
30
+ launchTemplateName : string ;
31
+ ec2instanceCriteria : RunnerInputParameters [ 'ec2instanceCriteria' ] ;
32
+ }
33
+
34
+ function generateRunnerServiceConfig ( githubRunnerConfig : CreateGitHubRunnerConfig , token : any ) {
35
+ const labelsArgument =
36
+ githubRunnerConfig . runnerExtraLabels !== undefined ? `--labels ${ githubRunnerConfig . runnerExtraLabels } ` : '' ;
37
+ const runnerGroupArgument =
38
+ githubRunnerConfig . runnerGroup !== undefined ? `--runnergroup ${ githubRunnerConfig . runnerGroup } ` : '' ;
39
+ const configBaseUrl = githubRunnerConfig . ghesBaseUrl ? githubRunnerConfig . ghesBaseUrl : 'https://github.com' ;
40
+ const ephemeralArgument = githubRunnerConfig . ephemeral ? '--ephemeral ' : '' ;
31
41
const runnerArgs = `--token ${ token } ${ labelsArgument } ${ ephemeralArgument } ` ;
32
- return runnerType === 'Org'
33
- ? `--url ${ configBaseUrl } /${ payload . repositoryOwner } ${ runnerArgs } ${ runnerGroupArgument } ` . trim ( )
34
- : `--url ${ configBaseUrl } /${ payload . repositoryOwner } / ${ payload . repositoryName } ${ runnerArgs } ` . trim ( ) ;
42
+ return githubRunnerConfig . runnerType === 'Org'
43
+ ? `--url ${ configBaseUrl } /${ githubRunnerConfig . runnerOwner } ${ runnerArgs } ${ runnerGroupArgument } ` . trim ( )
44
+ : `--url ${ configBaseUrl } /${ githubRunnerConfig . runnerOwner } ${ runnerArgs } ` . trim ( ) ;
35
45
}
36
46
37
- async function getGithubRunnerRegistrationToken (
38
- enableOrgLevel : boolean ,
39
- githubInstallationClient : Octokit ,
40
- payload : ActionRequestMessage ,
41
- ) {
42
- const registrationToken = enableOrgLevel
43
- ? await githubInstallationClient . actions . createRegistrationTokenForOrg ( { org : payload . repositoryOwner } )
44
- : await githubInstallationClient . actions . createRegistrationTokenForRepo ( {
45
- owner : payload . repositoryOwner ,
46
- repo : payload . repositoryName ,
47
- } ) ;
48
- const token = registrationToken . data . token ;
49
- return token ;
47
+ async function getGithubRunnerRegistrationToken ( githubRunnerConfig : CreateGitHubRunnerConfig , ghClient : Octokit ) {
48
+ const registrationToken =
49
+ githubRunnerConfig . runnerType === 'Org'
50
+ ? await ghClient . actions . createRegistrationTokenForOrg ( { org : githubRunnerConfig . runnerOwner } )
51
+ : await ghClient . actions . createRegistrationTokenForRepo ( {
52
+ owner : githubRunnerConfig . runnerOwner . split ( '/' ) [ 0 ] ,
53
+ repo : githubRunnerConfig . runnerOwner . split ( '/' ) [ 1 ] ,
54
+ } ) ;
55
+ return registrationToken . data . token ;
50
56
}
51
57
52
58
async function getInstallationId ( ghesApiUrl : string , enableOrgLevel : boolean , payload : ActionRequestMessage ) {
@@ -95,41 +101,20 @@ async function isJobQueued(githubInstallationClient: Octokit, payload: ActionReq
95
101
return isQueued ;
96
102
}
97
103
98
- async function createRunners (
99
- enableOrgLevel : boolean ,
100
- githubInstallationClient : Octokit ,
101
- payload : ActionRequestMessage ,
102
- runnerExtraLabels : string | undefined ,
103
- runnerGroup : string | undefined ,
104
- ghesBaseUrl : string ,
105
- ephemeral : boolean ,
106
- runnerType : 'Org' | 'Repo' ,
107
- environment : string ,
108
- runnerOwner : string ,
109
- subnets : string [ ] ,
110
- launchTemplateName : string ,
111
- ec2instanceCriteria : RunnerInputParameters [ 'ec2instanceCriteria' ] ,
104
+ export async function createRunners (
105
+ githubRunnerConfig : CreateGitHubRunnerConfig ,
106
+ ec2RunnerConfig : CreateEC2RunnerConfig ,
107
+ ghClient : Octokit ,
112
108
) : Promise < void > {
113
- const token = await getGithubRunnerRegistrationToken ( enableOrgLevel , githubInstallationClient , payload ) ;
114
-
115
- const runnerServiceConfig = generateRunnerServiceConfig (
116
- runnerExtraLabels ,
117
- runnerGroup ,
118
- ghesBaseUrl ,
119
- ephemeral ,
120
- token ,
121
- runnerType ,
122
- payload ,
123
- ) ;
109
+ const token = await getGithubRunnerRegistrationToken ( githubRunnerConfig , ghClient ) ;
110
+
111
+ const runnerServiceConfig = generateRunnerServiceConfig ( githubRunnerConfig , token ) ;
124
112
125
113
await createRunner ( {
126
- environment,
127
114
runnerServiceConfig,
128
- runnerOwner,
129
- runnerType,
130
- subnets,
131
- launchTemplateName,
132
- ec2instanceCriteria,
115
+ runnerType : githubRunnerConfig . runnerType ,
116
+ runnerOwner : githubRunnerConfig . runnerOwner ,
117
+ ...ec2RunnerConfig ,
133
118
} ) ;
134
119
}
135
120
@@ -197,24 +182,26 @@ export async function scaleUp(eventSource: string, payload: ActionRequestMessage
197
182
logger . info ( `Attempting to launch a new runner` , LogFields . print ( ) ) ;
198
183
199
184
await createRunners (
200
- enableOrgLevel ,
201
- githubInstallationClient ,
202
- payload ,
203
- runnerExtraLabels ,
204
- runnerGroup ,
205
- ghesBaseUrl ,
206
- ephemeral ,
207
- runnerType ,
208
- environment ,
209
- runnerOwner ,
210
- subnets ,
211
- launchTemplateName ,
212
185
{
213
- instanceTypes,
214
- targetCapacityType : instanceTargetTargetCapacityType ,
215
- maxSpotPrice : instanceMaxSpotPrice ,
216
- instanceAllocationStrategy : instanceAllocationStrategy ,
186
+ ephemeral,
187
+ ghesBaseUrl,
188
+ runnerExtraLabels,
189
+ runnerGroup,
190
+ runnerOwner,
191
+ runnerType,
217
192
} ,
193
+ {
194
+ ec2instanceCriteria : {
195
+ instanceTypes,
196
+ targetCapacityType : instanceTargetTargetCapacityType ,
197
+ maxSpotPrice : instanceMaxSpotPrice ,
198
+ instanceAllocationStrategy : instanceAllocationStrategy ,
199
+ } ,
200
+ environment,
201
+ launchTemplateName,
202
+ subnets,
203
+ } ,
204
+ githubInstallationClient ,
218
205
) ;
219
206
} else {
220
207
logger . info ( 'No runner will be created, maximum number of runners reached.' , LogFields . print ( ) ) ;
0 commit comments