Skip to content

Commit 692b6f4

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. This is too complicated, unfortunately, in a GitHub workflow (at least without the risk of using non-official GitHub Actions), therefore we use the newly-added shell script to authenticate the GitHub CLI as a GitHub App (which of course now requires us to check out the repository). Signed-off-by: Johannes Schindelin <[email protected]>
1 parent cb7c524 commit 692b6f4

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,16 @@ jobs:
1818
delete-runner:
1919
runs-on: ubuntu-latest
2020
steps:
21+
- uses: actions/checkout@v4
2122
- name: Azure Login
2223
uses: azure/login@v1
2324
with:
2425
creds: ${{ secrets.AZURE_CREDENTIALS }}
2526
- name: Discover VMs to delete
2627
uses: azure/CLI@v1
28+
env:
29+
GH_APP_ID: ${{ secrets.GH_APP_ID }}
30+
GH_APP_PRIVATE_KEY: ${{ secrets.GH_APP_PRIVATE_KEY }}
2731
with:
2832
azcliversion: 2.64.0
2933
inlineScript: |
@@ -46,6 +50,13 @@ jobs:
4650
4751
if [ "$three_hours_ago" -lt "$vm_creation_time" ]; then
4852
echo "The VM ${vm_name} was created less then 3 hours ago and shouldn't be deleted yet. Skipping."
53+
elif test true = "$(if test ! -f .cli-authenticated; then
54+
./gh-cli-auth-as-app.sh &&
55+
>.cli-authenticated # only authenticate once
56+
fi &&
57+
gh api repos/$GITHUB_REPOSITORY/actions/runners \
58+
--jq '.runners[] | select(.name == "'$vm_name'") | .busy)"; then
59+
echo "The VM ${vm_name} is still busy."
4960
else
5061
echo "The VM ${vm_name} was created more than 3 hours ago and wasn't deleted. Let's do that now."
5162
az vm delete -n "$vm_name" -g ${{ secrets.AZURE_RESOURCE_GROUP }} --yes

0 commit comments

Comments
 (0)