diff --git a/README.md b/README.md index e3b4e890..28c4d159 100644 --- a/README.md +++ b/README.md @@ -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.

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.

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.

This field is a stringified JSON array of tag objects, each containing a `Key` and `Value` field (see example below).

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.

| +| `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:
          - name: Start EC2 runner
with:
mode: start
...
pre-runner-script: \|
sudo yum update -y && \
sudo yum install docker git libicu -y
sudo systemctl enable docker


| diff --git a/action.yml b/action.yml index 09fa9591..7d97d7de 100644 --- a/action.yml +++ b/action.yml @@ -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: @@ -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. @@ -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: diff --git a/dist/index.js b/dist/index.js index 409180d4..adb174f4 100644 --- a/dist/index.js +++ b/dist/index.js @@ -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(); @@ -62847,6 +62860,7 @@ async function startEc2Instance(label, githubRegistrationToken) { SecurityGroupIds: [config.input.securityGroupId], IamInstanceProfile: { Name: config.input.iamRoleName }, TagSpecifications: config.tagSpecifications, + InstanceMarketOptions: buildMarketOptions(), }; try { @@ -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 @@ -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`); diff --git a/src/aws.js b/src/aws.js index bcf53646..4578ae63 100644 --- a/src/aws.js +++ b/src/aws.js @@ -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(); @@ -47,6 +60,7 @@ async function startEc2Instance(label, githubRegistrationToken) { SecurityGroupIds: [config.input.securityGroupId], IamInstanceProfile: { Name: config.input.iamRoleName }, TagSpecifications: config.tagSpecifications, + InstanceMarketOptions: buildMarketOptions(), }; try { diff --git a/src/config.js b/src/config.js index 1100f51e..cb3661f9 100644 --- a/src/config.js +++ b/src/config.js @@ -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 @@ -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`);