forked from flagos-ai/FlagScale
-
Notifications
You must be signed in to change notification settings - Fork 0
292 lines (255 loc) · 11.1 KB
/
Copy pathpush_image_harbor.yml
File metadata and controls
292 lines (255 loc) · 11.1 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
name: Push Images to Harbor
on:
# Trigger on push to main to promote pre-built images
push:
branches: [main]
paths:
- 'docker/cuda/**'
- 'docker/build.sh'
- 'tools/install/**'
- 'requirements/**'
- '.github/workflows/build_image_cuda.yml'
workflow_dispatch:
permissions:
contents: read
env:
REMOTE_REGISTRY: harbor.baai.ac.cn
REMOTE_IMAGE_PREFIX: flagscale
jobs:
# ---------------------------------------------------------------------------
# Prepare: find tar files matching this commit SHA
# ---------------------------------------------------------------------------
prepare:
name: Detect tar files for ${{ github.sha }}
runs-on: [flagscale-nvidia-a100-gpu2-32c-128g]
timeout-minutes: 15
container:
image: harbor.baai.ac.cn/flagscale/flagscale-train:dev-cu128-py3.12-20260228210721
volumes:
- /mnt/airs-business/cicd/image_tar:/mnt/airs-business/cicd/image_tar
options: >-
--user root
outputs:
needs_promotion: ${{ steps.detect.outputs.needs_promotion }}
train_tar: ${{ steps.detect.outputs.train_tar }}
inference_tar: ${{ steps.detect.outputs.inference_tar }}
all_tar: ${{ steps.detect.outputs.all_tar }}
tar_dir: ${{ steps.config.outputs.tar_dir }}
suffix: ${{ steps.resolve_pr.outputs.suffix }}
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
sparse-checkout: .github/configs
- name: Load tar_dir from platform config
id: config
run: |
TAR_DIR=$(grep '^tar_dir:' .github/configs/cuda.yml | awk '{print $2}')
echo "tar_dir=${TAR_DIR}" >> "$GITHUB_OUTPUT"
- name: Install gh CLI
run: |
if ! command -v gh &>/dev/null; then
apt-get update && apt-get install -y gh
fi
gh --version
- name: Resolve PR number from merge commit
id: resolve_pr
env:
GH_TOKEN: ${{ github.token }}
run: |
MERGE_SHA="${{ github.sha }}"
PR_NUMBER=$(gh pr list \
--repo ${{ github.repository }} \
--state merged \
--base main \
--limit 50 \
--json number,mergeCommit \
--jq ".[] | select(.mergeCommit.oid == \"${MERGE_SHA}\") | .number" \
| head -1)
if [ -n "$PR_NUMBER" ]; then
echo "Found PR #${PR_NUMBER} for merge commit ${MERGE_SHA}"
echo "suffix=pr${PR_NUMBER}" >> "$GITHUB_OUTPUT"
else
echo "::warning::Could not find PR for merge commit ${MERGE_SHA}, falling back to short SHA"
SHORT_SHA=$(echo "$MERGE_SHA" | cut -c1-7)
echo "suffix=${SHORT_SHA}" >> "$GITHUB_OUTPUT"
fi
- name: Find tar files by PR number
id: detect
shell: bash
run: |
set -euo pipefail
TAR_DIR="${{ steps.config.outputs.tar_dir }}"
SUFFIX="${{ steps.resolve_pr.outputs.suffix }}"
TRAIN_TAR=$(find "$TAR_DIR" -maxdepth 1 -name "*flagscale-train*-${SUFFIX}.tar" 2>/dev/null | sort -r | head -1)
INFERENCE_TAR=$(find "$TAR_DIR" -maxdepth 1 -name "*flagscale-inference*-${SUFFIX}.tar" 2>/dev/null | sort -r | head -1)
ALL_TAR=$(find "$TAR_DIR" -maxdepth 1 -name "*flagscale-all*-${SUFFIX}.tar" 2>/dev/null | sort -r | head -1)
echo "train_tar=${TRAIN_TAR}" >> "$GITHUB_OUTPUT"
echo "inference_tar=${INFERENCE_TAR}" >> "$GITHUB_OUTPUT"
echo "all_tar=${ALL_TAR}" >> "$GITHUB_OUTPUT"
if [ -n "$TRAIN_TAR" ] || [ -n "$INFERENCE_TAR" ] || [ -n "$ALL_TAR" ]; then
echo "needs_promotion=true" >> "$GITHUB_OUTPUT"
echo "Detected tars for ${SUFFIX}:"
if [ -n "$TRAIN_TAR" ]; then echo " train: $TRAIN_TAR"; fi
if [ -n "$INFERENCE_TAR" ]; then echo " inference: $INFERENCE_TAR"; fi
if [ -n "$ALL_TAR" ]; then echo " all: $ALL_TAR"; fi
else
echo "needs_promotion=false" >> "$GITHUB_OUTPUT"
echo "::warning::No tar files found for ${SUFFIX} in $TAR_DIR, skipping promotion"
fi
# ---------------------------------------------------------------------------
# Promote: load tar → retag → push to Harbor
# ---------------------------------------------------------------------------
promote:
name: Push validated images to Harbor
needs: prepare
if: needs.prepare.outputs.needs_promotion == 'true'
runs-on: [flagscale-nvidia-a100-gpu2-32c-128g]
timeout-minutes: 60
outputs:
remote_train_tag: ${{ steps.promote_train.outputs.remote_tag }}
remote_inference_tag: ${{ steps.promote_inference.outputs.remote_tag }}
remote_all_tag: ${{ steps.promote_all.outputs.remote_tag }}
steps:
- name: Login to Harbor registry
uses: docker/login-action@v3
with:
registry: ${{ env.REMOTE_REGISTRY }}
username: ${{ secrets.REGISTRY_USERNAME }}
password: ${{ secrets.CONTAINER_REGISTRY }}
- name: Promote train image to Harbor
id: promote_train
if: needs.prepare.outputs.train_tar != ''
run: |
set -euo pipefail
TAR_PATH="${{ needs.prepare.outputs.train_tar }}"
echo "Loading tar: $TAR_PATH"
LOCAL_TAG=$(sudo docker load -i "$TAR_PATH" | grep -oP '(?<=Loaded image: ).+' | tail -1)
if [ -z "$LOCAL_TAG" ]; then
echo "::error::Failed to parse image tag from docker load output"
exit 1
fi
echo "Loaded image tag: $LOCAL_TAG"
IMAGE_AND_TAG="${LOCAL_TAG#*/}"
REMOTE_TAG="${{ env.REMOTE_REGISTRY }}/${{ env.REMOTE_IMAGE_PREFIX }}/${IMAGE_AND_TAG}"
sudo docker tag "${LOCAL_TAG}" "${REMOTE_TAG}"
sudo docker push "${REMOTE_TAG}"
echo "remote_tag=${REMOTE_TAG}" >> "$GITHUB_OUTPUT"
echo "Pushed: ${REMOTE_TAG}"
- name: Promote inference image to Harbor
id: promote_inference
if: needs.prepare.outputs.inference_tar != ''
run: |
set -euo pipefail
TAR_PATH="${{ needs.prepare.outputs.inference_tar }}"
echo "Loading tar: $TAR_PATH"
LOCAL_TAG=$(sudo docker load -i "$TAR_PATH" | grep -oP '(?<=Loaded image: ).+' | tail -1)
if [ -z "$LOCAL_TAG" ]; then
echo "::error::Failed to parse image tag from docker load output"
exit 1
fi
echo "Loaded image tag: $LOCAL_TAG"
IMAGE_AND_TAG="${LOCAL_TAG#*/}"
REMOTE_TAG="${{ env.REMOTE_REGISTRY }}/${{ env.REMOTE_IMAGE_PREFIX }}/${IMAGE_AND_TAG}"
sudo docker tag "${LOCAL_TAG}" "${REMOTE_TAG}"
sudo docker push "${REMOTE_TAG}"
echo "remote_tag=${REMOTE_TAG}" >> "$GITHUB_OUTPUT"
echo "Pushed: ${REMOTE_TAG}"
- name: Promote all image to Harbor
id: promote_all
if: needs.prepare.outputs.all_tar != ''
run: |
set -euo pipefail
TAR_PATH="${{ needs.prepare.outputs.all_tar }}"
echo "Loading tar: $TAR_PATH"
LOCAL_TAG=$(sudo docker load -i "$TAR_PATH" | grep -oP '(?<=Loaded image: ).+' | tail -1)
if [ -z "$LOCAL_TAG" ]; then
echo "::error::Failed to parse image tag from docker load output"
exit 1
fi
echo "Loaded image tag: $LOCAL_TAG"
IMAGE_AND_TAG="${LOCAL_TAG#*/}"
REMOTE_TAG="${{ env.REMOTE_REGISTRY }}/${{ env.REMOTE_IMAGE_PREFIX }}/${IMAGE_AND_TAG}"
sudo docker tag "${LOCAL_TAG}" "${REMOTE_TAG}"
sudo docker push "${REMOTE_TAG}"
echo "remote_tag=${REMOTE_TAG}" >> "$GITHUB_OUTPUT"
echo "Pushed: ${REMOTE_TAG}"
# ---------------------------------------------------------------------------
# Update config: write Harbor image tags back to cuda.yml and commit
# ---------------------------------------------------------------------------
update_config:
name: Update cuda.yml with Harbor image tags
needs: promote
runs-on: ubuntu-latest
timeout-minutes: 10
permissions:
contents: write
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}
ref: main
- name: Update image tags in cuda.yml
run: |
set -euo pipefail
CONFIG_FILE=".github/configs/cuda.yml"
REMOTE_TRAIN="${{ needs.promote.outputs.remote_train_tag }}"
REMOTE_INFERENCE="${{ needs.promote.outputs.remote_inference_tag }}"
REMOTE_ALL="${{ needs.promote.outputs.remote_all_tag }}"
if [ -n "$REMOTE_TRAIN" ]; then
sed -i "s|^ci_train_image:.*|ci_train_image: ${REMOTE_TRAIN}|" "$CONFIG_FILE"
echo "Updated ci_train_image: ${REMOTE_TRAIN}"
fi
if [ -n "$REMOTE_INFERENCE" ]; then
sed -i "s|^ci_inference_image:.*|ci_inference_image: ${REMOTE_INFERENCE}|" "$CONFIG_FILE"
echo "Updated ci_inference_image: ${REMOTE_INFERENCE}"
fi
if [ -n "$REMOTE_ALL" ]; then
sed -i "s|^ci_image:.*|ci_image: ${REMOTE_ALL}|" "$CONFIG_FILE"
echo "Updated ci_image: ${REMOTE_ALL}"
fi
- name: Commit and push updated config
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git add .github/configs/cuda.yml
if git diff --cached --quiet; then
echo "No changes to commit"
else
git commit -m "ci: update cuda.yml with new Harbor image tags [skip ci]"
git push
fi
# ---------------------------------------------------------------------------
# Cleanup: clean up Docker build cache and dangling images on self-hosted runner
# ---------------------------------------------------------------------------
cleanup:
name: Clean up build cache
needs: ['prepare', 'promote']
runs-on: [flagscale-nvidia-a100-gpu2-32c-128g]
timeout-minutes: 10
if: always() && needs.prepare.result == 'success'
steps:
- name: Remove stale tar files for this PR
run: |
TAR_DIR="${{ needs.prepare.outputs.tar_dir }}"
SUFFIX="${{ needs.prepare.outputs.suffix }}"
if [ -n "$SUFFIX" ] && [ -n "$TAR_DIR" ]; then
echo "Cleaning up old tar files matching *-${SUFFIX}.tar in $TAR_DIR"
find "$TAR_DIR" -maxdepth 1 -name "*-${SUFFIX}.tar" -exec sudo rm -f {} +
fi
- name: Remove dangling images
run: sudo docker image prune -f 2>/dev/null || true
- name: Remove build cache older than 7 days
run: sudo docker builder prune -f --filter "until=168h" 2>/dev/null || true
- name: Remove old localhost registry images
run: |
sudo docker images --format '{{.Repository}}:{{.Tag}} {{.CreatedSince}}' \
| grep 'localhost:5000' \
| grep -E '(weeks|months)' \
| awk '{print $1}' \
| xargs -r sudo docker rmi 2>/dev/null || true
- name: Report disk usage
run: |
echo "Docker disk usage:"
sudo docker system df