Skip to content

Drain gate: strip the runner's custom labels so it takes no new jobs - #59

Merged
compscidr merged 3 commits into
mainfrom
drain-stops-new-jobs
Oct 13, 2026
Merged

compscidr merged 3 commits into
mainfrom
drain-stops-new-jobs

Conversation

@compscidr

Copy link
Copy Markdown
Owner

Problem

The drain-before-replace gate (0.1.5) only polls the runner's busy flag. On a busy fleet the runner picks up the next queued job each time it finishes one, so the gate never sees it idle and the deploy fails after github_runner_drain_timeout_minutes. Seen on BumpApp/bump: an infra merge to main queued ~12 test workflows onto the same runners the deploy was draining, and silverstone's JVM runner timed out after 45 min.

Change

When a replace is imminent:

  1. Record the matching runner's custom labels from the listing the gate already fetches.
  2. DELETE /actions/runners/{id}/labels so no new job can route to it (the in-flight job is unaffected).
  3. Poll until idle, as before, then replace. The new container registers with its configured labels.
  4. If the drain fails (no replace), PUT the recorded labels back (tasks/restore_labels.yml) so the old runner takes jobs again.

Read-only labels (self-hosted, OS, arch) can't be removed, so a job targeting only those can still land on a draining runner (noted in the CHANGELOG).

Also: github_runner_api_runners_base (the URL without ?per_page=100) for the per-runner label endpoints; version 0.1.7; CHANGELOG + README.

Testing

  • ansible-lint 26.4.0: passes, production profile.
  • Not run: molecule, or a live drain against a busy runner.

The token needs to be able to manage runners (classic admin:org for org runners, which the existing listing already requires).

🤖 Generated with Claude Code

The drain only polled `busy`, so on a busy fleet the runner picked up a new
queued job each time it finished one and the deploy timed out after
github_runner_drain_timeout_minutes. When a replace is imminent, record and
strip the runner's custom labels first, then wait out the in-flight job. The
replacement registers with its configured labels; a failed drain (no
replace) puts the stripped labels back.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings October 13, 2026 16:19

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The strip-labels loop omits the default([]) guard used elsewhere, so the drain gate errors on an undefined variable when no registered runner matches the name.

1 open finding
What changed in this PR

This PR fixes the drain-before-replace gate in the github_runner Ansible role. Previously the gate only polled the runner's busy flag, so on a busy fleet the runner kept accepting newly queued jobs and never went idle, causing deploys to time out after github_runner_drain_timeout_minutes. The fix strips the runner's custom labels (via the GitHub labels API) when a replace is imminent so no new jobs route to it, waits out the in-flight job, then replaces it; a failed drain restores the stripped labels.

Changes:

  • Added Record/Strip tasks to capture and DELETE a matching runner's custom labels before polling for idle, plus a rescue step that restores them via the new tasks/restore_labels.yml (PUT).
  • Split the runners API URL into github_runner_api_runners_base (no query string) so per-runner label endpoints can be built, keeping github_runner_api_runners_url for the paginated listing.
  • Bumped version to 0.1.7 and updated README/CHANGELOG to document the new behavior and the label-management token requirement.
File Description
roles/​github_runner/​tasks/​main.yml Adds record/strip-labels tasks before the idle poll and a rescue step to restore labels on drain failure; updates the drain comment.
roles/​github_runner/​tasks/​restore_labels.yml New task file that PUTs the recorded custom labels back when the drain fails.
roles/​github_runner/​vars/​main.yml Introduces github_runner_api_runners_base and derives github_runner_api_runners_url from it.
README.md Updates the github_runner_drain_before_replace description for the label-stripping behavior and token requirement.
galaxy.yml Bumps role version to 0.1.7.
CHANGELOG.md Documents the 0.1.7 drain-gate fix.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread roles/github_runner/tasks/main.yml Resolved
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@compscidr
compscidr merged commit 2bc4848 into main Oct 13, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants