Skip to content

Default AMIs #135

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

Open
wants to merge 12 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
57 changes: 36 additions & 21 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62803,50 +62803,60 @@ const core = __webpack_require__(42186);
const config = __webpack_require__(34570);

// User data scripts are run as the root user
function buildUserDataScript(githubRegistrationToken, label) {
function buildUserDataScript(githubRegistrationToken, label, runnerVersion = "2.301.1") {
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
return [
'#!/bin/bash',
`cd "${config.input.runnerHomeDir}"`,
'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} --token ${githubRegistrationToken} --labels ${label} --ephemeral`,
'./run.sh',
];
} else {
return [
'#!/bin/bash',
'yum update -y',
'yum install -y docker git htop',
'systemctl enable docker',
'systemctl start docker',
'mkdir actions-runner && cd actions-runner',
'case $(uname -m) in aarch64) ARCH="arm64" ;; amd64|x86_64) ARCH="x64" ;; esac && export RUNNER_ARCH=${ARCH}',
'curl -O -L https://github.com/actions/runner/releases/download/v2.299.1/actions-runner-linux-${RUNNER_ARCH}-2.299.1.tar.gz',
'tar xzf ./actions-runner-linux-${RUNNER_ARCH}-2.299.1.tar.gz',
`RUNNER_VERSION=${runnerVersion}`,
"curl -O -L https://github.com/actions/runner/releases/download/v${RUNNER_VERSION}/actions-runner-linux-${RUNNER_ARCH}-${RUNNER_VERSION}.tar.gz",
"tar xzf ./actions-runner-linux-${RUNNER_ARCH}-${RUNNER_VERSION}.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} --token ${githubRegistrationToken} --labels ${label} --ephemeral`,
'./run.sh',
];
}
}

async function getLatestAmazonLinuxAmi() {
const ssm = new AWS.SSM();
const result = await ssm.getParameter({ Name: "/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2" }).promise()
return result.Parameter.Value
}

