From 7f3b82a4c91927175e83b549a738574307d5af30 Mon Sep 17 00:00:00 2001 From: Rafal Wojdyla Date: Sun, 11 Jun 2023 03:06:03 +0100 Subject: [PATCH] Use auto shutdown hook instead of explicit shutdown --- .github/workflows/manual_test.yml | 8 +--- .github/workflows/test.yml | 4 -- README.md | 7 +-- action.sh | 80 ++++++++++++++----------------- action.yml | 10 ++-- 5 files changed, 44 insertions(+), 65 deletions(-) diff --git a/.github/workflows/manual_test.yml b/.github/workflows/manual_test.yml index 8838b73..7479e79 100644 --- a/.github/workflows/manual_test.yml +++ b/.github/workflows/manual_test.yml @@ -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: @@ -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() diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 2763871..bb62ce5 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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() diff --git a/README.md b/README.md index 70d8dac..7c9ab78 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/action.sh b/action.sh index 0ae5d3e..3614f88 100755 --- a/action.sh +++ b/action.sh @@ -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)" @@ -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 ;; diff --git a/action.yml b/action.yml index 4d41db4..049f55c 100644 --- a/action.yml +++ b/action.yml @@ -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. @@ -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 @@ -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: @@ -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 }}'