Repository navigation
247 lines (233 loc) · 10.9 KB
/
Copy path_build-pr-ci-image.yml
File metadata and controls
247 lines (233 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# doc-dev: docs/developer/ci/02-docker-build.md
name: PR CI Image Build
on:
workflow_call:
outputs:
built:
description: Whether this call built and pushed a PR-scoped image
value: ${{ jobs.docker-build.outputs.built }}
tag_available:
description: Whether a current PR-scoped image exists, built by this call or an earlier one
value: ${{ jobs.docker-build.outputs.tag_available }}
jobs:
# A PR keeps one image tag (pr-<number>) for its whole life. Rebuilds are driven by
# the content of the build inputs, not by whether the PR happens to touch them: the
# published tag records its inputs hash in a label, so reruns and unrelated pushes
# reuse the image instead of paying for an identical build.
docker-decide:
if: github.event.action != 'closed'
runs-on: ubuntu-latest
outputs:
needs_image: ${{ steps.decide.outputs.needs_image }}
rebuild: ${{ steps.decide.outputs.rebuild }}
force_rebuild: ${{ steps.prepare.outputs.force_rebuild }}
current: ${{ steps.compare.outputs.current }}
env:
SAME_REPO: ${{ github.event.pull_request.head.repo.full_name == github.repository }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 2
persist-credentials: false
- id: compare
shell: bash
env:
BASE_SHA: ${{ github.event.pull_request.base.sha || '' }}
run: |
set -euo pipefail
emit() {
echo "eligible=$1" >> "$GITHUB_OUTPUT"
echo "current=$2" >> "$GITHUB_OUTPUT"
}
# Cron and manual runs have no PR diff and never own a PR image.
if [ -z "$BASE_SHA" ]; then
emit false ""
exit 0
fi
# Compare the PR merge commit with its current base (HEAD^1) rather than the
# recorded base.sha, which can lag main and pull in unrelated changes.
CURRENT=$(python3 docker/image_inputs.py --rev HEAD)
BASE=$(python3 docker/image_inputs.py --rev HEAD^1)
if [ "$CURRENT" = "$BASE" ]; then
echo "Build inputs match the base branch; suites use the released image."
emit false ""
exit 0
fi
emit true "$CURRENT"
- id: prepare
shell: bash
env:
ELIGIBLE: ${{ steps.compare.outputs.eligible }}
CURRENT: ${{ steps.compare.outputs.current }}
PR_NUMBER: ${{ github.event.pull_request.number || '' }}
GH_TOKEN: ${{ github.token }}
run: |
set -euo pipefail
emit() {
echo "inspect_tag=$1" >> "$GITHUB_OUTPUT"
echo "force_rebuild=$2" >> "$GITHUB_OUTPUT"
echo "current=$3" >> "$GITHUB_OUTPUT"
}
if [ "$ELIGIBLE" != "true" ]; then
emit false false ""
exit 0
fi
LABELS=$(gh api --paginate "repos/${GITHUB_REPOSITORY}/issues/${PR_NUMBER}/labels" --jq '.[].name')
if grep -Fxq "rebuild-ci-image" <<< "$LABELS"; then
emit false true "$CURRENT"
else
emit true false "$CURRENT"
fi
# Registry auth is needed only when reading the inputs label from an existing tag.
- name: Login to Docker Hub
if: steps.prepare.outputs.inspect_tag == 'true' && env.SAME_REPO == 'true'
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- id: decide
shell: bash
env:
CURRENT: ${{ steps.prepare.outputs.current }}
FORCE_REBUILD: ${{ steps.prepare.outputs.force_rebuild }}
INSPECT_TAG: ${{ steps.prepare.outputs.inspect_tag }}
PR_NUMBER: ${{ github.event.pull_request.number || '' }}
run: |
set -euo pipefail
emit() { echo "needs_image=$1" >> "$GITHUB_OUTPUT"; echo "rebuild=$2" >> "$GITHUB_OUTPUT"; }
if [ "$FORCE_REBUILD" = "true" ]; then
echo "::notice::rebuild-ci-image label present; forcing a rebuild."
emit true true
exit 0
fi
if [ "$INSPECT_TAG" != "true" ]; then
emit false false
exit 0
fi
PUBLISHED=$(docker buildx imagetools inspect "radixark/miles:pr-${PR_NUMBER}" \
--format '{{ json .Image }}' 2>/dev/null \
| python3 docker/image_inputs.py --read-label || true)
if [ -z "$PUBLISHED" ]; then
echo "No usable pr-${PR_NUMBER} image; building one."
emit true true
elif [ "$PUBLISHED" = "$CURRENT" ]; then
echo "pr-${PR_NUMBER} already matches the current build inputs; reusing it."
emit true false
else
echo "Build inputs changed since pr-${PR_NUMBER} was published; rebuilding."
emit true true
fi
# Docker PRs build the multi-arch image after the caller's CPU gate passes.
# Without a rebuild to do this stays on a free hosted runner as a no-op;
# actual builds use docker-build nodes, never the GPU-suite pool.
docker-build:
needs: [docker-decide]
if: always() && !cancelled() && github.event.action != 'closed'
runs-on: ${{ fromJSON(needs.docker-decide.outputs.rebuild == 'true' && github.event.pull_request.head.repo.full_name == github.repository && '["docker-build"]' || '["ubuntu-latest"]') }}
# The fork job waits for a separately queued builder, whose own limit is 180m.
timeout-minutes: ${{ needs.docker-decide.outputs.rebuild == 'true' && github.event.pull_request.head.repo.full_name != github.repository && 360 || 180 }}
outputs:
built: ${{ steps.build.outputs.built || steps.wait.outputs.built || 'false' }}
# Reusing an image the previous run published is as good as building one now.
tag_available: ${{ needs.docker-decide.outputs.needs_image == 'true' && (steps.build.outputs.built == 'true' || steps.wait.outputs.built == 'true' || needs.docker-decide.outputs.rebuild != 'true') }}
env:
BUILD: ${{ needs.docker-decide.outputs.rebuild == 'true' && github.event.pull_request.head.repo.full_name == github.repository }}
REQUEST_BUILD: ${{ needs.docker-decide.outputs.rebuild == 'true' && github.event.pull_request.head.repo.full_name != github.repository }}
steps:
- name: Checkout repository
if: env.BUILD == 'true' || env.REQUEST_BUILD == 'true'
uses: actions/checkout@v4
with:
persist-credentials: false
- name: Request a trusted fork image build
if: env.REQUEST_BUILD == 'true'
env:
PR_NUMBER: ${{ github.event.pull_request.number }}
INPUTS_HASH: ${{ needs.docker-decide.outputs.current }}
FORCE_REBUILD: ${{ needs.docker-decide.outputs.force_rebuild }}
PR_LABELS_JSON: ${{ toJSON(github.event.pull_request.labels.*.name) }}
run: |
mkdir -p "$RUNNER_TEMP/fork-ci-image-request"
python3 - <<'PY'
import json, os
from pathlib import Path
request = dict(pr=int(os.environ['PR_NUMBER']), merge_sha=os.environ['GITHUB_SHA'],
inputs_hash=os.environ['INPUTS_HASH'], run_id=int(os.environ['GITHUB_RUN_ID']),
run_attempt=int(os.environ['GITHUB_RUN_ATTEMPT']),
force_rebuild=os.environ['FORCE_REBUILD'] == 'true',
labels=json.loads(os.environ['PR_LABELS_JSON']))
(Path(os.environ['RUNNER_TEMP']) / 'fork-ci-image-request/request.json').write_text(json.dumps(request))
PY
- uses: actions/upload-artifact@v4
if: env.REQUEST_BUILD == 'true'
with:
name: fork-ci-image-${{ github.run_id }}-${{ github.run_attempt }}
path: ${{ runner.temp }}/fork-ci-image-request/request.json
retention-days: 1
if-no-files-found: error
- name: Wait for trusted image publication
id: wait
if: env.REQUEST_BUILD == 'true'
env:
GH_TOKEN: ${{ github.token }}
run: |
python3 .github/workflows/scripts/fork_ci_image.py wait-build
echo "built=true" >> "$GITHUB_OUTPUT"
# Shared builders accumulate buildkit cache and orphaned builder volumes
# until the disk fills and installs die with ENOSPC mid-layer. Reclaim
# anything idle for 48h before building; recent cache survives.
# The daemon is shared with the host's GPU runners, so prune only what
# holds disk: `system prune` would also delete their idle networks
# (the aliyun rdma-rail* networks job containers need for RDMA).
- name: Free builder disk
if: env.BUILD == 'true'
run: |
df -h / || true
docker builder prune -af --filter "until=48h" || true
docker image prune -af --filter "until=48h" || true
docker volume prune -f || true
df -h / || true
- name: Set up Docker Buildx
if: env.BUILD == 'true'
uses: docker/setup-buildx-action@v3
with:
driver-opts: |
image=moby/buildkit:latest
network=host
- name: Install Python + dependencies
if: env.BUILD == 'true'
run: |
if ! command -v python3 >/dev/null || ! command -v pip3 >/dev/null; then
apt-get -o DPkg::Lock::Timeout=600 update
apt-get -o DPkg::Lock::Timeout=600 install -y python3 python3-pip
fi
# Older pip (<23) lacks --break-system-packages; newer PEP 668
# systems require it. Probe instead of assuming either.
PIP_BSP=""
pip3 install --help 2>/dev/null | grep -q -- --break-system-packages && PIP_BSP="--break-system-packages"
pip3 install $PIP_BSP --ignore-installed typer
- name: Login to Docker Hub
if: env.BUILD == 'true'
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and push PR tag
id: build
if: env.BUILD == 'true'
run: |
python3 docker/build.py --variant cu13 --image-tag custom \
--custom-tag pr-${{ github.event.pull_request.number }} --push
echo "built=true" >> "$GITHUB_OUTPUT"
# The label is a one-shot request: leaving it on would rebuild on every later run.
- name: Consume rebuild-ci-image label
if: steps.build.outputs.built == 'true' && needs.docker-decide.outputs.force_rebuild == 'true'
env:
GH_TOKEN: ${{ github.token }}
run: |
gh api -X DELETE \
"repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/labels/rebuild-ci-image" \
|| echo "::warning::Could not remove the rebuild-ci-image label; remove it by hand or every run will rebuild."
- name: Nothing to build
if: env.BUILD != 'true' && env.REQUEST_BUILD != 'true'
run: echo "No rebuild needed; suites use the PR image if it exists, else the released one."