Skip to content

Commit

Permalink
Add 'key-name' and 'block-device-mapping' options (machulav#156)
Browse files Browse the repository at this point in the history
  • Loading branch information
garbas committed Oct 10, 2023
1 parent f8fe7f5 commit f6823ac
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 7 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,9 @@ Now you're ready to go!
| `aws-resource-tags` | Optional. Used only with the `start` mode. | Specifies tags to add to the EC2 instance and any attached storage. <br><br> This field is a stringified JSON array of tag objects, each containing a `Key` and `Value` field (see example below). <br><br> Setting this requires additional AWS permissions for the role launching the instance (see above). |
| `runner-home-dir` | Optional. Used only with the `start` mode. | Specifies a directory where pre-installed actions-runner software and scripts are located.<br><br> |
| `pre-runner-script` | Optional. Used only with the `start` mode. | Specifies bash commands to run before the runner starts. It's useful for installing dependencies with apt-get, yum, dnf, etc. For example:<pre> - name: Start EC2 runner<br> with:<br> mode: start<br> ...<br> pre-runner-script: \|<br> sudo yum update -y && \ <br> sudo yum install docker git libicu -y<br> sudo systemctl enable docker</pre>
<br><br> |
`key-name` | Optional. Used only with the `start` mode. | Specifies SSH key-pair name to assign to an instance. This is useful for SSHing into an instance for debugging.<br><br> |
| `block-device-mappings` | Optional. Used only with the `start` mode. | JSON string specifying the [BlockDeviceMapping](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-blockdev-mapping.html). For example:<pre> block-device-mapper: \|<br> [<br> {"DeviceName" : "/dev/sda1", "Ebs" : { "VolumeType": "gp2", "VolumeSize": 34 }},<br> {"DeviceName" : "/dev/sdb", "VirtualName": "ephemeral0" }<br> ]
</pre>|

### Environment variables

Expand Down Expand Up @@ -263,6 +265,12 @@ jobs:
{"Key": "Name", "Value": "ec2-github-runner"},
{"Key": "GitHubRepository", "Value": "${{ github.repository }}"}
]
key-name: my-ssh-key # optional
block-device-mapper: | # optional
[
{"DeviceName" : "/dev/sda1", "Ebs" : { "VolumeType": "gp2", "VolumeSize": 34 }},
{"DeviceName" : "/dev/sdb", "VirtualName": "ephemeral0" }
]
do-the-job:
name: Do the job on the runner
needs: start-runner # required to start the main job when the runner is ready
Expand Down
9 changes: 9 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ inputs:
description: >-
Specifies bash commands to run before the runner starts. It's useful for installing dependencies with apt-get, yum, dnf, etc.
required: false
block-device-mappings:
description: >-
JSON string of EC2 BlockDeviceMapping (https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-blockdev-mapping.html).
required: false
default: '[]'
key-name:
description: >-
Assign SSH key-pair name to an instance. This can be useful for SSHing into an instance for debugging.
required: false

outputs:
label:
Expand Down
30 changes: 25 additions & 5 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62802,6 +62802,8 @@ const AWS = __webpack_require__(71786);
const core = __webpack_require__(42186);
const config = __webpack_require__(34570);

// https://github.com/actions/runner/releases

