Skip to content

Commit f6823ac

Browse files
committed
Add 'key-name' and 'block-device-mapping' options (machulav#156)
machulav#156
1 parent f8fe7f5 commit f6823ac

File tree

5 files changed

+61
-7
lines changed

5 files changed

+61
-7
lines changed

README.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,9 @@ Now you're ready to go!
206206
| `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). |
207207
| `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> |
208208
| `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>
209-
<br><br> |
209+
`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> |
210+
| `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> ]
211+
</pre>|
210212

211213
### Environment variables
212214

@@ -263,6 +265,12 @@ jobs:
263265
{"Key": "Name", "Value": "ec2-github-runner"},
264266
{"Key": "GitHubRepository", "Value": "${{ github.repository }}"}
265267
]
268+
key-name: my-ssh-key # optional
269+
block-device-mapper: | # optional
270+
[
271+
{"DeviceName" : "/dev/sda1", "Ebs" : { "VolumeType": "gp2", "VolumeSize": 34 }},
272+
{"DeviceName" : "/dev/sdb", "VirtualName": "ephemeral0" }
273+
]
266274
do-the-job:
267275
name: Do the job on the runner
268276
needs: start-runner # required to start the main job when the runner is ready

action.yml

+9
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,15 @@ inputs:
6969
description: >-
7070
Specifies bash commands to run before the runner starts. It's useful for installing dependencies with apt-get, yum, dnf, etc.
7171
required: false
72+
block-device-mappings:
73+
description: >-
74+
JSON string of EC2 BlockDeviceMapping (https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-blockdev-mapping.html).
75+
required: false
76+
default: '[]'
77+
key-name:
78+
description: >-
79+
Assign SSH key-pair name to an instance. This can be useful for SSHing into an instance for debugging.
80+
required: false
7281

7382
outputs:
7483
label:

dist/index.js

+25-5
Original file line numberDiff line numberDiff line change
@@ -62802,6 +62802,8 @@ const AWS = __webpack_require__(71786);
6280262802
const core = __webpack_require__(42186);
6280362803
const config = __webpack_require__(34570);
6280462804

62805+
// https://github.com/actions/runner/releases
62806+
6280562807
// User data scripts are run as the root user
6280662808
function buildUserDataScript(githubRegistrationToken, label) {
6280762809
if (config.input.runnerHomeDir) {
@@ -62813,7 +62815,7 @@ function buildUserDataScript(githubRegistrationToken, label) {
6281362815
`echo "${config.input.preRunnerScript}" > pre-runner-script.sh`,
6281462816
'source pre-runner-script.sh',
6281562817
'export RUNNER_ALLOW_RUNASROOT=1',
62816-
`./config.sh --url https://github.com/${config.githubContext.owner}/${config.githubContext.repo} --token ${githubRegistrationToken} --labels ${label}`,
62818+
`./config.sh --url https://github.com/${config.githubContext.owner}/${config.githubContext.repo} --token ${githubRegistrationToken} --labels ${label} --ephemeral`,
6281762819
'./run.sh',
6281862820
];
6281962821
} else {
@@ -62823,10 +62825,11 @@ function buildUserDataScript(githubRegistrationToken, label) {
6282362825
`echo "${config.input.preRunnerScript}" > pre-runner-script.sh`,
6282462826
'source pre-runner-script.sh',
6282562827
'case $(uname -m) in aarch64) ARCH="arm64" ;; amd64|x86_64) ARCH="x64" ;; esac && export RUNNER_ARCH=${ARCH}',
62826-
'curl -O -L https://github.com/actions/runner/releases/download/v2.299.1/actions-runner-linux-${RUNNER_ARCH}-2.299.1.tar.gz',
62827-
'tar xzf ./actions-runner-linux-${RUNNER_ARCH}-2.299.1.tar.gz',
62828+
'export RUNNER_VERSION="2.309.0"',
62829+
'curl -O -L https://github.com/actions/runner/releases/download/v${RUNNER_VERSION}/actions-runner-linux-${RUNNER_ARCH}-${RUNNER_VERSION}.tar.gz',
62830+
'tar xzf ./actions-runner-linux-${RUNNER_ARCH}-${RUNNER_VERSION}.tar.gz',
6282862831
'export RUNNER_ALLOW_RUNASROOT=1',
62829-
`./config.sh --url https://github.com/${config.githubContext.owner}/${config.githubContext.repo} --token ${githubRegistrationToken} --labels ${label}`,
62832+
`./config.sh --url https://github.com/${config.githubContext.owner}/${config.githubContext.repo} --token ${githubRegistrationToken} --labels ${label} --ephemeral`,
6283062833
'./run.sh',
6283162834
];
6283262835
}
@@ -62847,8 +62850,19 @@ async function startEc2Instance(label, githubRegistrationToken) {
6284762850
SecurityGroupIds: [config.input.securityGroupId],
6284862851
IamInstanceProfile: { Name: config.input.iamRoleName },
6284962852
TagSpecifications: config.tagSpecifications,
62853+
BlockDeviceMappings: config.blockDeviceMappings,
6285062854
};
6285162855

