diff --git a/dist/index.js b/dist/index.js index c213fdae..b3280bf5 100644 --- a/dist/index.js +++ b/dist/index.js @@ -62803,7 +62803,7 @@ 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 @@ -62811,42 +62811,52 @@ function buildUserDataScript(githubRegistrationToken, label) { '#!/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; @@ -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 { @@ -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; } } @@ -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`); diff --git a/src/aws.js b/src/aws.js index c6bd8c9d..6b37a964 100644 --- a/src/aws.js +++ b/src/aws.js @@ -3,7 +3,7 @@ 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 @@ -11,42 +11,52 @@ function buildUserDataScript(githubRegistrationToken, label) { '#!/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; diff --git a/src/gh.js b/src/gh.js index abf9af94..c2cef857 100644 --- a/src/gh.js +++ b/src/gh.js @@ -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 { @@ -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; } } @@ -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`);