Skip to content

Commit 2bc4848

Browse files
authored
Merge pull request #59 from compscidr/drain-stops-new-jobs
2 parents bafad91 + 643921f commit 2bc4848

6 files changed

Lines changed: 66 additions & 8 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## [0.1.7] - 2026-10-13
4+
### Fixed
5+
- 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.
6+
37
## [0.1.6] - 2026-08-20
48
### Added
59
- `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`).

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ github_runner_persist_config | whether to persist runner configur
7979
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)
8080
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)
8181
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)
82-
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)
82+
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
8383
github_runner_drain_timeout_minutes | how long to wait for a busy runner to go idle before failing the deploy (defaults to 45)
8484
github_runner_drain_poll_seconds | polling interval for the busy-check against the GitHub runners API (defaults to 30)
8585
github_runner_force_replace | emergency override: replace the container immediately even if a job is mid-flight (defaults to false)

‎galaxy.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ description: "Installs github self hosted repo or org runners within a docker co
88
can be a vanilla runner, or one with java / android installed."
99
license_file: LICENSE
1010
readme: README.md
11-
version: 0.1.6
11+
version: 0.1.7
1212
repository: https://github.com/compscidr/ansible-github-runner
1313
tags:
1414
- github

‎roles/github_runner/tasks/main.yml‎

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,14 @@
4545

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

93+
# Only custom labels can be removed; the read-only ones (self-hosted, OS,
94+
# arch) stay, so a job targeting just `self-hosted` could still land here.
95+
- name: Record this runner's custom labels
96+
ansible.builtin.set_fact:
97+
github_runner_stripped_labels: >-
98+
{{
99+
github_runner_stripped_labels | default([]) + [{
100+
'id': item.id,
101+
'labels': item.labels | selectattr('type', 'equalto', 'custom') | map(attribute='name') | list
102+
}]
103+
}}
104+
loop: "{{ github_runner_listing_probe.json.runners | selectattr('name', 'match', github_runner_name_match) | list }}"
105+
loop_control:
106+
label: "{{ item.name }}"
107+
108+
- name: Strip this runner's custom labels so it takes no new jobs
109+
ansible.builtin.uri:
110+
url: "{{ github_runner_api_runners_base }}/{{ item.id }}/labels"
111+
method: DELETE
112+
headers:
113+
Authorization: "Bearer {{ github_runner_personal_access_token }}"
114+
Accept: "application/vnd.github+json"
115+
loop: "{{ github_runner_stripped_labels | default([]) | selectattr('labels') | list }}"
116+
# The Authorization header carries the PAT — keep it out of logs.
117+
no_log: true
118+
89119
- name: Poll the runners API until this runner is idle
90120
ansible.builtin.uri:
91121
url: "{{ github_runner_api_runners_url }}"
@@ -109,6 +139,10 @@
109139
| selectattr('busy')
110140
| list | length == 0)
111141
rescue:
142+
# Best effort: the deploy is failing anyway.
143+
- name: Restore the stripped labels so the runner takes jobs again
144+
ansible.builtin.include_tasks: restore_labels.yml
145+
112146
- name: Fail with drain guidance
113147
ansible.builtin.fail:
114148
msg: >-
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Puts back the custom labels the drain gate stripped (main.yml) when the
2+
# drain fails and the container is left in place. A 404 means the runner is
3+
# already gone, which is fine.
4+
- name: Restore the runner's custom labels
5+
tags: github_runner
6+
check_mode: false
7+
ansible.builtin.uri:
8+
url: "{{ github_runner_api_runners_base }}/{{ item.id }}/labels"
9+
method: PUT
10+
headers:
11+
Authorization: "Bearer {{ github_runner_personal_access_token }}"
12+
Accept: "application/vnd.github+json"
13+
body_format: json
14+
body:
15+
labels: "{{ item.labels }}"
16+
status_code: [200, 404]
17+
loop: "{{ github_runner_stripped_labels | default([]) | selectattr('labels') | list }}"
18+
# The Authorization header carries the PAT — keep it out of logs.
19+
no_log: true

‎roles/github_runner/vars/main.yml‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,13 @@ github_runner_env: >-
4343
# depending on scope. The runners register as "<github_runner_name>-<random>"
4444
# (RUNNER_NAME_PREFIX), so the busy-check matches that prefix — and a bare
4545
# "<github_runner_name>" too, in case a future image drops the suffix.
46-
github_runner_api_runners_url: >-
46+
github_runner_api_runners_base: >-
4747
{{
4848
github_runner_api_base ~ '/' ~
4949
('orgs/' ~ github_runner_org_name if github_runner_org else 'repos/' ~ github_runner_repo) ~
50-
'/actions/runners?per_page=100'
50+
'/actions/runners'
5151
}}
52+
github_runner_api_runners_url: "{{ github_runner_api_runners_base }}?per_page=100"
5253
github_runner_name_match: "^{{ github_runner_name | regex_escape }}(-|$)"
5354

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

0 commit comments

Comments
 (0)