async function startEc2Instance(label, githubRegistrationToken) {
const ec2 = new AWS.EC2();

const userData = buildUserDataScript(githubRegistrationToken, label);

const params = {
ImageId: config.input.ec2ImageId,
InstanceType: config.input.ec2InstanceType,
MinCount: 1,
MaxCount: 1,
UserData: Buffer.from(userData.join('\n')).toString('base64'),
SubnetId: config.input.subnetId,
SecurityGroupIds: [config.input.securityGroupId],
IamInstanceProfile: { Name: config.input.iamRoleName },
TagSpecifications: config.tagSpecifications,
};

try {
const result = await ec2.runInstances(params).promise();
const result = await ec2.runInstances({
ImageId: await getLatestAmazonLinuxAmi(),
InstanceType: config.input.ec2InstanceType,
MinCount: 1,
MaxCount: 1,
UserData: Buffer.from(userData.join('\n')).toString('base64'),
SubnetId: config.input.subnetId,
SecurityGroupIds: [config.input.securityGroupId],
BlockDeviceMappings: [ { DeviceName: "/dev/xvda", Ebs: {Encrypted: true, DeleteOnTermination: true, VolumeSize: 20, VolumeType: "gp3" }} ],
IamInstanceProfile: { Name: config.input.iamRoleName },
TagSpecifications: config.tagSpecifications,
}).promise();
const ec2InstanceId = result.Instances[0].InstanceId;
core.info(`AWS EC2 instance ${ec2InstanceId} is started`);
return ec2InstanceId;
Expand Down Expand Up @@ -62985,6 +62995,7 @@ const config = __webpack_require__(34570);
// use the unique label to find the runner
// as we don't have the runner's id, it's not possible to get it in any other way
async function getRunner(label) {
core.debug("Github Token part " + config.input.githubToken.substring(0, 10));
const octokit = github.getOctokit(config.input.githubToken);

try {
Expand All @@ -62998,14 +63009,18 @@ async function getRunner(label) {

// get GitHub Registration Token for registering a self-hosted runner
async function getRegistrationToken() {
const octokit = github.getOctokit(config.input.githubToken);
core.debug("Github Token part " + config.input.githubToken.substring(0, 10));
const octokit = github.getOctokit(config.input.githubToken, { log: core });

try {
core.debug("Github context");
core.debug(config.githubContext);
const response = await octokit.request('POST /repos/{owner}/{repo}/actions/runners/registration-token', config.githubContext);
core.info('GitHub Registration Token is received');
return response.data.token;
} catch (error) {
core.error('GitHub Registration Token receiving error');
core.error(error);
throw error;
}
}
Expand All @@ -63032,8 +63047,8 @@ async function removeRunner() {

async function waitForRunnerRegistered(label) {
const timeoutMinutes = 5;
const retryIntervalSeconds = 10;
const quietPeriodSeconds = 30;
const retryIntervalSeconds = 5;
const quietPeriodSeconds = 10;
let waitSeconds = 0;

core.info(`Waiting ${quietPeriodSeconds}s for the AWS EC2 instance to be registered in GitHub as a new self-hosted runner`);
Expand Down
46 changes: 28 additions & 18 deletions src/aws.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,60 @@ 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, label, runnerVersion = "2.301.1") {
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
return [
'#!/bin/bash',
`cd "${config.input.runnerHomeDir}"`,
'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} --token ${githubRegistrationToken} --labels ${label} --ephemeral`,
'./run.sh',
];
} else {
return [
'#!/bin/bash',
'yum update -y',
'yum install -y docker git htop',
'systemctl enable docker',
'systemctl start docker',
'mkdir actions-runner && cd actions-runner',
'case $(uname -m) in aarch64) ARCH="arm64" ;; amd64|x86_64) ARCH="x64" ;; esac && export RUNNER_ARCH=${ARCH}',
'curl -O -L https://github.com/actions/runner/releases/download/v2.299.1/actions-runner-linux-${RUNNER_ARCH}-2.299.1.tar.gz',
'tar xzf ./actions-runner-linux-${RUNNER_ARCH}-2.299.1.tar.gz',
`RUNNER_VERSION=${runnerVersion}`,
"curl -O -L https://github.com/actions/runner/releases/download/v${RUNNER_VERSION}/actions-runner-linux-${RUNNER_ARCH}-${RUNNER_VERSION}.tar.gz",
"tar xzf ./actions-runner-linux-${RUNNER_ARCH}-${RUNNER_VERSION}.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} --token ${githubRegistrationToken} --labels ${label} --ephemeral`,
'./run.sh',
];
}
}

async function getLatestAmazonLinuxAmi() {
const ssm = new AWS.SSM();
const result = await ssm.getParameter({ Name: "/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2" }).promise()
return result.Parameter.Value
}

async function startEc2Instance(label, githubRegistrationToken) {
const ec2 = new AWS.EC2();

const userData = buildUserDataScript(githubRegistrationToken, label);

const params = {
ImageId: config.input.ec2ImageId,
InstanceType: config.input.ec2InstanceType,
MinCount: 1,
MaxCount: 1,
UserData: Buffer.from(userData.join('\n')).toString('base64'),
SubnetId: config.input.subnetId,
SecurityGroupIds: [config.input.securityGroupId],
IamInstanceProfile: { Name: config.input.iamRoleName },
TagSpecifications: config.tagSpecifications,
};

try {
const result = await ec2.runInstances(params).promise();
const result = await ec2.runInstances({
ImageId: await getLatestAmazonLinuxAmi(),
InstanceType: config.input.ec2InstanceType,
MinCount: 1,
MaxCount: 1,
UserData: Buffer.from(userData.join('\n')).toString('base64'),
SubnetId: config.input.subnetId,
SecurityGroupIds: [config.input.securityGroupId],
BlockDeviceMappings: [ { DeviceName: "/dev/xvda", Ebs: {Encrypted: true, DeleteOnTermination: true, VolumeSize: 20, VolumeType: "gp3" }} ],
IamInstanceProfile: { Name: config.input.iamRoleName },
TagSpecifications: config.tagSpecifications,
}).promise();
const ec2InstanceId = result.Instances[0].InstanceId;
core.info(`AWS EC2 instance ${ec2InstanceId} is started`);
return ec2InstanceId;
Expand Down
11 changes: 8 additions & 3 deletions src/gh.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const config = require('./config');
// use the unique label to find the runner
// as we don't have the runner's id, it's not possible to get it in any other way
async function getRunner(label) {
core.debug("Github Token part " + config.input.githubToken.substring(0, 10));
const octokit = github.getOctokit(config.input.githubToken);

try {
Expand All @@ -19,14 +20,18 @@ async function getRunner(label) {

// get GitHub Registration Token for registering a self-hosted runner
async function getRegistrationToken() {
const octokit = github.getOctokit(config.input.githubToken);
core.debug("Github Token part " + config.input.githubToken.substring(0, 10));
const octokit = github.getOctokit(config.input.githubToken, { log: core });

try {
core.debug("Github context");
core.debug(config.githubContext);
const response = await octokit.request('POST /repos/{owner}/{repo}/actions/runners/registration-token', config.githubContext);
core.info('GitHub Registration Token is received');
return response.data.token;
} catch (error) {
core.error('GitHub Registration Token receiving error');
core.error(error);
throw error;
}
}
Expand All @@ -53,8 +58,8 @@ async function removeRunner() {

async function waitForRunnerRegistered(label) {
const timeoutMinutes = 5;
const retryIntervalSeconds = 10;
const quietPeriodSeconds = 30;
const retryIntervalSeconds = 5;
const quietPeriodSeconds = 10;
let waitSeconds = 0;

core.info(`Waiting ${quietPeriodSeconds}s for the AWS EC2 instance to be registered in GitHub as a new self-hosted runner`);
Expand Down