|
45 | 45 |
|
46 | 46 | # Drain-before-replace: replacing the container while its runner is executing |
47 | 47 | # 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. |
52 | 56 | - name: Wait for the runner to finish its current job before replacing |
53 | 57 | tags: github_runner |
54 | 58 | check_mode: false |
|
86 | 90 | drain gate before deploying to this account. |
87 | 91 | quiet: true |
88 | 92 |
|
| 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 | + |
89 | 119 | - name: Poll the runners API until this runner is idle |
90 | 120 | ansible.builtin.uri: |
91 | 121 | url: "{{ github_runner_api_runners_url }}" |
|
109 | 139 | | selectattr('busy') |
110 | 140 | | list | length == 0) |
111 | 141 | 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 | + |
112 | 146 | - name: Fail with drain guidance |
113 | 147 | ansible.builtin.fail: |
114 | 148 | msg: >- |
|
0 commit comments