-
Notifications
You must be signed in to change notification settings - Fork 0
219 lines (199 loc) Β· 8.92 KB
/
build.yml
File metadata and controls
219 lines (199 loc) Β· 8.92 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
name: Build and Push
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# CI β triggered on every PR: builds images and pushes to GHCR for validation.
# CD β triggered when a PR is merged: promotes the GHCR image to Docker Hub
# via skopeo copy (no rebuild).
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
on:
# CI: validate every PR that touches a template
pull_request:
paths:
- 'official-templates/**'
# CD: promote on merge
pull_request_target:
types: [closed]
paths:
- 'official-templates/**'
# Manual override for both CI and CD
workflow_dispatch:
inputs:
template:
description: 'Template name to build (e.g. pytorch). Leave empty to build all changed.'
required: false
type: string
mode:
description: 'ci = build to GHCR only, cd = promote GHCR β Docker Hub'
required: false
default: 'ci'
type: choice
options: [ci, cd]
sha:
description: 'Full or short SHA of the GHCR image to promote (cd mode only). Defaults to HEAD.'
required: false
type: string
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# CI jobs
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
jobs:
ci-detect:
name: CI β Detect changed templates
if: >
github.event_name == 'pull_request' ||
(github.event_name == 'workflow_dispatch' && inputs.mode == 'ci')
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.set-matrix.outputs.matrix }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Determine templates to build
id: set-matrix
env:
BASE_SHA: ${{ github.event.pull_request.base.sha }}
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
run: |
if [ -n "${{ inputs.template }}" ]; then
TEMPLATES='["${{ inputs.template }}"]'
else
TEMPLATES=$(git diff --name-only "${BASE_SHA}" "${HEAD_SHA}" \
| grep '^official-templates/' \
| awk -F'/' '{print $2}' \
| sort -u \
| grep -v '^\s*$' \
| while read -r dir; do
[ -f "official-templates/$dir/docker-bake.hcl" ] && echo "$dir"
done \
| jq -R -s -c 'split("\n") | map(select(length > 0))')
fi
echo "matrix=${TEMPLATES}" >> "$GITHUB_OUTPUT"
echo "Templates to build: ${TEMPLATES}"
ci-build:
name: CI β Build ${{ matrix.template }}
needs: ci-detect
if: needs.ci-detect.outputs.matrix != '[]' && needs.ci-detect.outputs.matrix != ''
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
strategy:
fail-fast: false
matrix:
template: ${{ fromJson(needs.ci-detect.outputs.matrix) }}
steps:
- uses: actions/checkout@v4
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to GHCR
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Log in to Docker Hub (for registry cache read)
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and push to GHCR
working-directory: official-templates/${{ matrix.template }}
env:
HF_TOKEN: ${{ secrets.HF_TOKEN }}
TEMPLATE: ${{ matrix.template }}
BUILDX_BAKE_ENTITLEMENTS_FS: "0"
run: |
SHA_SHORT="${GITHUB_SHA::7}"
CACHE_REF="yottalabsai/buildcache:${TEMPLATE}"
# Override each bake target's tags to GHCR.
# Tag scheme: ghcr.io/yottalabsai/<template>:<target>-sha-<sha>
# Using per-target tags handles multi-target bake files (e.g. base has 8 targets).
OVERRIDES=$(docker buildx bake --print 2>/dev/null \
| jq -r --arg tmpl "${TEMPLATE}" --arg sha "${SHA_SHORT}" \
'.target | keys[] | "--set \(.).tags=ghcr.io/yottalabsai/\($tmpl):\(.)-sha-\($sha)"' \
| tr '\n' ' ')
eval "docker buildx bake ${OVERRIDES} \
--set '*.cache-from=type=registry,ref=${CACHE_REF}' \
--set '*.cache-to=type=registry,ref=${CACHE_REF},mode=max' \
--set '*.args.HF_TOKEN=${HF_TOKEN:-}' \
--push"
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# CD jobs
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
cd-detect:
name: CD β Detect merged templates
if: >
(github.event_name == 'pull_request_target' && github.event.pull_request.merged == true) ||
(github.event_name == 'workflow_dispatch' && inputs.mode == 'cd')
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.set-matrix.outputs.matrix }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Determine templates to promote
id: set-matrix
env:
BASE_SHA: ${{ github.event.pull_request.base.sha }}
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
run: |
if [ -n "${{ inputs.template }}" ]; then
TEMPLATES='["${{ inputs.template }}"]'
else
TEMPLATES=$(git diff --name-only "${BASE_SHA}" "${HEAD_SHA}" \
| grep '^official-templates/' \
| awk -F'/' '{print $2}' \
| sort -u \
| grep -v '^\s*$' \
| while read -r dir; do
[ -f "official-templates/$dir/docker-bake.hcl" ] && echo "$dir"
done \
| jq -R -s -c 'split("\n") | map(select(length > 0))')
fi
echo "matrix=${TEMPLATES}" >> "$GITHUB_OUTPUT"
echo "Templates to promote: ${TEMPLATES}"
cd-promote:
name: CD β Promote ${{ matrix.template }} β Docker Hub
needs: cd-detect
if: needs.cd-detect.outputs.matrix != '[]' && needs.cd-detect.outputs.matrix != ''
runs-on: ubuntu-latest
permissions:
contents: read
packages: read
strategy:
fail-fast: false
matrix:
template: ${{ fromJson(needs.cd-detect.outputs.matrix) }}
steps:
- uses: actions/checkout@v4
- name: Promote GHCR β Docker Hub
env:
TEMPLATE: ${{ matrix.template }}
PR_HEAD_SHA: ${{ github.event.pull_request.head.sha }}
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
# Priority: manual sha input β PR head β current HEAD
RESOLVE_SHA="${{ inputs.sha }}"
RESOLVE_SHA="${RESOLVE_SHA:-${PR_HEAD_SHA:-${GITHUB_SHA}}}"
SHA_SHORT="${RESOLVE_SHA::7}"
# Use the bake file at the exact commit so target names and
# Docker Hub tags match what CI built.
git checkout "${RESOLVE_SHA}" -- "official-templates/${TEMPLATE}/docker-bake.hcl"
# For each bake target, copy its GHCR image to the Docker Hub tag
# defined in the bake file. skopeo is pre-installed on ubuntu-latest.
cd "official-templates/${TEMPLATE}"
docker buildx bake --print 2>/dev/null \
| jq -r '.target | to_entries[] | "\(.key) \(.value.tags[])"' \
| while IFS=' ' read -r target dh_tag; do
src="docker://ghcr.io/yottalabsai/${TEMPLATE}:${target}-sha-${SHA_SHORT}"
dst="docker://${dh_tag}"
echo "Promoting ${src} β ${dst}"
skopeo copy \
--src-creds "x-access-token:${GITHUB_TOKEN}" \
--dest-creds "${DOCKERHUB_USERNAME}:${DOCKERHUB_TOKEN}" \
"${src}" "${dst}"
done