Skip to content

Commit

Permalink
Use auto shutdown hook instead of explicit shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
ravwojdyla committed Jun 12, 2023
1 parent bc46351 commit 7f3b82a
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 65 deletions.
8 changes: 2 additions & 6 deletions .github/workflows/manual_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ on:
description: Service account of the VM, defaults to default compute service account.
required: false
shutdown_timeout:
description: "Grace period for the `stop` command, in seconds."
description: "Shutdown grace period (in seconds)."
default: 30
required: true
no_external_address:
Expand Down Expand Up @@ -50,14 +50,10 @@ jobs:
image_family: ubuntu-2004-lts
no_external_address: ${{ inputs.no_external_address }}
actions_preinstalled: ${{ inputs.actions_preinstalled }}
shutdown_timeout: ${{ inputs.shutdown_timeout }}

test:
needs: create-runner
runs-on: ${{ needs.create-runner.outputs.label }}
steps:
- run: echo "This runs on the GCE runner VM"
- uses: related-sciences/gce-github-runner@main
with:
command: stop
shutdown_timeout: ${{ inputs.shutdown_timeout }}
if: always()
4 changes: 0 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,3 @@ jobs:
runs-on: ${{ needs.create-runner.outputs.label }}
steps:
- run: echo "This runs on the GCE runner VM"
- uses: related-sciences/gce-github-runner@main
with:
command: stop
if: always()
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,11 @@ jobs:
runs-on: ${{ needs.create-runner.outputs.label }}
steps:
- run: echo "This runs on the GCE VM"
- uses: related-sciences/gce-github-runner@v0.7
with:
command: stop
if: always()
```

* `create-runner` creates the GCE VM and registers the runner with unique label
* `test` uses the runner, and destroys it as the last step
* `test` uses the runner
* the runner VM will be automatically shut down after the workflow via [self-hosted runner hook](https://docs.github.com/en/actions/hosting-your-own-runners/managing-self-hosted-runners/running-scripts-before-or-after-a-job)

## Inputs

Expand Down
80 changes: 37 additions & 43 deletions action.sh
Original file line number Diff line number Diff line change
Expand Up @@ -193,32 +193,43 @@ function start_vm {
echo "The new GCE VM will be ${VM_ID}"

startup_script="
# Create a systemd service in charge of shutting down the machine once the workflow has finished
cat > /etc/systemd/system/shutdown.sh << EOF
#!/bin/sh
sleep ${shutdown_timeout}
gcloud compute instances delete $VM_ID --zone=$machine_zone --quiet
EOF
cat > /etc/systemd/system/shutdown.service << EOF1
[Unit]
Description=Shutdown service
[Service]
ExecStart=/etc/systemd/system/shutdown.sh
[Install]
WantedBy=multi-user.target
EOF1
chmod +x /etc/systemd/system/shutdown.sh
systemctl daemon-reload
systemctl enable shutdown.service
# 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
gcloud compute instances add-labels ${VM_ID} --zone=${machine_zone} --labels=gh_ready=0 && \\
RUNNER_ALLOW_RUNASROOT=1 ./config.sh --url https://github.com/${GITHUB_REPOSITORY} --token ${RUNNER_TOKEN} --labels ${VM_ID} --unattended ${ephemeral_flag} --disableupdate && \\
./svc.sh install && \\
./svc.sh start && \\
gcloud compute instances add-labels ${VM_ID} --zone=${machine_zone} --labels=gh_ready=1
# 3 days represents the max workflow runtime. This will shutdown the instance if everything else fails.
nohup sh -c \"sleep 3d && gcloud --quiet compute instances delete ${VM_ID} --zone=${machine_zone}\" > /dev/null &
"
cat <<-EOF > /etc/systemd/system/shutdown.service
[Unit]
Description=Shutdown service
[Service]
ExecStart=/etc/systemd/system/shutdown.sh
[Install]
WantedBy=multi-user.target
EOF
chmod +x /etc/systemd/system/shutdown.sh
systemctl daemon-reload
systemctl enable shutdown.service
cat <<-EOF > /usr/bin/gce_runner_shutdown.sh
#!/bin/sh
echo \"✅ Self deleting $VM_ID in ${machine_zone} in ${shutdown_timeout} seconds ...\"
# We tear down the machine by starting the systemd service that was registered by the startup script
systemctl start shutdown.service
EOF
# See: https://docs.github.com/en/actions/hosting-your-own-runners/managing-self-hosted-runners/running-scripts-before-or-after-a-job
echo "ACTIONS_RUNNER_HOOK_JOB_COMPLETED=/usr/bin/gce_runner_shutdown.sh" >.env
gcloud compute instances add-labels ${VM_ID} --zone=${machine_zone} --labels=gh_ready=0 && \\
RUNNER_ALLOW_RUNASROOT=1 ./config.sh --url https://github.com/${GITHUB_REPOSITORY} --token ${RUNNER_TOKEN} --labels ${VM_ID} --unattended ${ephemeral_flag} --disableupdate && \\
./svc.sh install && \\
./svc.sh start && \\
gcloud compute instances add-labels ${VM_ID} --zone=${machine_zone} --labels=gh_ready=1
# 3 days represents the max workflow runtime. This will shutdown the instance if everything else fails.
nohup sh -c \"sleep 3d && gcloud --quiet compute instances delete ${VM_ID} --zone=${machine_zone}\" > /dev/null &
"

if $actions_preinstalled ; then
echo "✅ Startup script won't install GitHub Actions (pre-installed)"
Expand Down Expand Up @@ -283,30 +294,13 @@ systemctl enable shutdown.service
fi
}

function stop_vm {
# NOTE: this function runs on the GCE VM
echo "Stopping GCE VM ..."
# NOTE: it would be nice to gracefully shut down the runner, but we actually don't need
# to do that. VM shutdown will disconnect the runner, and GH will unregister it
# in 30 days
# TODO: RUNNER_ALLOW_RUNASROOT=1 /actions-runner/config.sh remove --token $TOKEN
NAME=$(curl -S -s -X GET http://metadata.google.internal/computeMetadata/v1/instance/name -H 'Metadata-Flavor: Google')
ZONE=$(curl -S -s -X GET http://metadata.google.internal/computeMetadata/v1/instance/zone -H 'Metadata-Flavor: Google')
echo "✅ Self deleting $NAME in $ZONE in ${shutdown_timeout} seconds ..."
# We tear down the machine by starting the systemd service that was registered by the startup script
systemctl start shutdown.service
}

safety_on
case "$command" in
start)
start_vm
;;
stop)
stop_vm
;;
*)
echo "Invalid command: \`${command}\`, valid values: start|stop" >&2
echo "Invalid command: \`${command}\`, valid values: start" >&2
usage
exit 1
;;
Expand Down
10 changes: 3 additions & 7 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ branding:
icon: triangle
color: purple
inputs:
command:
description: "`start` or `stop` of the runner"
default: start
required: true
token:
description: >-
GitHub auth token, needs `repo`/`public_repo` scope: https://docs.github.com/en/rest/reference/actions#self-hosted-runners.
Expand All @@ -26,7 +22,7 @@ inputs:
required: false
runner_ver:
description: Version of the GitHub Runner.
default: "2.303.0"
default: "2.304.0"
required: true
machine_zone:
description: GCE zone
Expand Down Expand Up @@ -87,7 +83,7 @@ inputs:
default: cloud-platform
required: true
shutdown_timeout:
description: "Grace period for the `stop` command, in seconds."
description: "Shutdown grace period (in seconds)."
default: 30
required: true
actions_preinstalled:
Expand Down Expand Up @@ -115,7 +111,7 @@ runs:
- id: gce-github-runner-script
run: >
${{ github.action_path }}/action.sh
--command=${{ inputs.command }}
--command=start
--token=${{ inputs.token }}
--project_id=${{ inputs.project_id }}
--service_account_key='${{ inputs.service_account_key }}'
Expand Down

0 comments on commit 7f3b82a

Please sign in to comment.