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
/
action.yml
129 lines (114 loc) · 4.77 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
---
name: Start EC2 actions runner
description: Starts and registers a self-hosted GitHub actions repository runner (Linux on EC2)
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 launching the instance. Required if using OIDC (AssumeRoleWithWebIdentity)
required: false
aws-launch-template:
description: AWS EC2 launch template (AWS CLI format, e.g. LaunchTemplateId=lt-0abcd290751193123)
required: true
github-token:
description: GitHub token (PAT with repo scope, pass via GitHub secret)
required: true
github-repo:
description: Github repository, e.g. ghost/example. Defaults to current repository
default: ${{ github.repository }}
required: false
runner-labels:
description: Extra runner labels (comma-separated). Can be referenced in job 'runs-on'
required: false
runner-home:
description: Directory that contains actions-runner software and scripts
required: false
default: /home/ubuntu/actions-runner
runner-user:
description: User to run the actions-runner service as
required: false
default: ubuntu
auto-shutdown-at:
description: Automatically shutdown instance at the specified time?. E.g. 'now + 72 hours'. Requires https://linux.die.net/man/1/at
required: false
ephemeral:
description: Flag the runner as ephemeral? An ephemeral runner is automatically de-registered after running _one_ workflow job
required: false
default: true
outputs:
runner-id:
description: GitHub repository runner id
value: ${{ steps.main.outputs.runner-id }}
instance-id:
description: AWS EC2 instance id
value: ${{ steps.main.outputs.instance-id }}
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-start
role-duration-seconds: 900
- id: main
shell: bash
run: |
runner_token="$(gh api -X POST "repos/$GH_REPO/actions/runners/registration-token" | jq -r .token)"
if [ "$EPHEMERAL" = "true" ]; then
extra_flags="--ephemeral"
fi
if [ -n "$RUNNER_LABELS" ]; then
extra_labels=",$RUNNER_LABELS"
fi
user_data=$(cat <<HERE
#!/bin/bash
cd "$RUNNER_HOME"
instance_id="\$(cat /var/lib/cloud/data/instance-id)"
sudo -u "$RUNNER_USER" ./config.sh --unattended $extra_flags --name "\$instance_id" --url "https://github.com/$GH_REPO" --token "$runner_token" --labels "\$instance_id$extra_labels"
./svc.sh install "$RUNNER_USER"
./svc.sh start
test -z "$SHUTDOWN_AT" || echo 'shutdown -P now' | at -M "$SHUTDOWN_AT"
HERE
)
echo "Starting EC2 instance ..."
instance_id="$(aws ec2 run-instances --launch-template "$LAUNCH_TEMPLATE" --user-data "$user_data" | jq -r .Instances[0].InstanceId)"
echo "::set-output name=instance-id::$instance_id"
echo "Started EC2 instance: $instance_id"
echo "Waiting for repository runner to be registered ..."
for i in {1..12}; do
sleep 10
runner_id=$(gh api -X GET "repos/$GH_REPO/actions/runners" -f per_page=100 \
| jq -r --arg INSTANCE_ID "$instance_id" '.runners[] | select(.name == $INSTANCE_ID and .status == "online") | .id')
if [ -n "$runner_id" ]; then
break
fi
done
if [ -z "$runner_id" ]; then
echo "Runner registration failed (timed out). Terminating instance ..."
aws ec2 terminate-instances --instance-ids "$instance_id"
exit 1
else
echo "::set-output name=runner-id::$runner_id"
echo "Repository runner started (ID: $runner_id)"
fi
env:
GH_TOKEN: ${{ inputs.github-token }}
AWS_DEFAULT_REGION: ${{ inputs.aws-region }}
LAUNCH_TEMPLATE: ${{ inputs.aws-launch-template }}
GH_REPO: ${{ inputs.github-repo }}
RUNNER_HOME: ${{ inputs.runner-home }}
RUNNER_USER: ${{ inputs.runner-user }}
RUNNER_LABELS: ${{ inputs.runner-labels }}
SHUTDOWN_AT: ${{ inputs.auto-shutdown-at }}
EPHEMERAL: ${{ inputs.ephemeral }}