Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow pat tokens #196

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
6 changes: 6 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
15 changes: 10 additions & 5 deletions src/aws.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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,
Expand Down
10 changes: 9 additions & 1 deletion src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand All @@ -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
Expand All @@ -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`);
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down