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

feat: support spot instances (#1) #3

Merged
merged 2 commits into from
Dec 18, 2023
Merged
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ Now you're ready to go!
| `iam-role-name` | Optional. Used only with the `start` mode. | IAM role name to attach to the created EC2 runner. <br><br> This allows the runner to have permissions to run additional actions within the AWS account, without having to manage additional GitHub secrets and AWS users. <br><br> Setting this requires additional AWS permissions for the role launching the instance (see above). |
| `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> |
| `market-type` | Optional. Used only with the `start` mode. | Specifies the market (purchasing) option for the instance. Allowed values: `spot`. The default is to use an on-demand instance.
| `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> |

Expand Down
5 changes: 5 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ 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
market-type:
description: >-
Specifies the market (purchasing) option for the instance:
- 'spot' - Use a spot instance
required: false

outputs:
label:
Expand Down
26 changes: 24 additions & 2 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65611,6 +65611,19 @@ function buildUserDataScript(githubRegistrationToken, label) {
}
}

function buildMarketOptions() {
if (config.input.marketType === 'spot') {
return {
MarketType: config.input.marketType,
SpotOptions: {
SpotInstanceType: 'one-time',
},
};
}

return undefined;
}

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

Expand All @@ -65626,6 +65639,7 @@ async function startEc2Instances(label, githubRegistrationToken) {
SecurityGroupIds: [config.input.securityGroupId],
IamInstanceProfile: { Name: config.input.iamRoleName },
TagSpecifications: config.tagSpecifications,
InstanceMarketOptions: buildMarketOptions(),
};

try {
Expand Down Expand Up @@ -65705,12 +65719,16 @@ class Config {
iamRoleName: core.getInput('iam-role-name'),
runnerHomeDir: core.getInput('runner-home-dir'),
preRunnerScript: core.getInput('pre-runner-script'),
marketType: core.getInput('market-type'),
};

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 @@ -65737,6 +65755,10 @@ class Config {
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`);
}

if (this.marketType?.length > 0 && this.input.marketType !== 'spot') {
throw new Error(`Invalid 'market-type' input. Allowed values: spot.`);
}
} else if (this.input.mode === 'stop') {
if (!this.input.label || !this.input.ec2InstanceIds) {
throw new Error(`Not all the required inputs are provided for the 'stop' mode`);
Expand Down Expand Up @@ -72694,4 +72716,4 @@ module.exports = require("zlib");;
/******/ // Load entry module and return exports
/******/ return __webpack_require__(4351);
/******/ })()
;
;
14 changes: 14 additions & 0 deletions src/aws.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@ function buildUserDataScript(githubRegistrationToken, label) {
}
}

function buildMarketOptions() {
if (config.input.marketType === 'spot') {
return {
MarketType: config.input.marketType,
SpotOptions: {
SpotInstanceType: 'one-time',
},
};
}

return undefined;
}

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

Expand All @@ -47,6 +60,7 @@ async function startEc2Instances(label, githubRegistrationToken) {
SecurityGroupIds: [config.input.securityGroupId],
IamInstanceProfile: { Name: config.input.iamRoleName },
TagSpecifications: config.tagSpecifications,
InstanceMarketOptions: buildMarketOptions(),
};

try {
Expand Down
10 changes: 9 additions & 1 deletion src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@ class Config {
iamRoleName: core.getInput('iam-role-name'),
runnerHomeDir: core.getInput('runner-home-dir'),
preRunnerScript: core.getInput('pre-runner-script'),
marketType: core.getInput('market-type'),
};

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 @@ -48,6 +52,10 @@ class Config {
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`);
}

if (this.marketType?.length > 0 && this.input.marketType !== 'spot') {
throw new Error(`Invalid 'market-type' input. Allowed values: spot.`);
}
} else if (this.input.mode === 'stop') {
if (!this.input.label || !this.input.ec2InstanceIds) {
throw new Error(`Not all the required inputs are provided for the 'stop' mode`);
Expand Down