@@ -19,50 +19,45 @@ function safety_off {
19
19
source " ${ACTION_DIR} /vendor/getopts_long.sh"
20
20
21
21
command=
22
- token =
22
+ runner_token =
23
23
project_id=
24
- runner_ver=
25
24
machine_zone=
26
25
machine_type=
27
26
boot_disk_type=
28
27
disk_size=
29
- runner_service_account=
30
28
image_project=
31
29
image=
32
30
image_family=
33
31
scopes=
34
32
shutdown_timeout=
33
+ task=
35
34
36
35
OPTLIND=1
37
36
while getopts_long :h opt \
38
37
command required_argument \
39
- token required_argument \
38
+ runner_token required_argument \
40
39
project_id required_argument \
41
- runner_ver required_argument \
42
40
machine_zone required_argument \
43
41
machine_type required_argument \
44
42
boot_disk_type optional_argument \
45
43
disk_size optional_argument \
46
- runner_service_account optional_argument \
47
44
image_project optional_argument \
48
45
image optional_argument \
49
46
image_family optional_argument \
50
47
scopes required_argument \
51
48
shutdown_timeout required_argument \
49
+ task required_argument \
52
50
help no_argument " " " $@ " ; do
53
51
case " $opt " in
54
52
command)
55
53
command=$OPTLARG
56
54
;;
57
- token )
58
- token =$OPTLARG
55
+ runner_token )
56
+ runner_token =$OPTLARG
59
57
;;
60
58
project_id)
61
59
project_id=$OPTLARG
62
60
;;
63
- runner_ver)
64
- runner_ver=$OPTLARG
65
- ;;
66
61
machine_zone)
67
62
machine_zone=$OPTLARG
68
63
;;
@@ -75,9 +70,6 @@ while getopts_long :h opt \
75
70
disk_size)
76
71
disk_size=${OPTLARG-$disk_size }
77
72
;;
78
- runner_service_account)
79
- runner_service_account=${OPTLARG-$runner_service_account }
80
- ;;
81
73
image_project)
82
74
image_project=${OPTLARG-$image_project }
83
75
;;
@@ -93,6 +85,9 @@ while getopts_long :h opt \
93
85
shutdown_timeout)
94
86
shutdown_timeout=$OPTLARG
95
87
;;
88
+ task)
89
+ task=$OPTLARG
90
+ ;;
96
91
h | help)
97
92
usage
98
93
exit 0
@@ -106,21 +101,33 @@ while getopts_long :h opt \
106
101
done
107
102
108
103
function start_vm {
109
- VM_ID=" gce-gh- runner-${GITHUB_RUN_ID} -${GITHUB_RUN_ATTEMPT} "
104
+ VM_ID=" runner-$( echo $ {GITHUB_RUN_ID} -${GITHUB_RUN_NUMBER} - ${task} | sha1sum | cut -f 1 -d " " ) "
110
105
111
106
if [ ! -z " $( gcloud compute instances list | grep " ${VM_ID} " ) " ]; then
112
107
# the VM already exists.
113
108
# this can happen when we call the action from a reusable workflow.
114
109
# in these scenarios we don't want a new VM ;)
115
- echo " Skipping creation of new VM. Using the existing one."
110
+ echo " Skipping creation of new VM. Using the existing one (${VM_ID} )"
111
+ echo " label=${VM_ID} " >> " ${GITHUB_OUTPUT} "
112
+ echo " machine-zone=${machine_zone} " >> " ${GITHUB_OUTPUT} "
116
113
exit 0
117
114
fi
118
115
119
116
echo " Starting GCE VM ..."
117
+ if [ -z " $runner_token " ]; then
118
+ echo " ❌ runner_token parameter is required"
119
+ exit 1
120
+ fi
121
+
120
122
RUNNER_TOKEN=$( curl -S -s -XPOST \
121
- -H " authorization : Bearer ${token} " \
122
- " https://api.github.com/repos/${ GITHUB_REPOSITORY} /actions/runners/registration-token" |
123
+ -H " Authorization : Bearer $runner_token " \
124
+ " https://api.github.com/repos/$GITHUB_REPOSITORY /actions/runners/registration-token" |
123
125
jq -r .token)
126
+ if [ -z " $RUNNER_TOKEN " ]; then
127
+ echo " ❌ Failed to get a registration token"
128
+ exit 1
129
+ fi
130
+
124
131
echo " ✅ Successfully got the GitHub Runner registration token"
125
132
126
133
image_project_flag=$( [[ -z " ${image_project} " ]] || echo " --image-project=${image_project} " )
@@ -132,11 +139,12 @@ function start_vm {
132
139
133
140
echo " The new GCE VM will be ${VM_ID} "
134
141
142
+ RUNNER_ID=" ${VM_ID} -$( date +%s) "
143
+
135
144
cat << FILE_EOF >/tmp/startup-script.sh
136
145
#!/bin/bash
137
146
138
147
set -e
139
- set -x
140
148
141
149
# leeway temporal directories
142
150
chmod 777 /var/tmp
@@ -156,6 +164,12 @@ cleanup() {
156
164
trap 'cleanup; exit 130' INT
157
165
trap 'cleanup; exit 143' TERM
158
166
167
+ cat <<-EOF >/etc/environment
168
+ 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"
169
+ GOPATH="/home/runner/go-packages"
170
+ GOROOT="/home/runner/go"
171
+ EOF
172
+
159
173
# Create a systemd service in charge of shutting down the machine once the workflow has finished
160
174
cat <<-EOF >/etc/systemd/system/shutdown.sh
161
175
#!/bin/sh
165
179
166
180
chmod +x /etc/systemd/system/shutdown.sh
167
181
168
- RUNNER_ID=${VM_ID} -$( date +%s)
169
182
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
170
183
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
171
184
@@ -180,7 +193,6 @@ FILE_EOF
180
193
#!/bin/bash
181
194
182
195
set -e
183
- set -x
184
196
185
197
pushd /actions-runner || exit 0
186
198
@@ -191,7 +203,7 @@ REMOVE_TOKEN=\$(curl \
191
203
-H "Authorization: Bearer ${RUNNER_TOKEN} " \
192
204
https://api.github.com/repos/${GITHUB_REPOSITORY} /actions/runners/remove-token | jq .token --raw-output)
193
205
if [ -z "\$ REMOVE_TOKEN" ]; then
194
- echo "Failed to get a removal token"
206
+ echo "❌ Failed to get a removal token"
195
207
exit 0
196
208
fi
197
209
@@ -217,8 +229,10 @@ FILE_EOF
217
229
--maintenance-policy=" TERMINATE" \
218
230
--metadata-from-file=" startup-script=/tmp/startup-script.sh,shutdown-script=/tmp/shutdown-script.sh" &&
219
231
echo " label=${VM_ID} " >> " ${GITHUB_OUTPUT} "
232
+ echo " machine-zone=${machine_zone} " >> " ${GITHUB_OUTPUT} "
220
233
221
234
safety_off
235
+ set +x
222
236
while (( i++ < 60 )) ; do
223
237
GH_READY=$( gcloud compute instances describe " ${VM_ID} " --zone=" ${machine_zone} " --format=' json(labels)' | jq -r .labels.gh_ready)
224
238
if [[ $GH_READY == 1 ]]; then
@@ -230,7 +244,7 @@ FILE_EOF
230
244
if [[ $GH_READY == 1 ]]; then
231
245
echo " ✅ ${VM_ID} ready ..."
232
246
else
233
- echo " Waited 5 minutes for ${VM_ID} , without luck, deleting ${VM_ID} ..."
247
+ echo " ❌ Waited 5 minutes for ${VM_ID} , without luck, deleting ${VM_ID} ..."
234
248
gcloud --quiet compute instances delete " ${VM_ID} " --zone=" ${machine_zone} "
235
249
exit 1
236
250
fi
0 commit comments