Skip to content

Commit 3e80652

Browse files
add support organization scoped runner
allow label definition
1 parent 8bcafba commit 3e80652

File tree

7 files changed

+70
-1823
lines changed

7 files changed

+70
-1823
lines changed

action.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,15 @@ inputs:
3939
required: false
4040
label:
4141
description: >-
42-
Name of the unique label assigned to the runner.
42+
Defines the runner's label name when set. If this value is not defined, the runner will generate a random label.
4343
The label is used to remove the runner from GitHub when the runner is not needed anymore.
4444
This input is required if you use the 'stop' mode.
4545
required: false
46+
scope:
47+
description: >-
48+
The runner's scope. The runner scope must be "repository", "organization" or "enterprise".
49+
required: true
50+
options: ["repository", "organization", "enterprise"]
4651
ec2-instance-id:
4752
description: >-
4853
EC2 Instance Id of the created runner.

dist/index.js

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -62811,7 +62811,7 @@ function buildUserDataScript(githubRegistrationToken, label) {
6281162811
'#!/bin/bash',
6281262812
`cd "${config.input.runnerHomeDir}"`,
6281362813
'export RUNNER_ALLOW_RUNASROOT=1',
62814-
`./config.sh --url https://github.com/${config.githubContext.owner}/${config.githubContext.repo} --token ${githubRegistrationToken} --labels ${label}`,
62814+
`./config.sh --url ${config.githubConfig.url} --token ${githubRegistrationToken} --labels ${label}`,
6281562815
'./run.sh',
6281662816
];
6281762817
} else {
@@ -62822,7 +62822,7 @@ function buildUserDataScript(githubRegistrationToken, label) {
6282262822
'curl -O -L https://github.com/actions/runner/releases/download/v2.299.1/actions-runner-linux-${RUNNER_ARCH}-2.299.1.tar.gz',
6282362823
'tar xzf ./actions-runner-linux-${RUNNER_ARCH}-2.299.1.tar.gz',
6282462824
'export RUNNER_ALLOW_RUNASROOT=1',
62825-
`./config.sh --url https://github.com/${config.githubContext.owner}/${config.githubContext.repo} --token ${githubRegistrationToken} --labels ${label}`,
62825+
`./config.sh --url ${config.githubConfig.url} --token ${githubRegistrationToken} --labels ${label}`,
6282662826
'./run.sh',
6282762827
];
6282862828
}
@@ -62918,6 +62918,21 @@ class Config {
6291862918
ec2InstanceId: core.getInput('ec2-instance-id'),
6291962919
iamRoleName: core.getInput('iam-role-name'),
6292062920
runnerHomeDir: core.getInput('runner-home-dir'),
62921+
scope: core.getInput('scope'),
62922+
};
62923+
62924+
this.GITHUB_SCOPES = {
62925+
organization: {
62926+
url: `https://github.com/${github.context.repo.owner}`,
62927+
context: { owner: github.context.repo.owner }
62928+
},
62929+
repository: {
62930+
url: `https://github.com/${github.context.repo.owner}/${github.context.repo.repo}`,
62931+
context: {
62932+
owner: github.context.repo.owner,
62933+
repo: github.context.repo.repo
62934+
}
62935+
}
6292162936
};
6292262937

6292362938
const tags = JSON.parse(core.getInput('aws-resource-tags'));
@@ -62926,13 +62941,10 @@ class Config {
6292662941
this.tagSpecifications = [{ResourceType: 'instance', Tags: tags}, {ResourceType: 'volume', Tags: tags}];
6292762942
}
6292862943

62929-
// the values of github.context.repo.owner and github.context.repo.repo are taken from
62930-
// the environment variable GITHUB_REPOSITORY specified in "owner/repo" format and
62931-
// provided by the GitHub Action on the runtime
62932-
this.githubContext = {
62933-
owner: github.context.repo.owner,
62934-
repo: github.context.repo.repo,
62935-
};
62944+
this.githubConfig = this.GITHUB_SCOPES[this.input.scope];
62945+
if (!this.githubConfig) {
62946+
throw new Error(`The 'scope' input is not valid`);
62947+
}
6293662948

6293762949
//
6293862950
// validate input
@@ -62959,8 +62971,12 @@ class Config {
6295962971
}
6296062972
}
6296162973

62962-
generateUniqueLabel() {
62963-
return Math.random().toString(36).substr(2, 5);
62974+
generateLabel() {
62975+
if (!this.input.label) {
62976+
return Math.random().toString(36).substr(2, 5);
62977+
}
62978+
62979+
return this.input.label
6296462980
}
6296562981
}
6296662982

@@ -62988,7 +63004,7 @@ async function getRunner(label) {
6298863004
const octokit = github.getOctokit(config.input.githubToken);
6298963005

6299063006
try {
62991-
const runners = await octokit.paginate('GET /repos/{owner}/{repo}/actions/runners', config.githubContext);
63007+
const runners = await octokit.paginate('GET /repos/{owner}/{repo}/actions/runners', config.githubConfig.context);
6299263008
const foundRunners = _.filter(runners, { labels: [{ name: label }] });
6299363009
return foundRunners.length > 0 ? foundRunners[0] : null;
6299463010
} catch (error) {
@@ -63001,7 +63017,7 @@ async function getRegistrationToken() {
6300163017
const octokit = github.getOctokit(config.input.githubToken);
6300263018

6300363019
try {
63004-
const response = await octokit.request('POST /repos/{owner}/{repo}/actions/runners/registration-token', config.githubContext);
63020+
const response = await octokit.request('POST /repos/{owner}/{repo}/actions/runners/registration-token', config.githubConfig.context);
6300563021
core.info('GitHub Registration Token is received');
6300663022
return response.data.token;
6300763023
} catch (error) {
@@ -63021,7 +63037,7 @@ async function removeRunner() {
6302163037
}
6302263038

6302363039
try {
63024-
await octokit.request('DELETE /repos/{owner}/{repo}/actions/runners/{runner_id}', _.merge(config.githubContext, { runner_id: runner.id }));
63040+
await octokit.request('DELETE /repos/{owner}/{repo}/actions/runners/{runner_id}', _.merge(config.githubConfig.context, { runner_id: runner.id }));
6302563041
core.info(`GitHub self-hosted runner ${runner.name} is removed`);
6302663042
return;
6302763043
} catch (error) {
@@ -63085,7 +63101,7 @@ function setOutput(label, ec2InstanceId) {
6308563101
}
6308663102

6308763103
async function start() {
63088-
const label = config.generateUniqueLabel();
63104+
const label = config.generateLabel();
6308963105
const githubRegistrationToken = await gh.getRegistrationToken();
6309063106
const ec2InstanceId = await aws.startEc2Instance(label, githubRegistrationToken);
6309163107
setOutput(label, ec2InstanceId);

0 commit comments

Comments
 (0)