diff --git a/.env.example b/.env.example index 2db35299..54b5e816 100644 --- a/.env.example +++ b/.env.example @@ -12,6 +12,7 @@ AWS_SECRET_ACCESS_KEY= AWS_REGION= INPUT_MODE= INPUT_GITHUB-TOKEN= +INPUT_GITHUB-TOKEN-TYPE= INPUT_EC2-IMAGE-ID= INPUT_EC2-INSTANCE-TYPE= INPUT_SUBNET-ID= diff --git a/action.yml b/action.yml index ea4aea71..ca973e24 100644 --- a/action.yml +++ b/action.yml @@ -15,6 +15,12 @@ inputs: description: >- GitHub Personal Access Token with the 'repo' scope assigned. required: true + github-token-type: + description: >- + Github Token Type. + Indicates whether the github access token is of the original 'classic', or the new 'fine-grained' Type. + This input has the default value of 'classic' + required: false ec2-image-id: description: >- EC2 Image Id (AMI). The new runner will be launched from this image. diff --git a/src/aws.js b/src/aws.js index 09042ea7..ca86f28e 100644 --- a/src/aws.js +++ b/src/aws.js @@ -3,7 +3,12 @@ const core = require('@actions/core'); const config = require('./config'); // User data scripts are run as the root user -function buildUserDataScript(githubRegistrationToken, label) { +function buildUserDataScript(githubRegistrationToken, githubTokenType, label) { + let tokenArg = 'token'; + if (githubTokenType == 'fine-grained') { + tokenArg = 'pat'; + } + if (config.input.runnerHomeDir) { // If runner home directory is specified, we expect the actions-runner software (and dependencies) // to be pre-installed in the AMI, so we simply cd into that directory and then start the runner @@ -13,7 +18,7 @@ function buildUserDataScript(githubRegistrationToken, label) { `echo "${config.input.preRunnerScript}" > pre-runner-script.sh`, 'source pre-runner-script.sh', 'export RUNNER_ALLOW_RUNASROOT=1', - `./config.sh --url https://github.com/${config.githubContext.owner}/${config.githubContext.repo} --token ${githubRegistrationToken} --labels ${label}`, + `./config.sh --url https://github.com/${config.githubContext.owner}/${config.githubContext.repo} --${tokenArg} ${githubRegistrationToken} --labels ${label} --name $(hostname)-$(uuidgen)`, './run.sh', ]; } else { @@ -26,16 +31,16 @@ function buildUserDataScript(githubRegistrationToken, label) { 'curl -O -L https://github.com/actions/runner/releases/download/v2.313.0/actions-runner-linux-${RUNNER_ARCH}-2.313.0.tar.gz', 'tar xzf ./actions-runner-linux-${RUNNER_ARCH}-2.313.0.tar.gz', 'export RUNNER_ALLOW_RUNASROOT=1', - `./config.sh --url https://github.com/${config.githubContext.owner}/${config.githubContext.repo} --token ${githubRegistrationToken} --labels ${label}`, + `./config.sh --url https://github.com/${config.githubContext.owner}/${config.githubContext.repo} --${tokenArg} ${githubRegistrationToken} --labels ${label} --name $(hostname)-$(uuidgen)`, './run.sh', ]; } } -async function startEc2Instance(label, githubRegistrationToken) { +async function startEc2Instance(label, githubRegistrationToken, githubTokenType) { const ec2 = new AWS.EC2(); - const userData = buildUserDataScript(githubRegistrationToken, label); + const userData = buildUserDataScript(githubRegistrationToken, githubTokenType, label); const params = { ImageId: config.input.ec2ImageId, diff --git a/src/config.js b/src/config.js index 1100f51e..e752244c 100644 --- a/src/config.js +++ b/src/config.js @@ -6,6 +6,7 @@ class Config { this.input = { mode: core.getInput('mode'), githubToken: core.getInput('github-token'), + githubTokenType: core.getInput('github-token-type'), ec2ImageId: core.getInput('ec2-image-id'), ec2InstanceType: core.getInput('ec2-instance-type'), subnetId: core.getInput('subnet-id'), @@ -20,7 +21,10 @@ class Config { const tags = JSON.parse(core.getInput('aws-resource-tags')); this.tagSpecifications = null; if (tags.length > 0) { - this.tagSpecifications = [{ResourceType: 'instance', Tags: tags}, {ResourceType: 'volume', Tags: tags}]; + this.tagSpecifications = [ + { ResourceType: 'instance', Tags: tags }, + { ResourceType: 'volume', Tags: tags }, + ]; } // the values of github.context.repo.owner and github.context.repo.repo are taken from @@ -43,6 +47,10 @@ class Config { throw new Error(`The 'github-token' input is not specified`); } + if (!this.input.githubTokenType) { + this.input.githubTokenType = 'classic'; + } + if (this.input.mode === 'start') { if (!this.input.ec2ImageId || !this.input.ec2InstanceType || !this.input.subnetId || !this.input.securityGroupId) { throw new Error(`Not all the required inputs are provided for the 'start' mode`); diff --git a/src/index.js b/src/index.js index 00bc5152..63f3aee0 100644 --- a/src/index.js +++ b/src/index.js @@ -11,7 +11,7 @@ function setOutput(label, ec2InstanceId) { async function start() { const label = config.generateUniqueLabel(); const githubRegistrationToken = await gh.getRegistrationToken(); - const ec2InstanceId = await aws.startEc2Instance(label, githubRegistrationToken); + const ec2InstanceId = await aws.startEc2Instance(label, githubRegistrationToken, config.input.githubTokenType); setOutput(label, ec2InstanceId); await aws.waitForInstanceRunning(ec2InstanceId); await gh.waitForRunnerRegistered(label);