Repository navigation
296 lines (261 loc) · 9.76 KB
/
Copy pathdocker-image.yml
File metadata and controls
296 lines (261 loc) · 9.76 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
# Builds the single-image application (FastAPI + built CRA frontend + Chromium)
# from deploy/Dockerfile with the repository root as context, and publishes it
# to ghcr.io/<owner>/webmonitor.
#
# Every publish is a multi-arch manifest (linux/amd64 + linux/arm64) assembled
# from per-arch images built on native runners. The image is large (~1.1 GB)
# because it ships Chromium, so a cold build takes several minutes per arch.
#
# Push vX.Y.Z to publish X.Y.Z and X.Y and create a GitHub Release. The newest
# stable release also moves latest; prereleases and older stable patches do not.
# Pull requests build the image without publishing it.
#
# This package is public and supports anonymous pulls. Visibility is managed in
# the package settings UI, separately from this publishing workflow.
name: docker-image
on:
push:
tags: ["v*"]
pull_request:
paths:
- ".github/workflows/docker-image.yml"
- "backend/**"
- "deploy/**"
- "frontend/**"
- "compose*.yaml"
- "pyproject.toml"
- "requirements.txt"
permissions:
contents: read
packages: write
concurrency:
group: docker-${{ github.ref }}
cancel-in-progress: true
env:
REGISTRY: ghcr.io
jobs:
verify:
name: verify tag matches versions
if: startsWith(github.ref, 'refs/tags/v')
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Compare tag with declared versions
env:
TAG: ${{ github.ref_name }}
run: |
set -eu
version="${TAG#v}"
case "$version" in
*[!0-9A-Za-z.-]*|'') echo "::error::tag $TAG is not v<semver>"; exit 1 ;;
esac
# Prereleases compare on the base version: v1.2.3-rc.1 still ships 1.2.3.
base="${version%%-*}"
fail=0
check() {
declared=$(sed -n "$2" "$1" | head -1)
if [ "$declared" != "$base" ]; then
echo "::error file=$1::$1 declares '$declared', tag says '$base'"
fail=1
fi
}
check pyproject.toml 's/^version = "\(.*\)"$/\1/p'
check backend/app/core/config.py 's/^ APP_VERSION: str = "\(.*\)"$/\1/p'
exit "$fail"
test:
name: backend and frontend tests
runs-on: ubuntu-latest
timeout-minutes: 30
env:
CI: "true"
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.12"
cache: pip
cache-dependency-path: requirements.txt
- name: Install backend dependencies
run: python -m pip install --require-hashes -r requirements.txt
- name: Run backend unit tests
run: python -m unittest discover -s tests -p 'test_*.py'
- name: Set up pnpm
uses: pnpm/action-setup@v4
with:
version: 10.34.8
run_install: false
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: "22"
cache: pnpm
cache-dependency-path: frontend/pnpm-lock.yaml
- name: Install frontend dependencies
working-directory: frontend
run: pnpm install --frozen-lockfile
- name: Test frontend
working-directory: frontend
run: pnpm test --watchAll=false
build:
name: build ${{ matrix.arch }}
needs: [verify, test]
if: always() && (needs.verify.result == 'success' || needs.verify.result == 'skipped') && needs.test.result == 'success'
runs-on: ${{ matrix.runner }}
timeout-minutes: 90
strategy:
fail-fast: false
matrix:
include:
- arch: amd64
runner: ubuntu-latest
platform: linux/amd64
- arch: arm64
runner: ubuntu-24.04-arm
platform: linux/arm64
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Compute image name
id: image
run: echo "name=$(printf '%s' '${{ github.repository }}' | tr '[:upper:]' '[:lower:]')" >> "$GITHUB_OUTPUT"
- name: Set up Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to GHCR
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v')
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push per-architecture image
uses: docker/build-push-action@v6
with:
context: .
file: deploy/Dockerfile
platforms: ${{ matrix.platform }}
push: ${{ github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v') }}
tags: ${{ env.REGISTRY }}/${{ steps.image.outputs.name }}:${{ github.sha }}-${{ matrix.arch }}
labels: |
org.opencontainers.image.source=${{ github.server_url }}/${{ github.repository }}
org.opencontainers.image.revision=${{ github.sha }}
org.opencontainers.image.version=${{ github.ref_name }}
cache-from: type=gha,scope=app-${{ matrix.arch }}
cache-to: type=gha,scope=app-${{ matrix.arch }},mode=max
publish:
name: publish
needs: [build, test]
if: always() && github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v') && needs.build.result == 'success' && needs.test.result == 'success'
runs-on: ubuntu-latest
steps:
- name: Checkout repository and release tags
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Check whether this is the newest stable release
id: latest
env:
TAG: ${{ github.ref_name }}
run: |
set -euo pipefail
python - <<'PY'
import os
import re
import subprocess
stable_tag = re.compile(r"^v(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)$")
tags = subprocess.check_output(["git", "tag", "--list", "v*"], text=True).splitlines()
stable_versions = [
tuple(int(part) for part in match.groups())
for tag in tags
if (match := stable_tag.fullmatch(tag)) is not None
]
current_match = stable_tag.fullmatch(os.environ["TAG"])
current = tuple(int(part) for part in current_match.groups()) if current_match else None
enabled = current is not None and all(current >= version for version in stable_versions)
with open(os.environ["GITHUB_OUTPUT"], "a", encoding="utf-8") as output:
output.write(f"enabled={str(enabled).lower()}\n")
print(f"Move latest: {str(enabled).lower()}")
PY
- name: Compute image name
id: image
run: echo "name=$(printf '%s' '${{ github.repository }}' | tr '[:upper:]' '[:lower:]')" >> "$GITHUB_OUTPUT"
- name: Set up Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to GHCR
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Compute tags
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ steps.image.outputs.name }}
# Older stable patches keep their full/minor tags without replacing latest.
flavor: |
latest=false
tags: |
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=raw,value=latest,enable=${{ steps.latest.outputs.enabled }}
- name: Assemble multi-arch manifest
shell: bash
env:
TAGS: ${{ steps.meta.outputs.tags }}
IMAGE: ${{ env.REGISTRY }}/${{ steps.image.outputs.name }}
SHA: ${{ github.sha }}
run: |
set -eu
tag_args=()
while read -r tag; do
[ -n "$tag" ] && tag_args+=(-t "$tag")
done <<< "$TAGS"
docker buildx imagetools create "${tag_args[@]}" "$IMAGE:$SHA-amd64" "$IMAGE:$SHA-arm64"
docker buildx imagetools inspect "$(printf '%s\n' "$TAGS" | head -1)"
release:
name: create GitHub release
needs: [publish]
if: startsWith(github.ref, 'refs/tags/v')
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Create release
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
TAG: ${{ github.ref_name }}
REPO: ${{ github.repository }}
run: |
set -eu
version="${TAG#v}"
notes=$(cat <<'EOF'
Public container image (linux/amd64 + linux/arm64):
```sh
docker pull ghcr.io/REPO:VERSION
```
No registry login or repository clone is required. Follow the README to
download `compose.yaml` and create your own `.env`. Set its image:
```dotenv
WEBMONITOR_IMAGE=ghcr.io/REPO:VERSION
```
Start from the deployment directory:
```sh
docker compose up -d --wait
```
Setup, configuration and data-retention limits: https://github.com/REPO#readme
EOF
)
repo_lower=$(printf '%s' "$REPO" | tr '[:upper:]' '[:lower:]')
notes=$(printf '%s' "$notes" | sed -e "s|REPO|$repo_lower|g" -e "s|VERSION|$version|g")
if gh release view "$TAG" >/dev/null 2>&1; then
echo "release $TAG already exists; leaving it untouched"
exit 0
fi
prerelease=""
case "$version" in *-*) prerelease="--prerelease" ;; esac
gh release create "$TAG" --generate-notes --title "$TAG" --notes "$notes" ${prerelease:+$prerelease}