Skip to content

Commit b7ae6e7

Browse files
jderravwojdyla
authored andcommitted
Shutdown the runner VM if startup fails
1 parent 3151034 commit b7ae6e7

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

action.sh

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -202,12 +202,22 @@ function start_vm {
202202

203203
echo "The new GCE VM will be ${VM_ID}"
204204

205+
shutdown_command="gcloud compute instances delete $VM_ID --zone=$machine_zone --quiet"
206+
startup_prelude="#!/bin/bash
207+
set -e
208+
shutdown() {
209+
echo ❌ Machine setup failed so deleting $VM_ID in ${machine_zone} ...
210+
${shutdown_command}
211+
}
212+
trap shutdown EXIT
213+
"
214+
205215
startup_script="
206216
# Create a systemd service in charge of shutting down the machine once the workflow has finished
207217
cat <<-EOF > /etc/systemd/system/shutdown.sh
208218
#!/bin/sh
209219
sleep ${shutdown_timeout}
210-
gcloud compute instances delete $VM_ID --zone=$machine_zone --quiet
220+
${shutdown_command}
211221
EOF
212222
213223
cat <<-EOF > /etc/systemd/system/shutdown.service
@@ -238,12 +248,12 @@ function start_vm {
238248
./svc.sh start && \\
239249
gcloud compute instances add-labels ${VM_ID} --zone=${machine_zone} --labels=gh_ready=1
240250
# 3 days represents the max workflow runtime. This will shutdown the instance if everything else fails.
241-
nohup sh -c \"sleep 3d && gcloud --quiet compute instances delete ${VM_ID} --zone=${machine_zone}\" > /dev/null &
251+
nohup sh -c \"sleep 3d && ${shutdown_command}\" > /dev/null &
242252
"
243253

244254
if $actions_preinstalled ; then
245255
echo "✅ Startup script won't install GitHub Actions (pre-installed)"
246-
startup_script="#!/bin/bash
256+
startup_script="${startup_prelude}
247257
cd /actions-runner
248258
$startup_script"
249259
else
@@ -258,20 +268,20 @@ function start_vm {
258268
fi
259269
echo "✅ Startup script will install GitHub Actions v$runner_ver"
260270
if $arm ; then
261-
startup_script="#!/bin/bash
271+
startup_script="${startup_prelude}
262272
mkdir /actions-runner
263273
cd /actions-runner
264274
curl -o actions-runner-linux-arm64-${runner_ver}.tar.gz -L https://github.com/actions/runner/releases/download/v${runner_ver}/actions-runner-linux-arm64-${runner_ver}.tar.gz
265275
tar xzf ./actions-runner-linux-arm64-${runner_ver}.tar.gz
266-
./bin/installdependencies.sh && \\
276+
./bin/installdependencies.sh
267277
$startup_script"
268278
else
269-
startup_script="#!/bin/bash
279+
startup_script="${startup_prelude}
270280
mkdir /actions-runner
271281
cd /actions-runner
272282
curl -o actions-runner-linux-x64-${runner_ver}.tar.gz -L https://github.com/actions/runner/releases/download/v${runner_ver}/actions-runner-linux-x64-${runner_ver}.tar.gz
273283
tar xzf ./actions-runner-linux-x64-${runner_ver}.tar.gz
274-
./bin/installdependencies.sh && \\
284+
./bin/installdependencies.sh
275285
$startup_script"
276286
fi
277287
fi

0 commit comments

Comments
 (0)