Skip to content

Commit

Permalink
Update the action's code and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
machulav committed Dec 21, 2020
1 parent 2fdcdf7 commit 7d48b51
Show file tree
Hide file tree
Showing 15 changed files with 18,323 additions and 37,775 deletions.
18 changes: 9 additions & 9 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
#
# - Use environment variables with prefix "AWS_" to give access to your test AWS account.
# - Use environment variables with prefix "INPUT_" to simulate GitHub Actions input.
# - Use environment variables with prefix "STATE_" to simulate GitHub Actions state.
# - Use GITHUB_REPOSITORY environment variable to provide the GitHub repository and it's owher as a context in the script. Use formst "owner/repo".

AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_REGION=
INPUT_GITHUB_TOKEN=
INPUT_EC2_IMAGE_ID=
INPUT_EC2_INSTANCE_TYPE=
INPUT_SUBNET_ID=
INPUT_SECURITY_GROUP_ID=
STATE_EC2_INSTANCE_ID=
STATE_LABEL=
GITHUB_REPOSITORY=
INPUT_MODE=
INPUT_GITHUB-TOKEN=
INPUT_EC2-IMAGE-ID=
INPUT_EC2-INSTANCE-TYPE=
INPUT_SUBNET-ID=
INPUT_SECURITY-GROUP-ID=
INPUT_LABEL=
INPUT_EC2-INSTANCE-ID=
GITHUB_REPOSITORY=
4 changes: 3 additions & 1 deletion .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
env:
commonjs: true
es6: true
node: true
jest: true
Expand All @@ -8,3 +7,6 @@ extends:
parserOptions:
ecmaVersion: 2018
sourceType: module
rules:
no-use-before-define: error
prefer-const: error
8 changes: 0 additions & 8 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,6 @@
"cwd": "${workspaceFolder}",
"runtimeExecutable": "npm",
"runtimeArgs": ["run-script", "index"]
},
{
"name": "debug cleanup",
"type": "node",
"request": "launch",
"cwd": "${workspaceFolder}",
"runtimeExecutable": "npm",
"runtimeArgs": ["run-script", "cleanup"]
}
]
}
114 changes: 108 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,113 @@
# (In Progress) "aws-github-runner" action for GitHub Actions
# On-demand self-hosted EC2 runner for GitHub Actions

