This repository has been archived by the owner on Oct 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaction.yml
70 lines (65 loc) · 2.79 KB
/
action.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
---
name: Stop EC2 actions runner
description: Deregisters and terminates a self-hosted GitHub actions repository runner
inputs:
aws-region:
description: AWS region, e.g. eu-west-1
required: true
aws-access-key-id:
description: AWS access key ID (pass via GitHub secret). Required only if **not** using OIDC
required: false
aws-secret-access-key:
description: AWS secret access key (pass via GitHub secret). Required only if **not** using OIDC
required: false
aws-role-to-assume:
description: AWS IAM role (ARN) to assume, for terminating the instance. Required if using OIDC (AssumeRoleWithWebIdentity)
required: false
github-token:
description: GitHub auth token (PAT with repo scope, pass via GitHub secret). Optional if using ephemeral runners
required: false
github-repo:
description: GitHub repository, e.g. ghost/example. Optional, defaults to the repository where the workflow is run
default: ${{ github.repository }}
required: false
runner-id:
description: GitHub repository runner ID, e.g. 53. Optional if using ephemeral runners
required: false
instance-id:
description: EC2 instance id, e.g. i-0ab3a789090bd84c7
required: true
runs:
using: composite
steps:
- id: auth
uses: aws-actions/configure-aws-credentials@v1
with:
aws-region: ${{ inputs.aws-region }}
aws-access-key-id: ${{ inputs.aws-access-key-id }}
aws-secret-access-key: ${{ inputs.aws-secret-access-key }}
role-to-assume: ${{ inputs.aws-role-to-assume }}
role-session-name: ec2-actions-runner-stop
role-duration-seconds: 900
- id: main
shell: bash
run: |
# We do not fail the job on missing runner ID, because offline and ephemeral runners are automatically deregistered
if [ -n "$RUNNER_ID" ]; then
echo "Deregistering GitHub repository runner: $RUNNER_ID ..."
gh api -X DELETE "repos/$GH_REPO/actions/runners/$RUNNER_ID" \
|| echo "WARN: Failed to deregister GitHub repository runner (GitHub API call failed)"
fi
# Failure to terminate the EC2 instance will incur unnecessary costs, so we fail the job to ensure the user notices
if [ -z "$INSTANCE_ID" ]; then
echo "ERROR: Unable to terminate EC2 instance (instance ID not available)"
exit 1
else
echo "Terminating EC2 instance: $INSTANCE_ID ..."
aws ec2 terminate-instances --instance-ids "$INSTANCE_ID" > /dev/null
echo "Repository runner stopped"
fi
env:
GH_TOKEN: ${{ inputs.github-token }}
AWS_DEFAULT_REGION: ${{ inputs.aws-region }}
RUNNER_ID: ${{ inputs.runner-id }}
GH_REPO: ${{ inputs.github-repo }}
INSTANCE_ID: ${{ inputs.instance-id }}