Skip to content

Commit f355727

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 d2e6833 commit f355727

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@v2
2324
with:
2425
creds: ${{ secrets.AZURE_CREDENTIALS }}
2526
- name: Discover VMs to delete
2627
uses: azure/CLI@v2
28+
env:
29+
GH_APP_ID: ${{ secrets.GH_APP_ID }}
30+
GH_APP_PRIVATE_KEY: ${{ secrets.GH_APP_PRIVATE_KEY }}
2731
with:
2832
# Stick to 2.63.0 until jq is added to 2.64.0+ https://github.com/Azure/azure-cli/issues/29830
2933
azcliversion: 2.63.0
@@ -49,6 +53,13 @@ jobs:
4953
5054
if [ "$three_hours_ago" -lt "$vm_creation_time" ]; then
5155
echo "The VM ${vm_name} was created less then 3 hours ago and shouldn't be deleted yet. Skipping."
56+
elif test true = "$(if test ! -f .cli-authenticated; then
57+
./gh-cli-auth-as-app.sh &&
58+
>.cli-authenticated # only authenticate once
59+
fi &&
60+
gh api repos/$GITHUB_REPOSITORY/actions/runners \
61+
--jq '.runners[] | select(.name == "'$vm_name'") | .busy')"; then
62+
echo "The VM ${vm_name} is still busy."
5263
else
5364
echo "The VM ${vm_name} was created more than 3 hours ago and wasn't deleted. Let's do that now."
5465
az vm delete -n "$vm_name" -g ${{ secrets.AZURE_RESOURCE_GROUP }} --yes

0 commit comments

Comments
 (0)