|
1 |
| -# (In Progress) "aws-github-runner" action for GitHub Actions |
| 1 | +# On-demand self-hosted EC2 runner for GitHub Actions |
2 | 2 |
|
3 |
| -Create an on-demand AWS EC2 instance and register it as a self-hosted GitHub Actions runner for your GitHub repository. |
| 3 | +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. |
4 | 4 |
|
5 |
| -The runner is automatically started when the GitHub Actions workflow starts, runs all your jobs and is removed after the work is done. |
| 5 | +**Table of Contents** |
6 | 6 |
|
7 |
| -# Notes |
| 7 | +- [Usage](#usage) |
| 8 | + - [Inputs](#inputs) |
| 9 | + - [Environment variables](#environment-variables) |
| 10 | + - [Outputs](#outputs) |
| 11 | + - [Example](#example) |
| 12 | +- [License Summary](#license-summary) |
8 | 13 |
|
9 |
| -## GitHub Secret Token |
| 14 | +## Usage |
10 | 15 |
|
11 |
| -Your GitHub Secret Token should have `repo` scope assigned. |
| 16 | +### Inputs |
| 17 | + |
| 18 | +| Name | Required | Description | |
| 19 | +| ------------------- | ------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | |
| 20 | +| `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. | |
| 21 | +| `github-token` | Always. | GitHub Personal Access Token with a `repo` scope assigned. | |
| 22 | +| `ec2-image-id` | Required if you use the `start` mode. | EC2 AMI Id. <br><br> The new runner will be launched from this image. | |
| 23 | +| `ec2-instance-type` | Required if you use the `start` mode. | EC2 Instance Type. | |
| 24 | +| `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. | |
| 25 | +| `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. | |
| 26 | +| `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. | |
| 27 | +| `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. | |
| 28 | + |
| 29 | +### Environment variables |
| 30 | + |
| 31 | +In addition to the inputs described above, the action also requires the following environment variables to access your AWS account: |
| 32 | + |
| 33 | +- `AWS_DEFAULT_REGION` |
| 34 | +- `AWS_REGION` |
| 35 | +- `AWS_ACCESS_KEY_ID` |
| 36 | +- `AWS_SECRET_ACCESS_KEY` |
| 37 | + |
| 38 | +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. |
| 39 | + |
| 40 | +### Outputs |
| 41 | + |
| 42 | +| Name | Description | |
| 43 | +| ----------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | |
| 44 | +| `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. | |
| 45 | +| `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. | |
| 46 | + |
| 47 | +### Example |
| 48 | + |
| 49 | +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: |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | +The workflow, declared in `.github/workflows/do-the-job.yml`, looks like this: |
| 54 | + |
| 55 | +```yml |
| 56 | +name: do-the-job |
| 57 | +on: pull_request |
| 58 | +jobs: |
| 59 | + start-runner: |
| 60 | + name: Start self-hosted EC2 runner |
| 61 | + runs-on: ubuntu-latest |
| 62 | + outputs: |
| 63 | + label: ${{ steps.start-ec2-runner.outputs.label }} |
| 64 | + ec2-instance-id: ${{ steps.start-ec2-runner.outputs.ec2-instance-id }} |
| 65 | + steps: |
| 66 | + - name: Configure AWS credentials |
| 67 | + uses: aws-actions/configure-aws-credentials@v1 |
| 68 | + with: |
| 69 | + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} |
| 70 | + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} |
| 71 | + aws-region: ${{ secrets.AWS_REGION }} |
| 72 | + - name: Start EC2 runner |
| 73 | + id: start-ec2-runner |
| 74 | + uses: machulav/ec2-github-runner@main |
| 75 | + with: |
| 76 | + mode: start |
| 77 | + github-token: ${{ secrets.GH_PERSONAL_ACCESS_TOKEN }} |
| 78 | + ec2-image-id: ami-123 |
| 79 | + ec2-instance-type: t3.nano |
| 80 | + subnet-id: subnet-123 |
| 81 | + security-group-id: sg-123 |
| 82 | + do-the-job: |
| 83 | + name: Do the job |
| 84 | + runs-on: ${{ needs.start-runner.outputs.label }} # run the job on the newly created runner |
| 85 | + needs: start-runner # required to start the main job when the runner is ready |
| 86 | + steps: |
| 87 | + - name: Hello World |
| 88 | + run: echo 'Hello World!' |
| 89 | + stop-runner: |
| 90 | + name: Stop self-hosted EC2 runner |
| 91 | + runs-on: ubuntu-latest |
| 92 | + needs: |
| 93 | + - start-runner # required to get output from the job in this job |
| 94 | + - do-the-job # required to remove the runner when the main job is done |
| 95 | + steps: |
| 96 | + - name: Configure AWS credentials |
| 97 | + uses: aws-actions/configure-aws-credentials@v1 |
| 98 | + with: |
| 99 | + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} |
| 100 | + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} |
| 101 | + aws-region: ${{ secrets.AWS_REGION }} |
| 102 | + - name: Stop EC2 runner |
| 103 | + uses: machulav/aws-github-runner@main |
| 104 | + with: |
| 105 | + mode: stop |
| 106 | + github-token: ${{ secrets.GH_PERSONAL_ACCESS_TOKEN }} |
| 107 | + label: ${{ needs.start-runner.outputs.label }} |
| 108 | + ec2-instance-id: ${{ needs.start-runner.outputs.ec2-instance-id }} |
| 109 | +``` |
| 110 | +
|
| 111 | +## License Summary |
| 112 | +
|
| 113 | +This code is made available under the MIT license. |
0 commit comments