62856+
if (config.blockDeviceMappings !== null) {
62857+
params.BlockDeviceMappings = config.blockDeviceMappings;
62858+
}
62859+
62860+
if (config.input.keyName !== '') {
62861+
params.KeyName = config.input.keyName;
62862+
}
62863+
62864+
core.debug(`AWS EC2 runInstances params: ${JSON.stringify(params, null, 2)}`);
62865+
6285262866
try {
6285362867
const result = await ec2.runInstances(params).promise();
6285462868
const ec2InstanceId = result.Instances[0].InstanceId;
@@ -62923,6 +62937,7 @@ class Config {
6292362937
iamRoleName: core.getInput('iam-role-name'),
6292462938
runnerHomeDir: core.getInput('runner-home-dir'),
6292562939
preRunnerScript: core.getInput('pre-runner-script'),
62940+
keyName: core.getInput('key-name'),
6292662941
};
6292762942

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

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

6310163121
async function stop() {
63102-
await aws.terminateEc2Instance();
6310363122
await gh.removeRunner();
63123+
await aws.terminateEc2Instance();
6310463124
}
6310563125

6310663126
(async function () {

src/aws.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function buildUserDataScript(githubRegistrationToken, label) {
2626
'source pre-runner-script.sh',
2727
'case $(uname -m) in aarch64) ARCH="arm64" ;; amd64|x86_64) ARCH="x64" ;; esac && export RUNNER_ARCH=${ARCH}',
2828
'export RUNNER_VERSION="2.309.0"',
29-
'curl -O -L https://github.com/actions/runner/releases/download/${RUNNER_VERSION}/actions-runner-linux-${RUNNER_ARCH}-${RUNNER_VERSION}.tar.gz',
29+
'curl -O -L https://github.com/actions/runner/releases/download/v${RUNNER_VERSION}/actions-runner-linux-${RUNNER_ARCH}-${RUNNER_VERSION}.tar.gz',
3030
'tar xzf ./actions-runner-linux-${RUNNER_ARCH}-${RUNNER_VERSION}.tar.gz',
3131
'export RUNNER_ALLOW_RUNASROOT=1',
3232
`./config.sh --url https://github.com/${config.githubContext.owner}/${config.githubContext.repo} --token ${githubRegistrationToken} --labels ${label} --ephemeral`,
@@ -50,8 +50,19 @@ async function startEc2Instance(label, githubRegistrationToken) {
5050
SecurityGroupIds: [config.input.securityGroupId],
5151
IamInstanceProfile: { Name: config.input.iamRoleName },
5252
TagSpecifications: config.tagSpecifications,
53+
BlockDeviceMappings: config.blockDeviceMappings,
5354
};
5455

56+
if (config.blockDeviceMappings !== null) {
57+
params.BlockDeviceMappings = config.blockDeviceMappings;
58+
}
59+
60+
if (config.input.keyName !== '') {
61+
params.KeyName = config.input.keyName;
62+
}
63+
64+
core.debug(`AWS EC2 runInstances params: ${JSON.stringify(params, null, 2)}`);
65+
5566
try {
5667
const result = await ec2.runInstances(params).promise();
5768
const ec2InstanceId = result.Instances[0].InstanceId;

src/config.js

+6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class Config {
1515
iamRoleName: core.getInput('iam-role-name'),
1616
runnerHomeDir: core.getInput('runner-home-dir'),
1717
preRunnerScript: core.getInput('pre-runner-script'),
18+
keyName: core.getInput('key-name'),
1819
};
1920

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

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

0 commit comments

Comments
 (0)