Repository navigation
685 lines (637 loc) · 32.8 KB
/
Copy pathdocker-publish.yml
File metadata and controls
685 lines (637 loc) · 32.8 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
# No GPU runner needed: cu128 wheels are fat binaries and UNSLOTH_COMPILE_DISABLE=1 blocks GPU JIT.
# DOCKER_API_KEY is an org access token. HAS_GPU_RUNNER='true' gates the smoke test.
name: Publish Blackwell Docker image
on:
push:
# Not on main merges (too costly); the daily cron publishes main. Tags publish immediately.
tags: ['v*']
schedule:
# Off the hour: on-the-hour cron runs queue behind the global peak and can be dropped.
- cron: '17 18 * * *'
workflow_dispatch:
inputs:
unsloth_ref:
# Must default EMPTY: the stable-tag gates require it.
description: 'unsloth git ref override (blank = dispatched branch + stable tags)'
required: false
default: ''
unsloth_zoo_ref:
description: 'unsloth-zoo git ref to bake in'
required: false
default: 'main'
llama_prebuilt_tag:
description: 'unslothai/llama.cpp prebuilt release tag to bake (blank = newest)'
required: false
default: ''
notebooks_ref:
description: 'unslothai/notebooks git ref to bake (resolved to one commit)'
required: false
default: 'main'
env:
REGISTRY: docker.io
IMAGE_NAME: unsloth/unsloth
# Must be the org name: Docker Hub authenticates an org access token as the organization.
REGISTRY_USERNAME: unsloth
# On main each run gets its own group so an overlapping schedule/dispatch is not discarded.
concurrency:
group: docker-publish-${{ github.ref }}-${{ github.ref == 'refs/heads/main' && github.sha || '' }}
cancel-in-progress: false
# Pushes use Docker Hub registry creds, not GITHUB_TOKEN, so read is enough.
permissions:
contents: read
jobs:
# Resolve every upstream ref ONCE, so both arch legs and Studio bake identical bits.
prepare:
runs-on: ubuntu-latest
timeout-minutes: 5
permissions:
contents: read
actions: read
outputs:
# computed once, so a rerun of failed jobs keeps the same date
pin_date: ${{ steps.pin.outputs.date }}
llama_tag: ${{ steps.llama.outputs.tag }}
unsloth_ref: ${{ steps.unsloth_ref.outputs.ref }}
zoo_ref: ${{ steps.zoo_ref.outputs.ref }}
notebooks_commit: ${{ steps.notebooks.outputs.commit }}
steps:
- name: Date of this run, for the nightly pins
id: pin
env:
GH_TOKEN: ${{ github.token }}
run: |
# created_at is fixed at the first attempt: a late rerun cannot claim today's pin.
created="$(gh api "repos/${{ github.repository }}/actions/runs/${{ github.run_id }}" --jq .created_at || true)"
case "$created" in
[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]T*) ;;
*) echo "::error::Could not read this run's creation time (got '${created}')"; exit 1 ;;
esac
day="${created%%T*}"
echo "date=${day//-/.}" >> "$GITHUB_OUTPUT"
- name: Resolve llama.cpp prebuilt tag
id: llama
env:
INPUT_TAG: ${{ github.event.inputs.llama_prebuilt_tag }}
run: |
TAG="$INPUT_TAG"
if [ -z "$TAG" ]; then
# No explicit `shell:`, so no pipefail: a failing curl inside `curl | sed` would yield tag=latest.
if ! REDIRECT="$(curl -fsSL -o /dev/null -w '%{url_effective}' \
https://github.com/unslothai/llama.cpp/releases/latest)"; then
echo "::error::unslothai/llama.cpp unreachable; cannot resolve the newest prebuilt tag"
exit 1
fi
TAG="$(printf '%s\n' "$REDIRECT" | sed -n 's#.*/releases/tag/##p')"
if [ -z "$TAG" ]; then
echo "::error::/releases/latest did not redirect to a release tag (landed on ${REDIRECT})"
exit 1
fi
fi
echo "tag=${TAG}" >> "$GITHUB_OUTPUT"
echo "llama.cpp prebuilt tag: ${TAG}"
# Precedence: dispatch input, else pushed tag, else trigger sha, else main.
- name: Resolve unsloth ref
id: unsloth_ref
env:
INPUT_REF: ${{ github.event.inputs.unsloth_ref }}
TAG_REF: ${{ startsWith(github.ref, 'refs/tags/') && github.ref_name || '' }}
PUSH_SHA: ${{ github.sha }}
run: |
REF="$INPUT_REF"
[ -n "$REF" ] || REF="$TAG_REF"
[ -n "$REF" ] || REF="$PUSH_SHA"
REF="${REF:-main}"
if printf '%s' "$REF" | grep -Eq '^[0-9a-f]{40}$'; then
SHA="$REF"
else
# ls-remote exits 0 even with no match, so non-zero means the remote was unreachable.
# Prefer the peeled ^{} line: an annotated tag's own sha is not a commit.
if ! LS_OUT="$(git ls-remote https://github.com/unslothai/unsloth "$REF" "${REF}^{}")"; then
echo "::error::unslothai/unsloth unreachable; cannot freeze ref '${REF}' to a sha"
exit 1
fi
SHA="$(printf '%s\n' "$LS_OUT" | awk 'NR==1{s=$1; r=$2} $2==r"^{}"{s=$1} END{print s}')"
[ -n "$SHA" ] || SHA="$REF"
fi
echo "ref=${SHA}" >> "$GITHUB_OUTPUT"
echo "unsloth ref: ${SHA}"
# only when the tag exists there: unsloth's v* tags are Studio releases
- name: Resolve unsloth-zoo ref
id: zoo_ref
run: |
REF="${{ github.event.inputs.unsloth_zoo_ref }}"
if [ -z "$REF" ] && [ "${{ startsWith(github.ref, 'refs/tags/') }}" = "true" ]; then
# Status 2 means no matching ref; any other non-zero is a transport failure, not a fallback.
_rc=0
git ls-remote --exit-code --tags https://github.com/unslothai/unsloth-zoo \
"refs/tags/${{ github.ref_name }}" >/dev/null 2>&1 || _rc=$?
if [ "$_rc" = "0" ]; then
REF="${{ github.ref_name }}"
elif [ "$_rc" != "2" ]; then
echo "::error::unslothai/unsloth-zoo unreachable (git ls-remote exit ${_rc}); cannot tell whether it carries tag '${{ github.ref_name }}'"
exit 1
fi
fi
REF="${REF:-main}"
if printf '%s' "$REF" | grep -Eq '^[0-9a-f]{40}$'; then
SHA="$REF"
else
if ! LS_OUT="$(git ls-remote https://github.com/unslothai/unsloth-zoo "$REF" "${REF}^{}")"; then
echo "::error::unslothai/unsloth-zoo unreachable; cannot freeze ref '${REF}' to a sha"
exit 1
fi
SHA="$(printf '%s\n' "$LS_OUT" | awk 'NR==1{s=$1; r=$2} $2==r"^{}"{s=$1} END{print s}')"
[ -n "$SHA" ] || SHA="$REF"
fi
echo "ref=${SHA}" >> "$GITHUB_OUTPUT"
echo "unsloth-zoo ref: ${SHA}"
# one commit, so baked templates + .unsloth_template_commit match across legs
- name: Resolve unsloth/notebooks commit
id: notebooks
env:
INPUT_REF: ${{ github.event.inputs.notebooks_ref }}
run: |
REF="${INPUT_REF:-main}"
if printf '%s' "$REF" | grep -Eq '^[0-9a-f]{40}$'; then
SHA="$REF"
else
if ! LS_OUT="$(git ls-remote https://github.com/unslothai/notebooks "$REF" "${REF}^{}")"; then
echo "::error::unslothai/notebooks unreachable; cannot freeze ref '${REF}' to a sha"
exit 1
fi
SHA="$(printf '%s\n' "$LS_OUT" | awk 'NR==1{s=$1; r=$2} $2==r"^{}"{s=$1} END{print s}')"
[ -n "$SHA" ] || SHA="$REF"
fi
echo "commit=${SHA}" >> "$GITHUB_OUTPUT"
echo "notebooks commit: ${SHA}"
build:
needs: prepare
strategy:
fail-fast: false
matrix:
include:
- platform: linux/amd64
runner: ubuntu-latest
- platform: linux/arm64
runner: ubuntu-24.04-arm
runs-on: ${{ matrix.runner }}
timeout-minutes: 90
permissions:
contents: read
packages: write
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- name: Reclaim disk
run: |
# unused toolchains; paths differ across runners, hence `|| true`
sudo rm -rf /usr/share/dotnet /usr/local/lib/android /opt/ghc \
/opt/hostedtoolcache "$AGENT_TOOLSDIRECTORY" \
/usr/local/.ghcup /usr/share/swift \
/usr/local/share/powershell /usr/local/lib/node_modules \
/usr/local/julia* /opt/microsoft /usr/share/miniconda \
/opt/az /usr/local/share/boost /usr/local/share/chromium || true
sudo docker image prune -af >/dev/null 2>&1 || true
df -h /
- uses: docker/setup-buildx-action@f87e5991a6d7451dcb8d9637bfbc97413f497069 # v4.4.1
- name: Log in to Docker Hub
uses: docker/login-action@81c6f93d4bc991e510b342a00b419b3bb4efc5c6 # v4.7.0
with:
username: ${{ env.REGISTRY_USERNAME }}
password: ${{ secrets.DOCKER_API_KEY }}
- name: Resolve labels
id: meta
uses: docker/metadata-action@dc802804100637a589fabce1cb79ff13a1411302 # v6.2.0
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
# The action copies the repo's Apache-2.0, but both images ship Studio (AGPL-3.0) too.
labels: |
org.opencontainers.image.licenses=Apache-2.0 AND AGPL-3.0-only
- name: Build and push (per-arch by digest)
id: build
uses: docker/build-push-action@c3c9e263c25d99ce0380d002d59b67737d91b0dc # v7.4.0
with:
context: ./docker
file: ./docker/Dockerfile
platforms: ${{ matrix.platform }}
labels: ${{ steps.meta.outputs.labels }}
# No registry cache: the frozen refs move most nights, so the install layer is invalidated anyway
# and the cache cost ~20 GB per arch.
outputs: type=image,name=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }},push-by-digest=true,name-canonical=true,push=true
# Keep comments OUT of build-args: every non-empty line becomes a --build-arg.
build-args: |
CUDA_VERSION=12.8.1
UBUNTU_VERSION=24.04
PYTHON_VERSION=3.12
UNSLOTH_REF=${{ needs.prepare.outputs.unsloth_ref }}
UNSLOTH_ZOO_REF=${{ needs.prepare.outputs.zoo_ref }}
LLAMA_PREBUILT_TAG=${{ needs.prepare.outputs.llama_tag }}
UNSLOTH_NOTEBOOKS_REF=${{ needs.prepare.outputs.notebooks_commit }}
- name: Export digest
run: |
mkdir -p /tmp/digests
digest='${{ steps.build.outputs.digest }}'
touch "/tmp/digests/${digest#sha256:}"
- name: Upload digest
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
with:
name: digests-core-${{ matrix.platform == 'linux/amd64' && 'amd64' || 'arm64' }}
path: /tmp/digests/*
if-no-files-found: error
retention-days: 1
merge:
runs-on: ubuntu-latest
needs: [prepare, build]
timeout-minutes: 15
permissions:
contents: read
packages: write
outputs:
digest: ${{ steps.manifest_digest.outputs.digest }}
steps:
- uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
with:
path: /tmp/digests
pattern: digests-core-*
merge-multiple: true
- uses: docker/setup-buildx-action@f87e5991a6d7451dcb8d9637bfbc97413f497069 # v4.4.1
- name: Log in to Docker Hub
uses: docker/login-action@81c6f93d4bc991e510b342a00b419b3bb4efc5c6 # v4.7.0
with:
username: ${{ env.REGISTRY_USERNAME }}
password: ${{ secrets.DOCKER_API_KEY }}
- name: Resolve tags
id: meta
uses: docker/metadata-action@dc802804100637a589fabce1cb79ff13a1411302 # v6.2.0
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
# latest=auto would tag :latest on a v* push, colliding with the Studio image
flavor: latest=false
tags: |
# only on the default branch AND with no baked input overridden on
# dispatch, else a feature ref overwrites :core with non-main bits
type=raw,value=core,enable=${{ github.ref == format('refs/heads/{0}', github.event.repository.default_branch) && github.event.inputs.unsloth_ref == '' && (github.event.inputs.unsloth_zoo_ref == '' || github.event.inputs.unsloth_zoo_ref == 'main') && (github.event.inputs.notebooks_ref == '' || github.event.inputs.notebooks_ref == 'main') && github.event.inputs.llama_prebuilt_tag == '' }}
type=ref,event=tag,prefix=core-
type=schedule,pattern=core-nightly-${{ needs.prepare.outputs.pin_date }}
type=raw,value=core-build-${{ github.run_id }}
- name: Create multi-arch manifest
working-directory: /tmp/digests
run: |
# A dated pin is immutable: a rerun must not replace an existing one.
TAGS=""
for t in $(jq -r '.tags[]' <<<"$DOCKER_METADATA_OUTPUT_JSON"); do
name="${t##*:}"
case "$name" in
nightly-[0-9]*|core-nightly-[0-9]*)
# Only 404 frees the name; 000/429/5xx must not become an overwrite.
for i in 1 2 3 4 5; do
code="$(curl -sS -o /dev/null -w '%{http_code}' "https://hub.docker.com/v2/repositories/${{ env.IMAGE_NAME }}/tags/${name}" || true)"
case "$code" in 200|404) break ;; esac
echo "probe of ${t} returned HTTP ${code} (attempt $i)"; sleep 15
done
case "$code" in
200) echo "::notice::${t} already exists and stays as it is"; continue ;;
404) ;;
*) echo "::error::Cannot tell whether ${t} exists (HTTP ${code}); not risking an overwrite"; exit 1 ;;
esac ;;
esac
TAGS="${TAGS} -t ${t}"
done
docker buildx imagetools create ${TAGS} \
$(printf '${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@sha256:%s ' *)
- name: Inspect the result
run: |
for tag in $(jq -r '.tags[]' <<<"$DOCKER_METADATA_OUTPUT_JSON"); do
echo "=== $tag ==="
docker buildx imagetools inspect "$tag"
done
- name: Export manifest digest
id: manifest_digest
working-directory: /tmp/digests
run: |
# NOT tags[0]: on a main push that is the mutable :core, retagged by racing runs.
TAG="$(jq -r '([.tags[] | select(contains(":core-build-"))] | first) // .tags[0]' <<<"$DOCKER_METADATA_OUTPUT_JSON")"
DIGEST="$(docker buildx imagetools inspect "$TAG" --format '{{json .Manifest.Digest}}' | tr -d '"')"
test -n "$DIGEST"
# Confirm it holds what this run pushed: baking another run's base is silent.
REF="${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@${DIGEST}"
CHILDREN="$(docker buildx imagetools inspect --raw "$REF" | jq -r '.manifests[]?.digest')"
# Per-arch pushes are OCI indexes (image + attestation) that the merge flattens, so compare
# their children, not the per-arch index digest.
missing=0
for d in *; do
want="$(docker buildx imagetools inspect --raw "${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@sha256:${d}" \
| jq -r '.manifests[]?.digest')"
[ -n "$want" ] || want="sha256:${d}"
for w in $want; do
grep -qxF "$w" <<<"$CHILDREN" || {
echo "::error::${TAG} resolved to ${DIGEST}, which does not contain ${w} (from sha256:${d} this run pushed); another ref at this commit retagged it"
missing=1
}
done
done
test "$missing" = 0
echo "digest=${DIGEST}" >> "$GITHUB_OUTPUT"
echo "base manifest: ${TAG} @ ${DIGEST}"
# Full image (:latest). The arm64 leg builds Studio's vite frontend natively.
build-studio:
needs: [prepare, merge]
strategy:
fail-fast: false
matrix:
include:
- platform: linux/amd64
runner: ubuntu-latest
- platform: linux/arm64
runner: ubuntu-24.04-arm
runs-on: ${{ matrix.runner }}
timeout-minutes: 150
permissions:
contents: read
packages: write
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- name: Reclaim disk
run: |
# unused toolchains; paths differ across runners, hence `|| true`
sudo rm -rf /usr/share/dotnet /usr/local/lib/android /opt/ghc \
/opt/hostedtoolcache "$AGENT_TOOLSDIRECTORY" \
/usr/local/.ghcup /usr/share/swift \
/usr/local/share/powershell /usr/local/lib/node_modules \
/usr/local/julia* /opt/microsoft /usr/share/miniconda \
/opt/az /usr/local/share/boost /usr/local/share/chromium || true
sudo docker image prune -af >/dev/null 2>&1 || true
df -h /
- uses: docker/setup-buildx-action@f87e5991a6d7451dcb8d9637bfbc97413f497069 # v4.4.1
- name: Log in to Docker Hub
uses: docker/login-action@81c6f93d4bc991e510b342a00b419b3bb4efc5c6 # v4.7.0
with:
username: ${{ env.REGISTRY_USERNAME }}
password: ${{ secrets.DOCKER_API_KEY }}
- name: Resolve labels
id: meta
uses: docker/metadata-action@dc802804100637a589fabce1cb79ff13a1411302 # v6.2.0
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
# The action copies the repo's Apache-2.0, but both images ship Studio (AGPL-3.0) too.
labels: |
org.opencontainers.image.licenses=Apache-2.0 AND AGPL-3.0-only
- name: Build and push (per-arch by digest)
id: build
uses: docker/build-push-action@c3c9e263c25d99ce0380d002d59b67737d91b0dc # v7.4.0
with:
context: ./docker
file: ./docker/Dockerfile.studio
platforms: ${{ matrix.platform }}
labels: ${{ steps.meta.outputs.labels }}
# No cache-from / cache-to, for the same reason as the core build above.
outputs: type=image,name=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }},push-by-digest=true,name-canonical=true,push=true
# Same values the base build baked (build-args must be KEY=VALUE only).
build-args: |
BASE_IMAGE=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@${{ needs.merge.outputs.digest }}
UNSLOTH_STUDIO_REF=${{ needs.prepare.outputs.unsloth_ref }}
UNSLOTH_STUDIO_ZOO_REF=${{ needs.prepare.outputs.zoo_ref }}
LLAMA_PREBUILT_TAG=${{ needs.prepare.outputs.llama_tag }}
# An image-wide UNSLOTH_ALLOW_CPU would disable Unsloth's TRL patches on GPU hosts; check the
# pushed image. The image opts in via UNSLOTH_IMAGE_ALLOW_CPU=1, translated by entrypoint.sh.
- name: Assert the Studio image env cannot disable training
run: |
REF='${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@${{ steps.build.outputs.digest }}'
ENV_JSON="$(docker buildx imagetools inspect "$REF" --format '{{json .Image}}' \
| jq -c '[.. | objects | select(has("config")) | .config.Env // []] | add')"
echo "$ENV_JSON" | jq -r '.[] | select(startswith("UNSLOTH_"))'
echo "$ENV_JSON" | jq -e 'index("UNSLOTH_IMAGE_ALLOW_CPU=1") != null' >/dev/null \
|| { echo "::error::Studio image env lacks UNSLOTH_IMAGE_ALLOW_CPU=1"; exit 1; }
echo "$ENV_JSON" | jq -e 'map(select(startswith("UNSLOTH_ALLOW_CPU="))) | length == 0' >/dev/null \
|| { echo "::error::Studio image env carries UNSLOTH_ALLOW_CPU, which disables Unsloth's TRL patches on GPU hosts"; exit 1; }
- name: Export digest
run: |
mkdir -p /tmp/digests
digest='${{ steps.build.outputs.digest }}'
touch "/tmp/digests/${digest#sha256:}"
- name: Upload digest
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
with:
name: digests-studio-${{ matrix.platform == 'linux/amd64' && 'amd64' || 'arm64' }}
path: /tmp/digests/*
if-no-files-found: error
retention-days: 1
merge-studio:
runs-on: ubuntu-latest
needs: [prepare, build-studio]
timeout-minutes: 15
permissions:
contents: read
packages: write
outputs:
digest: ${{ steps.studio_manifest_digest.outputs.digest }}
steps:
- uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
with:
path: /tmp/digests
pattern: digests-studio-*
merge-multiple: true
- uses: docker/setup-buildx-action@f87e5991a6d7451dcb8d9637bfbc97413f497069 # v4.4.1
- name: Log in to Docker Hub
uses: docker/login-action@81c6f93d4bc991e510b342a00b419b3bb4efc5c6 # v4.7.0
with:
username: ${{ env.REGISTRY_USERNAME }}
password: ${{ secrets.DOCKER_API_KEY }}
- name: Resolve tags
id: meta
uses: docker/metadata-action@dc802804100637a589fabce1cb79ff13a1411302 # v6.2.0
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
# Disables implicit latest=auto, which would tag :latest on a v* push and bypass the gate below.
flavor: latest=false
tags: |
type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', github.event.repository.default_branch) && github.event.inputs.unsloth_ref == '' && (github.event.inputs.unsloth_zoo_ref == '' || github.event.inputs.unsloth_zoo_ref == 'main') && (github.event.inputs.notebooks_ref == '' || github.event.inputs.notebooks_ref == 'main') && github.event.inputs.llama_prebuilt_tag == '' }}
type=raw,value=studio,enable=${{ github.ref == format('refs/heads/{0}', github.event.repository.default_branch) && github.event.inputs.unsloth_ref == '' && (github.event.inputs.unsloth_zoo_ref == '' || github.event.inputs.unsloth_zoo_ref == 'main') && (github.event.inputs.notebooks_ref == '' || github.event.inputs.notebooks_ref == 'main') && github.event.inputs.llama_prebuilt_tag == '' }}
type=ref,event=tag
type=schedule,pattern=nightly-${{ needs.prepare.outputs.pin_date }}
type=raw,value=build-${{ github.run_id }}
- name: Create multi-arch manifest
working-directory: /tmp/digests
run: |
# A dated pin is immutable: a rerun must not replace an existing one.
TAGS=""
for t in $(jq -r '.tags[]' <<<"$DOCKER_METADATA_OUTPUT_JSON"); do
name="${t##*:}"
case "$name" in
nightly-[0-9]*|core-nightly-[0-9]*)
# Only 404 frees the name; 000/429/5xx must not become an overwrite.
for i in 1 2 3 4 5; do
code="$(curl -sS -o /dev/null -w '%{http_code}' "https://hub.docker.com/v2/repositories/${{ env.IMAGE_NAME }}/tags/${name}" || true)"
case "$code" in 200|404) break ;; esac
echo "probe of ${t} returned HTTP ${code} (attempt $i)"; sleep 15
done
case "$code" in
200) echo "::notice::${t} already exists and stays as it is"; continue ;;
404) ;;
*) echo "::error::Cannot tell whether ${t} exists (HTTP ${code}); not risking an overwrite"; exit 1 ;;
esac ;;
esac
TAGS="${TAGS} -t ${t}"
done
docker buildx imagetools create ${TAGS} \
$(printf '${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@sha256:%s ' *)
- name: Inspect the result
run: |
for tag in $(jq -r '.tags[]' <<<"$DOCKER_METADATA_OUTPUT_JSON"); do
echo "=== $tag ==="
docker buildx imagetools inspect "$tag"
done
- name: Export manifest digest
id: studio_manifest_digest
working-directory: /tmp/digests
run: |
TAG="$(jq -r '([.tags[] | select(contains(":build-"))] | first) // .tags[0]' <<<"$DOCKER_METADATA_OUTPUT_JSON")"
DIGEST="$(docker buildx imagetools inspect "$TAG" --format '{{json .Manifest.Digest}}' | tr -d '"')"
test -n "$DIGEST"
REF="${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@${DIGEST}"
CHILDREN="$(docker buildx imagetools inspect --raw "$REF" | jq -r '.manifests[]?.digest')"
# per-arch pushes are indexes (image + attestation) that the merge
# flattens, so compare their children, not the index digest; see the base merge
missing=0
for d in *; do
want="$(docker buildx imagetools inspect --raw "${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@sha256:${d}" \
| jq -r '.manifests[]?.digest')"
[ -n "$want" ] || want="sha256:${d}"
for w in $want; do
grep -qxF "$w" <<<"$CHILDREN" || {
echo "::error::${TAG} resolved to ${DIGEST}, which does not contain ${w} (from sha256:${d} this run pushed); another ref at this commit retagged it"
missing=1
}
done
done
test "$missing" = 0
echo "digest=${DIGEST}" >> "$GITHUB_OUTPUT"
echo "studio manifest: ${TAG} @ ${DIGEST}"
# Syncs docker/DOCKERHUB.md to the Hub page (no push writes it) when :latest moved, read back,
# failing on a token that cannot edit. Namespace-scoped route: the legacy one 403s org tokens.
hub-readme:
needs: merge-studio
if: ${{ github.ref == format('refs/heads/{0}', github.event.repository.default_branch) && github.event.inputs.unsloth_ref == '' && (github.event.inputs.unsloth_zoo_ref == '' || github.event.inputs.unsloth_zoo_ref == 'main') && (github.event.inputs.notebooks_ref == '' || github.event.inputs.notebooks_ref == 'main') && github.event.inputs.llama_prebuilt_tag == '' }}
runs-on: ubuntu-latest
timeout-minutes: 5
permissions:
contents: read
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- name: Sync docker/DOCKERHUB.md to the Docker Hub page
# Via env, not ${{ secrets.* }} in the script, so the key never lands in the script file or argv.
env:
DOCKER_API_KEY: ${{ secrets.DOCKER_API_KEY }}
run: |
README=docker/DOCKERHUB.md
test -s "$README"
# Org tokens work only on namespace-scoped routes and need repository edit permission.
HUB="https://hub.docker.com/v2/namespaces/${IMAGE_NAME%%/*}/repositories/${IMAGE_NAME#*/}"
# Body built from env and passed on stdin, so the key is never a command-line argument.
TOKEN="$(python3 -c 'import json,os,sys; sys.stdout.write(json.dumps({"identifier": os.environ["REGISTRY_USERNAME"], "secret": os.environ["DOCKER_API_KEY"]}))' \
| curl -sS -X POST https://hub.docker.com/v2/auth/token \
-H 'Content-Type: application/json' --data-binary @- \
| python3 -c 'import json,sys; print(json.load(sys.stdin).get("access_token",""))')"
if [ -z "$TOKEN" ]; then
echo "::error::Could not exchange DOCKER_API_KEY for a Hub API token; the Hub page was not updated."
exit 1
fi
BODY="$(python3 -c 'import json,sys; print(json.dumps({"full_description": open(sys.argv[1], encoding="utf-8").read()}))' "$README")"
CODE="$(curl -sS -o /tmp/hub_patch.json -w '%{http_code}' -X PATCH "$HUB" \
-H "Authorization: Bearer ${TOKEN}" -H 'Content-Type: application/json' \
--data-binary "$BODY")"
echo "PATCH returned HTTP ${CODE}"
# Read it back rather than trusting the status code.
LIVE="$(curl -sS -H "Authorization: Bearer ${TOKEN}" "$HUB" \
| python3 -c 'import json,sys; print(json.load(sys.stdin).get("full_description",""))')"
if [ "$LIVE" != "$(cat "$README")" ]; then
echo "::error::The Hub page does not match ${README} after PATCH ${CODE}. DOCKER_API_KEY most likely lacks the repository edit permission on ${IMAGE_NAME}."
head -c 400 /tmp/hub_patch.json; echo
exit 1
fi
echo "Hub page for ${IMAGE_NAME} now matches ${README}."
smoke-test:
needs: [merge, merge-studio]
if: ${{ vars.HAS_GPU_RUNNER == 'true' }}
runs-on: [self-hosted, gpu]
timeout-minutes: 30
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
# Pulled by the digest the merge job verified, so no tag gates to keep in sync here.
- name: Pull and smoke-test the base image
run: |
# By digest: the handle tag is gone once cleanup ran.
REF="${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@${{ needs.merge.outputs.digest }}"
echo "smoke-testing $REF"
docker pull "$REF"
docker run --rm --gpus all "$REF" python /workspace/smoke_test.py
- name: Boot the full image and probe Studio + Jupyter
run: |
REF="${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@${{ needs.merge-studio.outputs.digest }}"
echo "booting $REF"
docker pull "$REF"
CID="$(docker run -d --gpus all -p 18000:8000 -p 18888:8888 "$REF")"
trap 'docker logs --tail 100 "$CID"; docker rm -f "$CID"' EXIT
ok_studio=0; ok_jupyter=0
for i in $(seq 1 60); do
if curl -fsS http://localhost:18000/api/health >/dev/null 2>&1; then ok_studio=1; fi
# /api returns 403 (the launcher sets a password hash); /login is
# unauthenticated and 200s once up
if curl -fsS http://localhost:18888/login >/dev/null 2>&1; then ok_jupyter=1; fi
[ "$ok_studio" = 1 ] && [ "$ok_jupyter" = 1 ] && break
sleep 5
done
[ "$ok_studio" = 1 ] || { echo "Studio /api/health never went healthy"; exit 1; }
[ "$ok_jupyter" = 1 ] || { echo "Jupyter /login never responded"; exit 1; }
echo "Studio + Jupyter healthy"
# Namespace-scoped route: the legacy one rejects org tokens. Overridden dispatches keep their handles.
cleanup:
needs: [merge, merge-studio, hub-readme, smoke-test]
if: ${{ always() && (github.event_name != 'workflow_dispatch' || (github.ref == format('refs/heads/{0}', github.event.repository.default_branch) && github.event.inputs.unsloth_ref == '' && (github.event.inputs.unsloth_zoo_ref == '' || github.event.inputs.unsloth_zoo_ref == 'main') && (github.event.inputs.notebooks_ref == '' || github.event.inputs.notebooks_ref == 'main') && github.event.inputs.llama_prebuilt_tag == '')) }}
runs-on: ubuntu-latest
timeout-minutes: 10
permissions:
contents: read
steps:
- name: Remove this run's handle tags, prune old nightly pins
env:
NIGHTLY_KEEP_DAYS: "60"
DOCKER_API_KEY: ${{ secrets.DOCKER_API_KEY }}
run: |
HUB="https://hub.docker.com/v2/namespaces/${IMAGE_NAME%%/*}/repositories/${IMAGE_NAME#*/}"
# Body built from env and passed on stdin, so the key is never a command-line argument.
TOKEN="$(python3 -c 'import json,os,sys; sys.stdout.write(json.dumps({"identifier": os.environ["REGISTRY_USERNAME"], "secret": os.environ["DOCKER_API_KEY"]}))' \
| curl -sS -X POST https://hub.docker.com/v2/auth/token \
-H 'Content-Type: application/json' --data-binary @- \
| python3 -c 'import json,sys; print(json.load(sys.stdin).get("access_token",""))')"
if [ -z "$TOKEN" ]; then
echo "::error::Could not exchange DOCKER_API_KEY for a Hub API token; handle tags of run ${{ github.run_id }} were not removed."
exit 1
fi
failed=0
drop() {
code="$(curl -sS -o /dev/null -w '%{http_code}' -X DELETE "${HUB}/tags/$1" -H "Authorization: Bearer ${TOKEN}")"
case "$code" in
204|404) echo "removed ${IMAGE_NAME}:$1 (HTTP ${code})" ;;
*) echo "::error::${IMAGE_NAME}:$1 not removed (HTTP ${code})"; failed=1 ;;
esac
}
drop "core-build-${{ github.run_id }}"
drop "build-${{ github.run_id }}"
if [ "${{ github.event_name }}" = "schedule" ]; then
cutoff="$(date -u -d "-${NIGHTLY_KEEP_DAYS} days" +%Y.%m.%d)"
url="${HUB}/tags?page_size=100"
names=""
while [ -n "$url" ]; do
page="$(curl -sS "$url" -H "Authorization: Bearer ${TOKEN}")"
names="${names} $(jq -r '.results[]?.name' <<<"$page")"
url="$(jq -r '.next // empty' <<<"$page")"
done
for name in $names; do
case "$name" in
nightly-[0-9][0-9][0-9][0-9].[0-9][0-9].[0-9][0-9]|core-nightly-[0-9][0-9][0-9][0-9].[0-9][0-9].[0-9][0-9])
stamp="${name##*nightly-}"
if [ "$stamp" \< "$cutoff" ]; then drop "$name"; fi ;;
esac
done
fi
test "$failed" = 0