Skip to content

Commit 9645cc1

Browse files
committed
cleanup-self-hosted-runner: avoid deleting VMs that are busy
So far, we avoid deleting VMs when they are too young. But we can do better than that: since the VMs are intended to host an ephemeral runner, and that runner is registered with the current repository, we can have a look whether GitHub says that the runner is busy. If it is, well, let's leave it a-running! Since this query requires a larger scope than the standard GitHub workflows' `GITHUB_TOKEN` provides, we need to authenticate as the GitForWindowsHelper GitHub App to do that. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 22d865b commit 9645cc1

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

.github/workflows/cleanup-self-hosted-runners.yml

+10
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ jobs:
2424
creds: ${{ secrets.AZURE_CREDENTIALS }}
2525
- name: Discover VMs to delete
2626
uses: azure/CLI@v1
27+
env:
28+
GH_APP_ID: ${{ secrets.GH_APP_ID }}
29+
GH_APP_PRIVATE_KEY: ${{ secrets.GH_APP_PRIVATE_KEY }}
2730
with:
2831
azcliversion: 2.54.0
2932
inlineScript: |
@@ -46,6 +49,13 @@ jobs:
4649
4750
if [ "$three_hours_ago" -lt "$vm_creation_time" ]; then
4851
echo "The VM ${vm_name} was created less then 3 hours ago and shouldn't be deleted yet. Skipping."
52+
elif test true = "$(if test ! -f .cli-authenticated; then
53+
./gh-cli-auth-as-app.sh &&
54+
>.cli-authenticated # only authenticate once
55+
fi &&
56+
gh api repos/$GITHUB_REPOSITORY/actions/runners \
57+
--jq '.runners[] | select(.name == "'$vm_name'") | .busy)"; then
58+
echo "The VM ${vm_name} is still busy."
4959
else
5060
echo "The VM ${vm_name} was created more than 3 hours ago and wasn't deleted. Let's do that now."
5161
az vm delete -n "$vm_name" -g ${{ secrets.AZURE_RESOURCE_GROUP }} --yes

0 commit comments

Comments
 (0)