Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## [0.1.7] - 2026-10-13
### Fixed
- The drain gate now actually drains. It used to only poll `busy`, so a runner on a busy fleet 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 the role now strips the runner's custom labels first (no new job can route to it), waits out the in-flight job, then replaces it; the new container registers with its configured labels. A failed drain puts the stripped labels back. Read-only labels (`self-hosted`, OS, arch) can't be removed, so a job targeting only those can still land on a draining runner.

## [0.1.6] - 2026-08-20
### Added
- `github_runner_mount_docker_socket` (default `true`): whether to mount the host's `/var/run/docker.sock` into the runner container. Set to `false` for runners whose CI jobs never use docker — it drops a root-equivalent privilege, and on a desktop host it guarantees the runner's jobs can't churn veth interfaces (each add/remove aborts in-flight Chrome requests on that host with `ERR_NETWORK_CHANGED`).
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ github_runner_persist_config | whether to persist runner configur
github_runner_mount_docker_socket | whether to mount the host's /var/run/docker.sock into the runner container; set to false for runners whose jobs never use docker to drop the root-equivalent privilege (defaults to true)
github_runner_stop_timeout | seconds docker waits after SIGTERM before SIGKILL when stopping the runner container, e.g. while replacing it on an image bump (defaults to 10)
github_runner_docker_timeout | Docker API client timeout in seconds for the deploy task; stopping and removing a crash-looping runner can exceed the SDK's 60s default (defaults to 180)
github_runner_drain_before_replace | when a deploy is about to replace the runner container while the runner is executing a CI job, wait for the job to finish instead of destroying it (defaults to true)
github_runner_drain_before_replace | when a deploy is about to replace the runner container, strip the runner's custom labels so it takes no new jobs and wait for any in-flight job to finish instead of destroying it (defaults to true). Needs a token that can manage the runner's labels
github_runner_drain_timeout_minutes | how long to wait for a busy runner to go idle before failing the deploy (defaults to 45)
github_runner_drain_poll_seconds | polling interval for the busy-check against the GitHub runners API (defaults to 30)
github_runner_force_replace | emergency override: replace the container immediately even if a job is mid-flight (defaults to false)
Expand Down
2 changes: 1 addition & 1 deletion galaxy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ description: "Installs github self hosted repo or org runners within a docker co
can be a vanilla runner, or one with java / android installed."
license_file: LICENSE
readme: README.md
version: 0.1.6
version: 0.1.7
repository: https://github.com/compscidr/ansible-github-runner
tags:
- github
Expand Down
42 changes: 38 additions & 4 deletions roles/github_runner/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,14 @@

# Drain-before-replace: replacing the container while its runner is executing
# a CI job destroys the job (GitHub waits out the runner heartbeat ~10 min,
# then fails every remaining step). When a replace is imminent and the
# existing runner is busy, poll the runners API until it goes idle. A timeout
# fails the deploy loudly — rerun with github_runner_force_replace=true to
# replace anyway (killing the in-flight job) in an emergency.
# then fails every remaining step). When a replace is imminent, strip the
# runner's custom labels so no new job routes to it, then poll the runners API
# until its in-flight job (if any) finishes. Without the strip a busy fleet
# hands the runner a fresh job each time it frees up and the drain never
# ends. The replacement registers with its configured labels; a failed drain
# (no replace) puts the stripped labels back. A timeout fails the deploy loudly —
# rerun with github_runner_force_replace=true to replace anyway (killing the
# in-flight job) in an emergency.
- name: Wait for the runner to finish its current job before replacing
tags: github_runner
check_mode: false
Expand Down Expand Up @@ -86,6 +90,32 @@
drain gate before deploying to this account.
quiet: true

# Only custom labels can be removed; the read-only ones (self-hosted, OS,
# arch) stay, so a job targeting just `self-hosted` could still land here.
- name: Record this runner's custom labels
ansible.builtin.set_fact:
github_runner_stripped_labels: >-
{{
github_runner_stripped_labels | default([]) + [{
'id': item.id,
'labels': item.labels | selectattr('type', 'equalto', 'custom') | map(attribute='name') | list
}]
}}
loop: "{{ github_runner_listing_probe.json.runners | selectattr('name', 'match', github_runner_name_match) | list }}"
loop_control:
label: "{{ item.name }}"

- name: Strip this runner's custom labels so it takes no new jobs
ansible.builtin.uri:
url: "{{ github_runner_api_runners_base }}/{{ item.id }}/labels"
method: DELETE
headers:
Authorization: "Bearer {{ github_runner_personal_access_token }}"
Accept: "application/vnd.github+json"
loop: "{{ github_runner_stripped_labels | default([]) | selectattr('labels') | list }}"
# The Authorization header carries the PAT — keep it out of logs.
no_log: true

- name: Poll the runners API until this runner is idle
ansible.builtin.uri:
url: "{{ github_runner_api_runners_url }}"
Expand All @@ -109,6 +139,10 @@
| selectattr('busy')
| list | length == 0)
rescue:
# Best effort: the deploy is failing anyway.
- name: Restore the stripped labels so the runner takes jobs again
ansible.builtin.include_tasks: restore_labels.yml

- name: Fail with drain guidance
ansible.builtin.fail:
msg: >-
Expand Down
19 changes: 19 additions & 0 deletions roles/github_runner/tasks/restore_labels.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Puts back the custom labels the drain gate stripped (main.yml) when the
# drain fails and the container is left in place. A 404 means the runner is
# already gone, which is fine.
- name: Restore the runner's custom labels
tags: github_runner
check_mode: false
ansible.builtin.uri:
url: "{{ github_runner_api_runners_base }}/{{ item.id }}/labels"
method: PUT
headers:
Authorization: "Bearer {{ github_runner_personal_access_token }}"
Accept: "application/vnd.github+json"
body_format: json
body:
labels: "{{ item.labels }}"
status_code: [200, 404]
loop: "{{ github_runner_stripped_labels | default([]) | selectattr('labels') | list }}"
# The Authorization header carries the PAT — keep it out of logs.
no_log: true
5 changes: 3 additions & 2 deletions roles/github_runner/vars/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,13 @@ github_runner_env: >-
# depending on scope. The runners register as "<github_runner_name>-<random>"
# (RUNNER_NAME_PREFIX), so the busy-check matches that prefix — and a bare
# "<github_runner_name>" too, in case a future image drops the suffix.
github_runner_api_runners_url: >-
github_runner_api_runners_base: >-
{{
github_runner_api_base ~ '/' ~
('orgs/' ~ github_runner_org_name if github_runner_org else 'repos/' ~ github_runner_repo) ~
'/actions/runners?per_page=100'
'/actions/runners'
}}
github_runner_api_runners_url: "{{ github_runner_api_runners_base }}?per_page=100"
github_runner_name_match: "^{{ github_runner_name | regex_escape }}(-|$)"

# Poll interval clamped to >=1s so a consumer setting 0 (or a value that casts
Expand Down
Loading