// User data scripts are run as the root user
function buildUserDataScript(githubRegistrationToken, label) {
if (config.input.runnerHomeDir) {
Expand All @@ -62813,7 +62815,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} --token ${githubRegistrationToken} --labels ${label} --ephemeral`,
'./run.sh',
];
} else {
Expand All @@ -62823,10 +62825,11 @@ function buildUserDataScript(githubRegistrationToken, label) {
`echo "${config.input.preRunnerScript}" > pre-runner-script.sh`,
'source pre-runner-script.sh',
'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',
'export RUNNER_VERSION="2.309.0"',
'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',
];
}
Expand All @@ -62847,8 +62850,19 @@ async function startEc2Instance(label, githubRegistrationToken) {
SecurityGroupIds: [config.input.securityGroupId],
IamInstanceProfile: { Name: config.input.iamRoleName },
TagSpecifications: config.tagSpecifications,
BlockDeviceMappings: config.blockDeviceMappings,
};

if (config.blockDeviceMappings !== null) {
params.BlockDeviceMappings = config.blockDeviceMappings;
}

if (config.input.keyName !== '') {
params.KeyName = config.input.keyName;
}

core.debug(`AWS EC2 runInstances params: ${JSON.stringify(params, null, 2)}`);

try {
const result = await ec2.runInstances(params).promise();
const ec2InstanceId = result.Instances[0].InstanceId;
Expand Down Expand Up @@ -62923,6 +62937,7 @@ class Config {
iamRoleName: core.getInput('iam-role-name'),
runnerHomeDir: core.getInput('runner-home-dir'),
preRunnerScript: core.getInput('pre-runner-script'),
keyName: core.getInput('key-name'),
};

const tags = JSON.parse(core.getInput('aws-resource-tags'));
Expand All @@ -62931,6 +62946,11 @@ class Config {
this.tagSpecifications = [{ResourceType: 'instance', Tags: tags}, {ResourceType: 'volume', Tags: tags}];
}

const mappings = JSON.parse(core.getInput('block-device-mappings'));
this.blockDeviceMappings = null;
if (mappings.length > 0) {
this.blockDeviceMappings = mappings;
}
// the values of github.context.repo.owner and github.context.repo.repo are taken from
// the environment variable GITHUB_REPOSITORY specified in "owner/repo" format and
// provided by the GitHub Action on the runtime
Expand Down Expand Up @@ -63099,8 +63119,8 @@ async function start() {
}

async function stop() {
await aws.terminateEc2Instance();
await gh.removeRunner();
await aws.terminateEc2Instance();
}

(async function () {
Expand Down
13 changes: 12 additions & 1 deletion src/aws.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function buildUserDataScript(githubRegistrationToken, label) {
'source pre-runner-script.sh',
'case $(uname -m) in aarch64) ARCH="arm64" ;; amd64|x86_64) ARCH="x64" ;; esac && export RUNNER_ARCH=${ARCH}',
'export RUNNER_VERSION="2.309.0"',
'curl -O -L https://github.com/actions/runner/releases/download/${RUNNER_VERSION}/actions-runner-linux-${RUNNER_ARCH}-${RUNNER_VERSION}.tar.gz',
'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} --ephemeral`,
Expand All @@ -50,8 +50,19 @@ async function startEc2Instance(label, githubRegistrationToken) {
SecurityGroupIds: [config.input.securityGroupId],
IamInstanceProfile: { Name: config.input.iamRoleName },
TagSpecifications: config.tagSpecifications,
BlockDeviceMappings: config.blockDeviceMappings,
};

if (config.blockDeviceMappings !== null) {
params.BlockDeviceMappings = config.blockDeviceMappings;
}

if (config.input.keyName !== '') {
params.KeyName = config.input.keyName;
}

core.debug(`AWS EC2 runInstances params: ${JSON.stringify(params, null, 2)}`);

try {
const result = await ec2.runInstances(params).promise();
const ec2InstanceId = result.Instances[0].InstanceId;
Expand Down
6 changes: 6 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Config {
iamRoleName: core.getInput('iam-role-name'),
runnerHomeDir: core.getInput('runner-home-dir'),
preRunnerScript: core.getInput('pre-runner-script'),
keyName: core.getInput('key-name'),
};

const tags = JSON.parse(core.getInput('aws-resource-tags'));
Expand All @@ -23,6 +24,11 @@ class Config {
this.tagSpecifications = [{ResourceType: 'instance', Tags: tags}, {ResourceType: 'volume', Tags: tags}];
}

const mappings = JSON.parse(core.getInput('block-device-mappings'));
this.blockDeviceMappings = null;
if (mappings.length > 0) {
this.blockDeviceMappings = mappings;
}
// the values of github.context.repo.owner and github.context.repo.repo are taken from
// the environment variable GITHUB_REPOSITORY specified in "owner/repo" format and
// provided by the GitHub Action on the runtime
Expand Down

0 comments on commit f6823ac

Please sign in to comment.