Skip to content

Commit

Permalink
Merge pull request #1 from troinine/main
Browse files Browse the repository at this point in the history
feat: support spot instances (#1)
  • Loading branch information
DevopsGoth authored Feb 10, 2024
2 parents 2c4d1dc + 4efaa2c commit 6a8bc32
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,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
9 changes: 7 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ inputs:
required: false
ec2-instance-type:
description: >-
EC2 Instance Type.
EC2 Instance Type.
This input is required if you use the 'start' mode.
required: false
subnet-id:
Expand All @@ -32,7 +32,7 @@ inputs:
required: false
security-group-id:
description: >-
EC2 Security Group Id.
EC2 Security Group Id.
The security group should belong to the same VPC as the specified subnet.
The runner doesn't require any inbound traffic. However, outbound traffic should be allowed.
This input is required if you use the 'start' mode.
Expand Down Expand Up @@ -69,6 +69,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
24 changes: 23 additions & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62832,6 +62832,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 startEc2Instance(label, githubRegistrationToken) {
const ec2 = new AWS.EC2();

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

try {
Expand Down Expand Up @@ -62923,12 +62937,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 @@ -62955,6 +62973,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.ec2InstanceId) {
throw new Error(`Not all the required inputs are provided for the 'stop' mode`);
Expand Down
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 startEc2Instance(label, githubRegistrationToken) {
const ec2 = new AWS.EC2();

Expand All @@ -47,6 +60,7 @@ async function startEc2Instance(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 @@ -15,12 +15,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 @@ -47,6 +51,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.ec2InstanceId) {
throw new Error(`Not all the required inputs are provided for the 'stop' mode`);
Expand Down

0 comments on commit 6a8bc32

Please sign in to comment.