forked from harvard-edge/cs249r_book
-
Notifications
You must be signed in to change notification settings - Fork 0
434 lines (395 loc) Β· 16.7 KB
/
Copy pathinfra-container-linux.yml
File metadata and controls
434 lines (395 loc) Β· 16.7 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
name: 'π§ Infra Β· π³ Container (Linux)'
# =============================================================================
# π³ Linux Container β Build and push the Quarto build container to GHCR
# =============================================================================
#
# Builds the Linux Docker image with all pre-installed dependencies (Quarto,
# Python, R, TinyTeX) to eliminate 30-45 minute setup time in CI runs.
#
# Flow:
# 1. ENVIRONMENT β Validate permissions, free disk space, configure Buildx
# 2. REGISTRY β Authenticate to GHCR and verify write access
# 3. BUILD & PUSH β Build container image with layer caching, push to GHCR
# 4. SUMMARY β Report build status, image digest, and cache hit
#
# Triggers:
# - push (dev): Changes to book/tools/dependencies/ or book/docker/linux/
# - schedule: Weekly rebuild (Sunday midnight UTC)
# - workflow_dispatch: Manual with force_rebuild, no_cache options
# - workflow_call: Reusable by other workflows
#
# Deploys to: ghcr.io/harvard-edge/mlsysbook/quarto-linux:latest
# Secrets: GITHUB_TOKEN (automatic)
# Vars: BOOK_DOCKER, BOOK_ROOT, BOOK_TOOLS, BOOK_QUARTO, BOOK_DEPS
#
# Related:
# - infra-container-windows.yml β Windows container build (runs 2h after)
# - infra-health-check.yml β Daily validation of built containers
# - infra-cleanup-caches.yml β Weekly GHA cache cleanup
#
# =============================================================================
# Prevent multiple builds running simultaneously
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
# Default token scope for jobs without their own `permissions` (e.g. preflight).
# The container build job overrides this with `packages: write` where needed.
permissions:
contents: read
on:
workflow_dispatch:
inputs:
force_rebuild:
description: 'Force rebuild even if no changes'
required: false
default: false
type: boolean
no_cache:
description: 'Disable Docker build cache (fresh build)'
required: false
default: false
type: boolean
container_registry:
description: 'Container registry URL'
required: false
default: 'ghcr.io'
type: string
container_name:
description: 'Container image name'
required: false
default: 'quarto-linux'
type: string
container_tag:
description: 'Container tag'
required: false
default: 'latest'
type: string
workflow_call:
inputs:
force_rebuild:
required: false
default: false
type: boolean
no_cache:
required: false
default: false
type: boolean
container_registry:
required: false
default: 'ghcr.io'
type: string
container_name:
required: false
default: 'quarto-linux'
type: string
container_tag:
required: false
default: 'latest'
type: string
outputs:
build-status:
description: "Container build status (success/failure/skipped)"
value: ${{ jobs.build.outputs.build-status }}
image-name:
description: "Full container image name with registry"
value: ${{ jobs.build.outputs.image-name }}
image-digest:
description: "Container image digest (SHA256)"
value: ${{ jobs.build.outputs.image-digest }}
cache-hit:
description: "Whether build used cache (true/false)"
value: ${{ jobs.build.outputs.cache-hit }}
# Re-enable automatic triggers
schedule:
- cron: '0 0 * * 0' # Weekly rebuild (Sunday at midnight)
push:
branches: [dev] # Only trigger on dev branch, not main
paths:
- 'book/tools/dependencies/**'
- 'book/docker/linux/**'
- '.github/workflows/infra-container-linux.yml'
env:
# =============================================================================
# PATH CONFIGURATION - Uses GitHub Repository Variables (Settings > Variables)
# =============================================================================
# MLSysBook content lives under book/ to accommodate TinyTorch at root
# Use ${{ vars.BOOK_ROOT }}, ${{ vars.BOOK_DOCKER }}, etc. in workflow steps
# Variables: BOOK_ROOT, BOOK_DOCKER, BOOK_TOOLS, BOOK_QUARTO, BOOK_DEPS
# Container Registry Configuration (configurable via inputs)
REGISTRY: ${{ (github.event_name == 'workflow_dispatch' && inputs.container_registry) || 'ghcr.io' }}
IMAGE_NAME: ${{ github.repository }}/${{ (github.event_name == 'workflow_dispatch' && inputs.container_name) || 'quarto-linux' }}
CONTAINER_TAG: ${{ (github.event_name == 'workflow_dispatch' && inputs.container_tag) || 'latest' }}
# Container Build Configuration
PLATFORM: linux/amd64
# Using vars.BOOK_DOCKER (repository variable) - works in all contexts
DOCKERFILE_PATH: ./${{ vars.BOOK_DOCKER }}/linux/Dockerfile
CONTEXT_PATH: .
jobs:
preflight:
name: π Preflight external URLs
runs-on: ubuntu-latest
if: github.repository_owner == 'harvard-edge'
timeout-minutes: 5
steps:
- name: π Probe required URLs and TL mirror pool
env:
QUARTO_VERSION: '1.9.27'
TEXLIVE_REPOSITORY_URL: 'https://mirrors.mit.edu/CTAN/systems/texlive/tlnet'
run: |
set -uo pipefail
# Required URLs β every one must return 200. These are the choke points
# the container build hits before installing TeX Live; a dead one wastes
# 30+ min on a runner before failing inside docker build.
required=(
"https://ppa.launchpadcontent.net/inkscape.dev/stable/ubuntu/dists/jammy/Release"
"https://cloud.r-project.org/bin/linux/ubuntu/marutter_pubkey.asc"
"https://cloud.r-project.org/bin/linux/ubuntu/jammy-cran40/InRelease"
"https://github.com/quarto-dev/quarto-cli/releases/download/v${QUARTO_VERSION}/quarto-${QUARTO_VERSION}-linux-amd64.deb"
"${TEXLIVE_REPOSITORY_URL}/tlpkg/texlive.tlpdb"
)
# TeX Live install-tl mirror pool β install-texlive-base.sh tries each
# in order and falls through on failure, so this is resilience not a
# hard gate. Require β₯3 of 5 alive.
mirrors=(
"https://mirrors.mit.edu/CTAN/systems/texlive/tlnet/install-tl-unx.tar.gz"
"https://ctan.math.washington.edu/tex-archive/systems/texlive/tlnet/install-tl-unx.tar.gz"
"https://mirrors.rit.edu/CTAN/systems/texlive/tlnet/install-tl-unx.tar.gz"
"https://mirror.las.iastate.edu/tex-archive/systems/texlive/tlnet/install-tl-unx.tar.gz"
"https://mirror.ctan.org/systems/texlive/tlnet/install-tl-unx.tar.gz"
)
probe() {
url="$1"
code=$(curl -sS -o /dev/null -w '%{http_code}' -m 15 -L --retry 1 "$url" 2>/dev/null || echo "ERR")
printf '%s\t%s\n' "$code" "$url"
}
export -f probe
echo "=== Required URLs (all must return 200) ==="
fail=0
tmp=$(mktemp)
printf '%s\n' "${required[@]}" | xargs -P 8 -I{} bash -c 'probe "$@"' _ {} > "$tmp"
while IFS=$'\t' read -r code url; do
if [ "$code" = "200" ]; then
echo " β $code $url"
else
echo " β $code $url"
fail=$((fail+1))
fi
done < "$tmp"
echo
echo "=== TL install-tl mirror pool (need β₯3 of ${#mirrors[@]} alive) ==="
alive=0
tmp2=$(mktemp)
printf '%s\n' "${mirrors[@]}" | xargs -P 8 -I{} bash -c 'probe "$@"' _ {} > "$tmp2"
while IFS=$'\t' read -r code url; do
if [ "$code" = "200" ]; then
echo " β $code $url"
alive=$((alive+1))
else
echo " β $code $url"
fi
done < "$tmp2"
echo
if [ "$fail" -gt 0 ]; then
echo "β $fail required URL(s) failed β aborting before container build"
exit 1
fi
if [ "$alive" -lt 3 ]; then
echo "β Only $alive of ${#mirrors[@]} TL mirrors alive (need β₯3) β aborting"
exit 1
fi
echo "β
Preflight passed: required URLs all up, $alive/${#mirrors[@]} TL mirrors alive"
build:
needs: preflight
runs-on: ubuntu-latest
if: github.repository_owner == 'harvard-edge'
timeout-minutes: 90 # 1.5 hour timeout for Linux builds
permissions:
contents: read
packages: write
outputs:
build-status: ${{ steps.build-summary.outputs.build-status }}
image-name: ${{ steps.build-summary.outputs.image-name }}
image-digest: ${{ steps.build-summary.outputs.image-digest }}
cache-hit: ${{ steps.build-summary.outputs.cache-hit }}
steps:
- name: π Check workflow environment
run: |
set -euo pipefail # Exit immediately on any error
echo "π Checking workflow environment..."
echo "π Repository: ${{ github.repository }}"
echo "π Actor: ${{ github.actor }}"
echo "π Event: ${{ github.event_name }}"
echo "π Ref: ${{ github.ref }}"
echo "π SHA: ${{ github.sha }}"
echo "π Workflow: ${{ github.workflow }}"
echo "π Run ID: ${{ github.run_id }}"
echo "π Run Number: ${{ github.run_number }}"
# Check if we have the required permissions
echo "π Checking permissions..."
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
echo "β
Manual workflow dispatch - should have full permissions"
elif [ "${{ github.event_name }}" = "push" ]; then
echo "β
Push event - should have full permissions"
elif [ "${{ github.event_name }}" = "schedule" ]; then
echo "β
Scheduled event - should have full permissions"
else
echo "β οΈ Unknown event type: ${{ github.event_name }}"
fi
# Check if secrets are available
echo "π Checking secrets availability..."
if [ -n "${{ secrets.GITHUB_TOKEN }}" ]; then
echo "β
GITHUB_TOKEN is available"
else
echo "β GITHUB_TOKEN is not available"
exit 1
fi
echo "β
Environment check completed"
- name: π₯ Checkout repository
uses: actions/checkout@v6
- name: π§Ή Free up disk space
run: |
echo "π§Ή Freeing up disk space for large container build..."
echo "π Disk space before cleanup:"
df -h /
# Remove unnecessary packages and files
sudo rm -rf /usr/share/dotnet
sudo rm -rf /usr/local/lib/android
sudo rm -rf /opt/ghc
sudo rm -rf /opt/hostedtoolcache/CodeQL
sudo docker system prune -af
echo "π Disk space after cleanup:"
df -h /
echo "β
Disk cleanup complete"
- name: π³ Set up Docker Buildx
uses: docker/setup-buildx-action@v4
with:
driver: docker-container
driver-opts: |
network=host
buildkitd-flags: |
--allow-insecure-entitlement security.insecure
--allow-insecure-entitlement network.host
buildkitd-config-inline: |
[worker.oci]
max-parallelism = 1
[registry."docker.io"]
mirrors = ["mirror.gcr.io"]
- name: π Verify Buildx Builder
run: |
echo "π Checking buildx builder status..."
docker buildx ls
echo "π Inspecting builder..."
docker buildx inspect --bootstrap
echo "β
Buildx builder ready"
- name: π Log in to Container Registry
id: login
uses: docker/login-action@v4
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: π Check registry access
run: |
set -euo pipefail # Exit immediately on any error
echo "π Checking container registry access..."
echo "π Registry: ${{ env.REGISTRY }}"
echo "π Repository: ${{ github.repository }}"
echo "π Actor: ${{ github.actor }}"
echo "π Event: ${{ github.event_name }}"
# Test if we can access the registry
echo "π Testing Docker daemon access..."
if docker info >/dev/null 2>&1; then
echo "β
Docker daemon is accessible"
else
echo "β Docker daemon not accessible"
exit 1
fi
# Test registry login with detailed error checking
echo "π Testing container registry login..."
if echo "${{ secrets.GITHUB_TOKEN }}" | docker login ${{ env.REGISTRY }} -u ${{ github.actor }} --password-stdin; then
echo "β
Successfully logged into container registry"
else
echo "β Failed to log into container registry"
echo "π This could be due to:"
echo " - Missing GITHUB_TOKEN secret"
echo " - Insufficient permissions"
echo " - Registry access issues"
echo "π Checking GITHUB_TOKEN availability..."
if [ -n "${{ secrets.GITHUB_TOKEN }}" ]; then
echo "β
GITHUB_TOKEN is available"
else
echo "β GITHUB_TOKEN is empty or not available"
fi
exit 1
fi
# Verify we can actually push to the registry
echo "π Testing registry write permissions..."
TEST_IMAGE="${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}/test-push:latest"
if docker pull hello-world:latest >/dev/null 2>&1; then
if docker tag hello-world:latest $TEST_IMAGE >/dev/null 2>&1; then
if docker push $TEST_IMAGE >/dev/null 2>&1; then
echo "β
Registry write permissions confirmed"
# Clean up test image
docker rmi $TEST_IMAGE >/dev/null 2>&1 || true
else
echo "β Registry write permissions failed"
exit 1
fi
else
echo "β Failed to tag test image"
exit 1
fi
else
echo "β Failed to pull hello-world image for testing"
exit 1
fi
- name: π·οΈ Extract metadata
id: meta
uses: docker/metadata-action@v6
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=raw,value=${{ env.CONTAINER_TAG }}
- name: π³ Build Linux container
id: build
uses: docker/build-push-action@v7
with:
context: ${{ env.CONTEXT_PATH }}
file: ${{ env.DOCKERFILE_PATH }}
load: true # Keep local copy for testing
push: true # Also push to registry
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
no-cache: ${{ github.event_name == 'workflow_dispatch' && inputs.no_cache || false }} # Use cache unless explicitly disabled
platforms: ${{ env.PLATFORM }}
provenance: false # Disable provenance for better compatibility
sbom: false # Disable SBOM for better compatibility
cache-from: ${{ (github.event_name != 'workflow_dispatch' || !inputs.no_cache) && 'type=gha' || '' }}
cache-to: ${{ (github.event_name != 'workflow_dispatch' || !inputs.no_cache) && 'type=gha,mode=max' || '' }}
outputs: type=docker
- name: Build Complete
run: |
echo "β
Linux container build completed successfully!"
echo "π Container: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ env.CONTAINER_TAG }}"
- name: π Build Summary
id: build-summary
if: always()
run: |
# Determine build status
if [ "${{ steps.build.outcome }}" = "success" ]; then
BUILD_STATUS="success"
else
BUILD_STATUS="failure"
fi
# Extract build information
IMAGE_NAME="${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ env.CONTAINER_TAG }}"
IMAGE_DIGEST="${{ steps.build.outputs.digest }}"
CACHE_HIT="${{ steps.build.outputs.cache-hit }}"
echo "build-status=$BUILD_STATUS" >> $GITHUB_OUTPUT
echo "image-name=$IMAGE_NAME" >> $GITHUB_OUTPUT
echo "image-digest=$IMAGE_DIGEST" >> $GITHUB_OUTPUT
echo "cache-hit=$CACHE_HIT" >> $GITHUB_OUTPUT
echo "π Build Status: $BUILD_STATUS"
echo "π³ Image: $IMAGE_NAME"
echo "π Digest: $IMAGE_DIGEST"
echo "πΎ Cache Hit: $CACHE_HIT"