Repository navigation
190 lines (172 loc) · 7.77 KB
/
Copy pathmain.yml
File metadata and controls
190 lines (172 loc) · 7.77 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
on:
- push
- workflow_dispatch
jobs:
# Build, smoke-test and push one image per architecture, each on a native runner
# (no QEMU emulation). Images are pushed as <sha>-<arch>; the publish job below
# joins them into the tags that cs50.dev and students actually reference.
build:
name: build (${{ matrix.arch }})
strategy:
# Never let an arm64 failure cancel the amd64 build: GitHub Codespaces runs on amd64,
# so the amd64 image is the one deployments depend on. arm64 is additive.
fail-fast: false
matrix:
include:
- arch: amd64
runner: ubuntu-2604-64-cores-amd
- arch: arm64
runner: ubuntu-2604-64-cores-arm # Same larger runner cs50/cli builds on
runs-on: ${{ matrix.runner }}
# A normal build takes a few minutes; a hung leg must not hold up publishing the other one
timeout-minutes: 60
steps:
- name: Checkout
uses: actions/checkout@v7
# Keyed by branch as well as SHA: develop and canary (or main and canary) can build the same
# commit at the same time on different base images, and with SHA-only names the two runs
# overwrote each other's intermediates, so canary once published a develop (3.14) build.
# A / in a branch name is not valid in a tag, so it becomes -.
- name: Name the image
run: echo "IMAGE=cs50/codespace:$GITHUB_SHA-${GITHUB_REF_NAME//\//-}-${{ matrix.arch }}" >> "$GITHUB_ENV"
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v4
- name: Log into Docker Hub
uses: docker/login-action@v4
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Log into GitHub Packages
uses: docker/login-action@v4
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Setup Python
uses: actions/setup-python@v7
with:
python-version: "3.15"
- name: Only use `build` and `features`, if any, from devcontainer.json
run: |
pip3 install jsmin
python3 -c "import jsmin, json; f = open('devcontainer.json', 'r'); data = json.loads(jsmin.jsmin(f.read())); o = {}; o['build']=data['build']; o['features']=data.get('features', []); o['remoteEnv']=data['remoteEnv']; f.close(); f = open('devcontainer.json', 'w'); f.write(json.dumps(o, indent=4)); f.close()"
# TAG selects the cs50/cli base image: canary builds track cs50/cli:canary (a multi-arch
# manifest, so each runner gets its own architecture); everything else uses cs50/cli:<arch>.
- name: Build (${{ github.ref_name }}, ${{ matrix.arch }})
run: |
export TAG=${{ github.ref == 'refs/heads/canary' && 'canary' || matrix.arch }}
export VCS_REF=$(git rev-parse HEAD)
npm install -g @devcontainers/cli
devcontainer build --workspace-folder . --config devcontainer.json --image-name "$IMAGE"
- name: Squash (${{ github.ref_name }}, ${{ matrix.arch }})
if: ${{ github.ref != 'refs/heads/main' }}
run: |
pip3 install docker-squash
docker-squash --tag "$IMAGE" "$IMAGE"
# After squashing, so that the image tested is the image pushed
- name: Smoke-test (${{ github.ref_name }}, ${{ matrix.arch }})
run: |
bash tests/smoke.sh "$IMAGE"
- name: Push (${{ github.ref_name }}, ${{ matrix.arch }}) to GitHub Packages and Docker Hub
run: |
docker tag "$IMAGE" "ghcr.io/$IMAGE"
docker push "ghcr.io/$IMAGE"
docker push "$IMAGE"
# Join the per-architecture images into multi-arch tags. Runs even if a build leg failed:
# the amd64 image is required (deployments depend on it), arm64 is included when available.
publish:
needs: build
if: ${{ !cancelled() }}
runs-on: ubuntu-latest
steps:
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v4
- name: Log into Docker Hub
uses: docker/login-action@v4
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Log into GitHub Packages
uses: docker/login-action@v4
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
# Tags:
# main: <sha> and latest on GitHub Packages and Docker Hub
# others: <branch> on GitHub Packages; <branch> and <branch>-<sha> on Docker Hub
# A branch's SHA tag carries the branch name because the same commit on develop and canary
# builds on different base images; a bare <sha> would mean whichever run finished last.
# latest only moves if this commit is still the head of main, so re-running an older run
# (e.g. after an arm64 flake) can never roll cs50.dev back to an older image.
- name: Create multi-arch tags (${{ github.ref_name }})
id: publish
run: |
set -o errexit -o nounset -o pipefail
branch="${GITHUB_REF_NAME//\//-}"
if [ "$GITHUB_REF" = "refs/heads/main" ]; then
head=$(git ls-remote --exit-code "https://github.com/$GITHUB_REPOSITORY" refs/heads/main | cut -f1)
if [ "$head" = "$GITHUB_SHA" ]; then
latest=latest
echo "latest=true" >> "$GITHUB_OUTPUT"
else
echo "::warning::main has moved on to $head; publishing $GITHUB_SHA without moving latest"
latest=
fi
ghcr_tags="$GITHUB_SHA $latest"
hub_tags="$GITHUB_SHA $latest"
else
ghcr_tags="$branch"
hub_tags="$branch $branch-$GITHUB_SHA"
fi
# publish REPO TAG...: creates each REPO:TAG from whichever REPO:<sha>-<branch>-<arch> images this
# run's build jobs pushed
publish() {
local repo="$1"; shift
local sources=()
for arch in amd64 arm64; do
local source="$repo:$GITHUB_SHA-$branch-$arch"
if docker buildx imagetools inspect "$source" > /dev/null 2>&1; then
sources+=("$source")
elif [ "$arch" = "amd64" ]; then
echo "::error::$source was not published; not tagging $repo"
return 1
else
echo "::warning::$source was not published; $repo tags will be amd64-only"
fi
done
local tags=()
for tag in "$@"; do
tags+=(--tag "$repo:$tag")
done
docker buildx imagetools create "${tags[@]}" "${sources[@]}"
for tag in "$@"; do
docker buildx imagetools inspect "$repo:$tag"
done
}
publish ghcr.io/cs50/codespace $ghcr_tags
publish cs50/codespace $hub_tags
# cs50.dev resolves this git tag to the <sha> it serves as ghcr.io/cs50/codespace:<sha>,
# so it must only move after that image exists on GitHub Packages (the step above).
- name: Tag main as latest
if: ${{ github.ref == 'refs/heads/main' && steps.publish.outputs.latest == 'true' }}
uses: actions/github-script@v9
with:
github-token: ${{ github.token }}
script: |
try {
await github.rest.git.updateRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: "tags/latest",
sha: context.sha,
force: true
})
} catch (e) {
await github.rest.git.createRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: "refs/tags/latest",
sha: context.sha
})
}