Skip to content

Commit

Permalink
feat(config): handle slab profile as input
Browse files Browse the repository at this point in the history
  • Loading branch information
soonum committed Jan 29, 2024
1 parent 67eeef5 commit b5f62b0
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 52 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,26 @@ jobs:
region: eu-west-3
label: ${{ steps.test-start.outputs.label }}
ec2-instance-id: ${{ steps.test-start.outputs.ec2-instance-id }}

- name: Test start instance with profile
id: test-start-profile
uses: ./
with:
mode: start
github-token: ${{ secrets.SLAB_ACTION_TOKEN }}
slab-url: ${{ secrets.SLAB_BASE_URL }}
job-secret: ${{ secrets.JOB_SECRET }}
profile: ci-test

- name: Test stop instance with profile
id: test-stop-profile
if: ${{ always() }}
uses: ./
with:
mode: stop
github-token: ${{ secrets.SLAB_ACTION_TOKEN }}
slab-url: ${{ secrets.SLAB_BASE_URL }}
job-secret: ${{ secrets.JOB_SECRET }}
profile: ci-test
label: ${{ steps.test-start-profile.outputs.label }}
ec2-instance-id: ${{ steps.test-start-profile.outputs.ec2-instance-id }}
27 changes: 14 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,20 @@ See [below](#example) the YAML code of the depicted workflow.

### Inputs

| Name | Required | Description |
|----------------------|--------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `mode` | Always required. | Specify here which mode you want to use: `start` to start a new runner, `stop` to stop the previously created runner. |
| `github-token` | Always required. | GitHub Personal Access Token with the `repo` scope assigned. |
| `slab-url` | Always required. | URL to Slab CI server. |
| `job-secret` | Always required. | Secret key used by Slab to perform HMAC computation. |
| `region` | Always required. | AWS deploy region. |
| `ec2-image-id` | Required if you use the `start` mode. | EC2 Image ID (AMI).The new runner will be launched from this image. The action is compatible with Amazon Linux 2 images. |
| `ec2-instance-type` | Required if you use the `start` mode. | EC2 Instance Type. |
| `subnet-id` | Optional. Used only with the `start` mode. | VPC Subnet ID.The subnet should belong to the same VPC as the specified security group. |
| `security-group-ids` | Optional. Used only with the `start` mode. | EC2 Security Group IDs.The security group should belong to the same VPC as the specified subnet.Only the outbound traffic for port 443 should be allowed. No inbound traffic is required. |
| `label` | Required if you use the `stop` mode. | Name of the unique label assigned to the runner.The label is provided by the output of the action in the `start` mode.The label is used to remove the runner from GitHub when the runner is not needed anymore. |
| `ec2-instance-id` | Required if you use the `stop` mode. | EC2 Instance ID of the created runner.The ID is provided by the output of the action in the `start` mode.The ID is used to terminate the EC2 instance when the runner is not needed anymore. | |
| Name | Required | Description |
|----------------------|-------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `mode` | Always required. | Specify here which mode you want to use: `start` to start a new runner, `stop` to stop the previously created runner. |
| `github-token` | Always required. | GitHub Personal Access Token with the `repo` scope assigned. |
| `slab-url` | Always required. | URL to Slab CI server. |
| `job-secret` | Always required. | Secret key used by Slab to perform HMAC computation. |
| `profile` | Optional. | Profile to use as described slab.toml file in repository that uses the action. |
| `region` | Required if you don't use `profile`. | AWS deploy region. |
| `ec2-image-id` | Required if you don't use `profile` with `start` mode. | EC2 Image ID (AMI).The new runner will be launched from this image. The action is compatible with Amazon Linux 2 images. |
| `ec2-instance-type` | Required if you don't use `profile` with `start` mode. | EC2 Instance Type. |
| `subnet-id` | Optional. Used only if you don't use `profile` with `start` mode. | VPC Subnet ID.The subnet should belong to the same VPC as the specified security group. |
| `security-group-ids` | Optional. Used only if you don't use `profile` with `start` mode. | EC2 Security Group IDs.The security group should belong to the same VPC as the specified subnet.Only the outbound traffic for port 443 should be allowed. No inbound traffic is required. |
| `label` | Required if you don't use `profile` with `stop` mode. | Name of the unique label assigned to the runner.The label is provided by the output of the action in the `start` mode.The label is used to remove the runner from GitHub when the runner is not needed anymore. |
| `ec2-instance-id` | Required if you don't use `profile` with `stop` mode. | EC2 Instance ID of the created runner.The ID is provided by the output of the action in the `start` mode.The ID is used to terminate the EC2 instance when the runner is not needed anymore. | |

### Outputs

Expand Down
11 changes: 10 additions & 1 deletion action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,39 @@ inputs:
description: >-
Secret key used by Slab to perform HMAC computation.
required: true
profile:
description: >-
Profile to use as described slab.toml file in repository that uses the action.
required: false
region:
description: >-
AWS deployment region.
required: true
Mutually exclusive with 'profile' input.
required: false
ec2-image-id:
description: >-
EC2 Image Id (AMI). The new runner will be launched from this image.
This input is required if you use the 'start' mode.
Mutually exclusive with 'profile' input.
required: false
ec2-instance-type:
description: >-
EC2 Instance Type.
This input is required if you use the 'start' mode.
Mutually exclusive with 'profile' input.
required: false
subnet-id:
description: >-
VPC Subnet Id. The subnet should belong to the same VPC as the specified security group.
This input is required if you use the 'start' mode.
Mutually exclusive with 'profile' input.
required: false
security-group-ids:
description: >-
EC2 Security Group Ids.
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.
Mutually exclusive with 'profile' input.
required: false
label:
description: >-
Expand Down
6 changes: 6 additions & 0 deletions ci/slab.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# This profile is dedicated to test action behavior by replicating
#profiles that can be found on repository using this workflow.
[profile.ci-test]
region = "eu-west-3"
image_id = "ami-01d21b7be69801c2f" # Ubuntu 22.04
instance_type = "t3.2xlarge"
79 changes: 60 additions & 19 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 18 additions & 3 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Config {
githubToken: core.getInput('github-token'),
slabUrl: core.getInput('slab-url'),
jobSecret: core.getInput('job-secret'),
profile: core.getInput('profile'),
region: core.getInput('region'),
ec2ImageId: core.getInput('ec2-image-id'),
ec2InstanceType: core.getInput('ec2-instance-type'),
Expand All @@ -23,7 +24,8 @@ class Config {
this.githubContext = {
owner: github.context.repo.owner,
repo: github.context.repo.repo,
sha: github.context.sha
sha: github.context.sha,
ref: github.context.ref
}

//
Expand All @@ -46,11 +48,24 @@ class Config {
throw new Error(`The 'job-secret' input is not specified`)
}

if (!this.input.region) {
if (
this.input.profile &&
(this.input.region ||
this.input.ec2ImageId ||
this.input.ec2InstanceType ||
this.input.subnetId ||
this.input.securityGroupIds)
) {
throw new Error(
`The 'profile' input is mutually exclusive with any AWS related inputs`
)
}

if (!this.input.profile && !this.input.region) {
throw new Error(`The 'region' input is not specified`)
}

if (this.input.mode === 'start') {
if (this.input.mode === 'start' && !this.input.profile) {
if (!this.input.ec2ImageId || !this.input.ec2InstanceType) {
throw new Error(
`Not all the required inputs are provided for the 'start' mode`
Expand Down
58 changes: 42 additions & 16 deletions src/slab.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,36 @@ function getSignature(content) {

async function startInstanceRequest() {
const url = config.input.slabUrl
const payload = {
region: config.input.region,
image_id: config.input.ec2ImageId,
instance_type: config.input.ec2InstanceType,
sha: config.githubContext.sha
}
if (config.input.subnetId) {
payload.subnet_id = config.input.subnetId
}
if (config.input.securityGroupIds) {
payload.security_group_ids = config.input.securityGroupIds
let payload

if (config.input.profile) {
payload = {
details: { profile: config.input.profile }
}
} else {
payload = {
details: {
custom_start: {
region: config.input.region,
image_id: config.input.ec2ImageId,
instance_type: config.input.ec2InstanceType
}
}
}
if (config.input.subnetId) {
payload.details.custom_start.subnet_id = config.input.subnetId
}
if (config.input.securityGroupIds) {
payload.details.custom_start.security_group_ids =
config.input.securityGroupIds
}
}
payload.sha = config.githubContext.sha
payload.git_ref = config.githubContext.ref

const body = JSON.stringify(payload)
const signature = getSignature(body)
console.log(`BODY: ${body}`)

try {
core.info('Request AWS EC2 instance start')
Expand Down Expand Up @@ -86,12 +101,23 @@ async function waitForInstance(taskId, taskName) {

async function terminateInstanceRequest(runnerName) {
const url = config.input.slabUrl
const payload = {
region: config.input.region,
runner_name: runnerName,
action: 'terminate',
sha: config.githubContext.sha
let payload
if (config.input.profile) {
payload = {
details: {
profile: config.input.profile
}
}
} else {
payload = {
details: { custom_stop: { region: config.input.region } }
}
}
payload.runner_name = runnerName
payload.action = 'terminate'
payload.sha = config.githubContext.sha
payload.git_ref = config.githubContext.ref

const body = JSON.stringify(payload)
const signature = getSignature(body)

Expand Down

0 comments on commit b5f62b0

Please sign in to comment.