-
Notifications
You must be signed in to change notification settings - Fork 4
/
action.sh
executable file
·279 lines (235 loc) · 6.89 KB
/
action.sh
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#!/usr/bin/env bash
set -e
ACTION_DIR="$(cd $(dirname "${BASH_SOURCE[0]}") >/dev/null 2>&1 && pwd)"
function usage {
echo "Usage: ${0} --command=[start|stop] <arguments>"
}
function safety_on {
set -o errexit -o pipefail -o noclobber -o nounset
}
function safety_off {
set +o errexit +o pipefail +o noclobber +o nounset
}
source "${ACTION_DIR}/vendor/getopts_long.sh"
command=
runner_token=
project_id=
machine_zone=
machine_type=
boot_disk_type=
disk_size=
image_project=
image=
image_family=
label=
scopes=
shutdown_timeout=
task=
OPTLIND=1
while getopts_long :h opt \
command required_argument \
runner_token required_argument \
project_id required_argument \
machine_zone required_argument \
machine_type required_argument \
boot_disk_type optional_argument \
disk_size optional_argument \
image_project optional_argument \
image optional_argument \
image_family optional_argument \
label optional_argument \
scopes required_argument \
shutdown_timeout required_argument \
task required_argument \
help no_argument "" "$@"; do
case "$opt" in
command)
command=$OPTLARG
;;
runner_token)
runner_token=$OPTLARG
;;
project_id)
project_id=$OPTLARG
;;
machine_zone)
machine_zone=$OPTLARG
;;
machine_type)
machine_type=$OPTLARG
;;
boot_disk_type)
boot_disk_type=${OPTLARG-$boot_disk_type}
;;
disk_size)
disk_size=${OPTLARG-$disk_size}
;;
image_project)
image_project=${OPTLARG-$image_project}
;;
image)
image=${OPTLARG-$image}
;;
image_family)
image_family=${OPTLARG-$image_family}
;;
label)
label=${OPTLARG-$label}
;;
scopes)
scopes=$OPTLARG
;;
shutdown_timeout)
shutdown_timeout=$OPTLARG
;;
task)
task=$OPTLARG
;;
h | help)
usage
exit 0
;;
:)
printf >&2 '%s: %s\n' "${0##*/}" "$OPTLERR"
usage
exit 1
;;
esac
done
function start_vm {
VM_ID="runner-$(echo ${GITHUB_RUN_ID}-${GITHUB_RUN_NUMBER}-${task} | sha1sum | cut -f 1 -d " ")"
if [ ! -z "${label}" ]; then
echo "Label provided, using it as VM ID (${label})"
VM_ID="${label}"
fi
if [ ! -z "$(gcloud compute instances list | grep "${VM_ID}")" ]; then
# the VM already exists.
# this can happen when we call the action from a reusable workflow.
# in these scenarios we don't want a new VM ;)
echo "Skipping creation of new VM. Using the existing one (${VM_ID})"
echo "label=${VM_ID}" >>"${GITHUB_OUTPUT}"
echo "machine-zone=${machine_zone}" >>"${GITHUB_OUTPUT}"
exit 0
fi
echo "Starting GCE VM ..."
if [ -z "$runner_token" ]; then
echo "❌ runner_token parameter is required"
exit 1
fi
RUNNER_TOKEN=$(curl -S -s -XPOST \
-H "Authorization: Bearer $runner_token" \
"https://api.github.com/repos/$GITHUB_REPOSITORY/actions/runners/registration-token" |
jq -r .token)
if [ -z "$RUNNER_TOKEN" ]; then
echo "❌ Failed to get a registration token"
exit 1
fi
echo "✅ Successfully got the GitHub Runner registration token"
image_project_flag=$([[ -z "${image_project}" ]] || echo "--image-project=${image_project}")
image_flag=$([[ -z "${image}" ]] || echo "--image=${image}")
image_family_flag=$([[ -z "${image_family}" ]] || echo "--image-family=${image_family}")
disk_size_flag=$([[ -z "${disk_size}" ]] || echo "--boot-disk-size=${disk_size}")
boot_disk_type_flag=$([[ -z "${boot_disk_type}" ]] || echo "--boot-disk-type=${boot_disk_type}")
project_id_flag=$(echo "--project=${project_id}")
echo "The new GCE VM will be ${VM_ID}"
RUNNER_ID="${VM_ID}-$(date +%s)"
cat <<FILE_EOF >/tmp/startup-script.sh
#!/bin/bash
set -e
# leeway temporal directories
chmod 777 /var/tmp
chmod 777 -R /var/tmp
cleanup() {
echo "Removing runner..."
REMOVE_TOKEN=\$(curl \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${RUNNER_TOKEN}" \
https://api.github.com/repos/${GITHUB_REPOSITORY}/actions/runners/remove-token | jq .token --raw-output)
./config.sh remove --token \${REMOVE_TOKEN}
}
trap 'cleanup; exit 130' INT
trap 'cleanup; exit 143' TERM
on_error() {
echo "Error on line \$(caller)"
}
trap on_error ERR
cat <<-EOF >/etc/environment
PATH="/home/runner/go-packages/bin:/home/runner/go/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin"
GOPATH="/home/runner/go-packages"
GOROOT="/home/runner/go"
EOF
# Create a systemd service in charge of shutting down the machine once the workflow has finished
cat <<-EOF >/etc/systemd/system/shutdown.sh
#!/bin/sh
sleep "${shutdown_timeout}"
gcloud compute instances delete "${VM_ID}" --zone="${machine_zone}" --quiet
EOF
chmod +x /etc/systemd/system/shutdown.sh
echo "Registering runners ${RUNNER_ID}-1 and ${RUNNER_ID}-2..."
su -s /bin/bash -c "cd /actions-runner-1/;/actions-runner-1/config.sh --url https://github.com/${GITHUB_REPOSITORY} --token ${RUNNER_TOKEN} --name ${RUNNER_ID}-1 --labels ${VM_ID} --unattended --disableupdate" runner
su -s /bin/bash -c "cd /actions-runner-2/;/actions-runner-2/config.sh --url https://github.com/${GITHUB_REPOSITORY} --token ${RUNNER_TOKEN} --name ${RUNNER_ID}-2 --labels ${VM_ID} --unattended --disableupdate" runner
touch /.github-runner-config-ready
gcloud compute instances add-labels "${VM_ID}" --zone="${machine_zone}" --labels=gh_ready=1
echo "Setup complete."
FILE_EOF
cat <<FILE_EOF >/tmp/shutdown-script.sh
#!/bin/bash
set -e
on_error() {
echo "Error on line \$(caller)"
}
trap on_error ERR
echo "Removing runners ${RUNNER_ID}-1 and ${RUNNER_ID}-2..."
cd /actions-runner-1/
su -s /bin/bash -c "/actions-runner-1/config.sh remove --token ${RUNNER_TOKEN}" runner
cd /actions-runner-2/
su -s /bin/bash -c "/actions-runner-2/config.sh remove --token ${RUNNER_TOKEN}" runner
echo "Removed runners"
FILE_EOF
chmod +x /tmp/startup-script.sh
chmod +x /tmp/shutdown-script.sh
gcloud compute instances create "${VM_ID}" \
${project_id_flag} \
--zone="${machine_zone}" \
--labels="gh_ready=0" \
${disk_size_flag} \
${boot_disk_type_flag} \
--machine-type="${machine_type}" \
--scopes="${scopes}" \
${image_project_flag} \
${image_flag} \
${image_family_flag} \
--maintenance-policy="TERMINATE" \
--metadata-from-file="startup-script=/tmp/startup-script.sh,shutdown-script=/tmp/shutdown-script.sh" &&
echo "label=${VM_ID}" >>"${GITHUB_OUTPUT}"
echo "machine-zone=${machine_zone}" >>"${GITHUB_OUTPUT}"
safety_off
set +x
while ((i++ < 60)); do
GH_READY=$(gcloud compute instances describe "${VM_ID}" --zone="${machine_zone}" --format='json(labels)' | jq -r .labels.gh_ready)
if [[ $GH_READY == 1 ]]; then
break
fi
echo "${VM_ID} not ready yet, waiting 5s ..."
sleep 5
done
if [[ $GH_READY == 1 ]]; then
echo "✅ ${VM_ID} ready ..."
else
echo "❌ Waited 5 minutes for ${VM_ID}, without luck, deleting ${VM_ID} ..."
gcloud --quiet compute instances delete "${VM_ID}" --zone="${machine_zone}"
exit 1
fi
}
safety_on
case "$command" in
start)
start_vm
;;
*)
echo "Invalid command: \`${command}\`, valid values: start" >&2
usage
exit 1
;;
esac