@@ -3,60 +3,37 @@ import { Octokit } from '@octokit/rest';
3
3
import { AppAuth } from '@octokit/auth-app/dist-types/types' ;
4
4
import { listRunners , terminateRunner , RunnerInfo } from './runners' ;
5
5
import { createGithubAppAuth , createInstallationClient } from './scale-up' ;
6
+ import yn from 'yn' ;
6
7
7
- // function createGithubAppAuth(installationId: number | undefined): AppAuth {
8
- // const privateKey = Buffer.from(process.env.GITHUB_APP_KEY_BASE64 as string, 'base64').toString();
9
- // const appId: number = parseInt(process.env.GITHUB_APP_ID as string);
10
- // const clientId = process.env.GITHUB_APP_CLIENT_ID as string;
11
- // const clientSecret = process.env.GITHUB_APP_CLIENT_SECRET as string;
12
-
13
- // return createAppAuth({
14
- // id: appId,
15
- // privateKey: privateKey,
16
- // installationId: installationId,
17
- // clientId: clientId,
18
- // clientSecret: clientSecret,
19
- // });
20
- // }
21
-
22
- // async function createInstallationClient(githubAppAuth: AppAuth): Promise<Octokit> {
23
- // const auth = await githubAppAuth({ type: 'installation' });
24
- // return new Octokit({ auth: auth.token });
25
- // }
26
-
27
- // specific to scale down
28
8
async function createAppClient ( githubAppAuth : AppAuth ) : Promise < Octokit > {
29
9
const auth = await githubAppAuth ( { type : 'app' } ) ;
30
10
return new Octokit ( { auth : auth . token } ) ;
31
11
}
32
12
33
13
interface Repo {
34
- isOrg : boolean ;
35
14
repoName : string ;
36
15
repoOwner : string ;
37
16
}
38
17
39
- function getRepo ( runner : RunnerInfo ) : Repo {
40
- if ( runner . repo ) {
18
+ function getRepo ( runner : RunnerInfo , orgLevel : boolean ) : Repo {
19
+ if ( orgLevel ) {
41
20
return {
42
- repoOwner : runner . repo ?. split ( '/' ) [ 0 ] as string ,
43
- repoName : runner . repo ?. split ( '/' ) [ 1 ] as string ,
44
- isOrg : false ,
21
+ repoOwner : runner . org as string ,
22
+ repoName : '' ,
45
23
} ;
46
24
} else {
47
25
return {
48
- repoOwner : runner . org as string ,
49
- repoName : '' ,
50
- isOrg : true ,
26
+ repoOwner : runner . repo ?. split ( '/' ) [ 0 ] as string ,
27
+ repoName : runner . repo ?. split ( '/' ) [ 1 ] as string ,
51
28
} ;
52
29
}
53
30
}
54
31
55
- async function createGitHubClientForRunner ( runner : RunnerInfo ) : Promise < Octokit > {
32
+ async function createGitHubClientForRunner ( runner : RunnerInfo , orgLevel : boolean ) : Promise < Octokit > {
56
33
const githubClient = await createAppClient ( createGithubAppAuth ( undefined ) ) ;
57
- const repo = getRepo ( runner ) ;
34
+ const repo = getRepo ( runner , orgLevel ) ;
58
35
59
- const repoInstallationId = repo . isOrg
36
+ const installationId = orgLevel
60
37
? (
61
38
await githubClient . apps . getOrgInstallation ( {
62
39
org : repo . repoOwner ,
@@ -69,10 +46,11 @@ async function createGitHubClientForRunner(runner: RunnerInfo): Promise<Octokit>
69
46
} )
70
47
) . data . id ;
71
48
72
- return createInstallationClient ( createGithubAppAuth ( repoInstallationId ) ) ;
49
+ return createInstallationClient ( createGithubAppAuth ( installationId ) ) ;
73
50
}
74
51
75
52
export async function scaleDown ( ) : Promise < void > {
53
+ const enableOrgLevel = yn ( process . env . ENABLE_ORGANIZATION_RUNNERS , { default : true } ) ;
76
54
const environment = process . env . ENVIRONMENT as string ;
77
55
const runners = await listRunners ( {
78
56
environment : environment ,
@@ -82,42 +60,46 @@ export async function scaleDown(): Promise<void> {
82
60
console . debug ( `No active runners found for environment: '${ environment } '` ) ;
83
61
return ;
84
62
}
63
+ console . log ( runners ) ;
85
64
86
- runners . forEach ( async ( r ) => {
87
- const githubAppClient = await createGitHubClientForRunner ( r ) ;
65
+ for ( const r of runners ) {
66
+ const githubAppClient = await createGitHubClientForRunner ( r , enableOrgLevel ) ;
88
67
89
- const repo = getRepo ( r ) ;
90
- const registered = await githubAppClient . actions . listSelfHostedRunnersForRepo ( {
91
- owner : repo . repoOwner ,
92
- repo : repo . repoName ,
93
- } ) ;
68
+ const repo = getRepo ( r , enableOrgLevel ) ;
69
+ console . log ( repo ) ;
70
+ const registered = enableOrgLevel
71
+ ? await githubAppClient . actions . listSelfHostedRunnersForOrg ( {
72
+ org : repo . repoOwner ,
73
+ } )
74
+ : await githubAppClient . actions . listSelfHostedRunnersForRepo ( {
75
+ owner : repo . repoOwner ,
76
+ repo : repo . repoName ,
77
+ } ) ;
78
+ console . log ( registered ) ;
94
79
95
80
console . log ( registered . data . runners ) ;
96
- registered . data . runners . forEach ( async ( a : any ) => {
81
+ for ( const a of registered . data . runners ) {
97
82
const runnerName = a . name as string ;
98
83
if ( runnerName === r . instanceId ) {
99
- console . log ( r . instanceId ) ;
100
84
try {
101
- const result = repo . isOrg
85
+ const result = enableOrgLevel
102
86
? await githubAppClient . actions . deleteSelfHostedRunnerFromOrg ( { runner_id : a . id , org : repo . repoOwner } )
103
87
: await githubAppClient . actions . deleteSelfHostedRunnerFromRepo ( {
104
88
runner_id : a . id ,
105
89
owner : repo . repoOwner ,
106
90
repo : repo . repoName ,
107
91
} ) ;
92
+
108
93
if ( result ?. status == 204 ) {
109
- terminateRunner ( r ) ;
94
+ await terminateRunner ( r ) ;
110
95
console . info (
111
96
`AWS runner instance '${ r . instanceId } ' is terminated and GitHub runner '${ runnerName } ' is de-registered.` ,
112
97
) ;
113
98
}
114
- console . info (
115
- `AWS runner instance '${ r . instanceId } ' is terminated and GitHub runner '${ runnerName } ' is de-registered.` ,
116
- ) ;
117
99
} catch ( e ) {
118
100
console . debug ( `Runner '${ runnerName } ' cannot be de-registered, most likely the runner is active.` ) ;
119
101
}
120
102
}
121
- } ) ;
122
- } ) ;
103
+ }
104
+ }
123
105
}
0 commit comments