Create an on-demand AWS EC2 instance and register it as a self-hosted GitHub Actions runner for your GitHub repository.
Using this GitHub action, you can start a new EC2 instance and register it as a [self-hosted runner in GitHub](<(https://docs.github.com/en/free-pro-team@latest/actions/hosting-your-own-runners)>) right before you need it. Then run all the required jobs on it and stop it when you don't need it anymore.

The runner is automatically started when the GitHub Actions workflow starts, runs all your jobs and is removed after the work is done.
**Table of Contents**

# Notes
- [Usage](#usage)
- [Inputs](#inputs)
- [Environment variables](#environment-variables)
- [Outputs](#outputs)
- [Example](#example)
- [License Summary](#license-summary)

## GitHub Secret Token
## Usage

Your GitHub Secret Token should have `repo` scope assigned.
### Inputs

| Name | Required | Description |
| ------------------- | ------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `mode` | Always. | Specify here which mode you want to use:<br>- `start` - to start a new runner;<br>- `stop` - to stop the previously created runner. |
| `github-token` | Always. | GitHub Personal Access Token with a `repo` scope assigned. |
| `ec2-image-id` | Required if you use the `start` mode. | EC2 AMI Id. <br><br> The new runner will be launched from this image. |
| `ec2-instance-type` | Required if you use the `start` mode. | EC2 Instance Type. |
| `subnet-id` | Required if you use the `start` mode. | VPC Subnet Id. The subnet should belong to the same VPC as the specified security group. |
| `security-group-id` | Required if you use the `start` mode. | EC2 Security Group Id. <br><br> The security group should belong to the same VPC as the specified subnet. <br><br> The runner doesn't require any inbound traffic. However, outbound traffic should be allowed. |
| `label` | Required if you use the `stop` mode. | Name of the unique label assigned to the runner. <br><br> 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. <br><br> The id is used to terminate the EC2 instance when the runner is not needed anymore. |

### Environment variables

In addition to the inputs described above, the action also requires the following environment variables to access your AWS account:

- `AWS_DEFAULT_REGION`
- `AWS_REGION`
- `AWS_ACCESS_KEY_ID`
- `AWS_SECRET_ACCESS_KEY`

We recommend using [aws-actions/configure-aws-credentials](https://github.com/aws-actions/configure-aws-credentials) action right before running the step for creating a self-hosted runner. This action perfectly does the job of setting the required environment variables.

### Outputs

| Name | Description |
| ----------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `label` | Name of the unique label assigned to the runner. <br><br> The label is used in two cases: <br> - to use as the input of `runs-on` property for the following jobs; <br> - to remove the runner from GitHub when it is not needed anymore. |
| `ec2-instance-id` | EC2 Instance Id of the created runner. <br><br> The id is used to terminate the EC2 instance when the runner is not needed anymore. |

### Example

In the following example, you can see how to start your EC2 self-hosted runner right before the job should be done, run the job on it, and then stop it at the end when you finish:

![GitHub Actions self-hosted EC2 runner](docs/images/github-actions-summary.png)

The workflow, declared in `.github/workflows/do-the-job.yml`, looks like this:

```yml
name: do-the-job
on: pull_request
jobs:
start-runner:
name: Start self-hosted EC2 runner
runs-on: ubuntu-latest
outputs:
label: ${{ steps.start-ec2-runner.outputs.label }}
ec2-instance-id: ${{ steps.start-ec2-runner.outputs.ec2-instance-id }}
steps:
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ secrets.AWS_REGION }}
- name: Start EC2 runner
id: start-ec2-runner
uses: machulav/ec2-github-runner@main
with:
mode: start
github-token: ${{ secrets.GH_PERSONAL_ACCESS_TOKEN }}
ec2-image-id: ami-123
ec2-instance-type: t3.nano
subnet-id: subnet-123
security-group-id: sg-123
do-the-job:
name: Do the job
runs-on: ${{ needs.start-runner.outputs.label }} # run the job on the newly created runner
needs: start-runner # required to start the main job when the runner is ready
steps:
- name: Hello World
run: echo 'Hello World!'
stop-runner:
name: Stop self-hosted EC2 runner
runs-on: ubuntu-latest
needs:
- start-runner # required to get output from the job in this job
- do-the-job # required to remove the runner when the main job is done
steps:
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ secrets.AWS_REGION }}
- name: Stop EC2 runner
uses: machulav/aws-github-runner@main
with:
mode: stop
github-token: ${{ secrets.GH_PERSONAL_ACCESS_TOKEN }}
label: ${{ needs.start-runner.outputs.label }}
ec2-instance-id: ${{ needs.start-runner.outputs.ec2-instance-id }}
```
## License Summary
This code is made available under the MIT license.
70 changes: 53 additions & 17 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,26 +1,62 @@
name: aws-github-runner-action
description: GitHub Action for automatic EC2 instance creation and registering it as a self-hosted GitHub Actions runner
name: On-demand self-hosted EC2 runner for GitHub Actions
description: GitHub Action for automatic creation and registration EC2 instance as a GitHub Actions self-hosted runner.
author: Volodymyr Machula
inputs:
github_token:
description: GitHub Secret Token with a 'repo' scope assigned
mode:
description: >-
Specify here which mode you want to use:
- 'start' - to start a new runner;
- 'stop' - to stop the previously created runner.
required: true
ec2_image_id:
description: '' # TODO, possible values: start, stop
required: true
ec2_instance_type:
description: '' # TODO, possible values: start, stop
required: true
subnet_id:
description: '' # TODO, possible values: start, stop
required: true
security_group_id:
description: '' # TODO, possible values: start, stop
github-token:
description: >-
GitHub Personal Access Token with a 'repo' scope assigned.
required: true
ec2-image-id:
description: >-
EC2 AMI Id. The new runner will be launched from this image.
This input is required if you use the 'start' mode.
required: false
ec2-instance-type:
description: >-
EC2 Instance Type.
This input is required if you use the 'start' mode.
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.
required: false
security-group-id:
description: >-
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.
required: false
label:
description: >-
Name of the unique label assigned to the runner.
The label is used to remove the runner from GitHub when the runner is not needed anymore.
This input is required if you use the 'stop' mode.
required: false
ec2-instance-id:
description: >-
EC2 Instance Id of the created runner.
The id is used to terminate the EC2 instance when the runner is not needed anymore.
This input is required if you use the 'stop' mode.
required: false
outputs:
label:
description: '' # TODO
description: >-
Name of the unique label assigned to the runner.
The label is used in two cases:
- to use as the input of 'runs-on' property for the following jobs;
- to remove the runner from GitHub when it is not needed anymore.
ec2-instance-id:
description: >-
EC2 Instance Id of the created runner.
The id is used to terminate the EC2 instance when the runner is not needed anymore.
runs:
using: node12
main: ./dist/index.js
post: ./dist/cleanup/index.js
Loading

0 comments on commit 7d48b51

Please sign in to comment.