Repository navigation
389 lines (348 loc) · 15.1 KB
/
Copy pathci.yml
File metadata and controls
389 lines (348 loc) · 15.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
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
# Build, gate, publish, release, track.
#
# Deploys are pull-initiated: this workflow only publishes an image. Watchtower on the VPS notices the new `latest`
# digest and rolls the app replicas one at a time; nothing here can reach the host and no SSH key exists anywhere.
#
# Per push to main:
# ghcr.io/<owner>/<repo>:latest what Watchtower follows
# ghcr.io/<owner>/<repo>:X.Y.Z, :X.Y SemVer from conventional commits (scripts/next-version.sh)
# ghcr.io/<owner>/<repo>:sha-<short> immutable; what a rollback pins to
# git tag vX.Y.Z + GitHub Release generated notes list the commits since the previous release
#
# Pull requests run the same build, security checks and image build, and publish nothing.
#
# bricks: every brick added, built and removed on its own runner (not a gate)
name: ci
on:
push:
branches: [main]
pull_request:
concurrency:
# A newer commit supersedes an in-flight run of the same ref; never race two publishes.
group: ci-${{ github.ref }}
cancel-in-progress: true
permissions:
contents: read
env:
JAVA_VERSION: "25"
jobs:
build:
name: Build and test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
# next-version.sh and the commit lint need tags and history.
fetch-depth: 0
- uses: actions/setup-java@de7274f081f381c8f8158605e0321c36c376e2e6 # v6.0.1
with:
distribution: temurin
java-version: ${{ env.JAVA_VERSION }}
- uses: gradle/actions/setup-gradle@9c971963bec38e04b3d30dcc455b5382be2fdbfb # v6.3.0
with:
# Persist the local build cache: a docs-only commit skips compile and tests entirely.
cache-read-only: ${{ github.ref != 'refs/heads/main' }}
- name: Lint commit subjects
# Pull requests: every commit the PR adds. Pushes: only the commits of this push, so history inherited from
# the template (GitHub's "Initial commit") can never block a release.
env:
BASE_SHA: ${{ github.event.pull_request.base.sha }}
BEFORE: ${{ github.event.before }}
run: |
if [ -n "$BASE_SHA" ]; then
scripts/lint-commits.sh "${BASE_SHA}..HEAD"
else
scripts/lint-commits.sh --push-range "${BEFORE}" "${GITHUB_SHA}"
fi
- name: Script tests
run: scripts/tests/run.sh
- name: Build, unit and integration tests, coverage gate
# Docker is available on ubuntu-latest, so Testcontainers works out of the box.
run: ./gradlew build --no-daemon
- uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
name: jar
path: services/app/build/libs/app.jar
retention-days: 3
- name: Upload test and coverage reports
if: ${{ !cancelled() }}
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
name: reports
path: |
services/app/build/reports/tests/
services/app/build/reports/jacoco/
if-no-files-found: ignore
# Fast security lane: runs in parallel with the build and takes about a minute, so it adds nothing to the time from
# push to live. The slow scans (image CVEs against what is actually running) live in security-weekly.yml.
security-fast:
name: Secrets, dependency CVEs, workflow lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
fetch-depth: 0
- name: gitleaks (secrets anywhere in history)
run: |
docker run --rm -v "$PWD:/repo" ghcr.io/gitleaks/gitleaks:v8.30.1 \
detect --source /repo --no-banner --redact --exit-code 1
- name: trivy (lockfile, Dockerfile, compose, secrets)
uses: aquasecurity/trivy-action@ed142fd0673e97e23eac54620cfb913e5ce36c25 # v0.36.0
with:
scan-type: fs
scan-ref: .
skip-dirs: "**/build,**/.gradle"
scanners: vuln,secret,misconfig
severity: HIGH,CRITICAL
exit-code: "1"
ignore-unfixed: true
trivyignores: .trivyignore
- uses: astral-sh/setup-uv@c18668ad3cf93ea998bef934396af7bb5c839dc7 # v10.2.0
- name: zizmor (workflow misconfiguration)
run: uvx [email protected] --min-severity medium .github/workflows
# Bricks are inert until enabled, so the plain build cannot catch a brick that rotted. Each one is added on a
# clean checkout, built with its tests, and removed again; nothing here gates the image, so push → live stays fast.
bricks:
name: Brick ${{ matrix.brick }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
brick: [observability, backup, minio]
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- uses: actions/setup-java@de7274f081f381c8f8158605e0321c36c376e2e6 # v6.0.1
with:
distribution: temurin
java-version: ${{ env.JAVA_VERSION }}
- uses: gradle/actions/setup-gradle@9c971963bec38e04b3d30dcc455b5382be2fdbfb # v6.3.0
with:
cache-read-only: true
- name: Skip bricks this repository has enabled
id: enabled
env:
BRICK: ${{ matrix.brick }}
run: |
if grep -qx "$BRICK" .bricks 2>/dev/null; then
echo "::notice::brick $BRICK is enabled in this repository; the build job already covers it"
echo "skip=true" >> "$GITHUB_OUTPUT"
fi
- name: Add the brick
if: steps.enabled.outputs.skip != 'true'
env:
BRICK: ${{ matrix.brick }}
run: scripts/brick.sh add "$BRICK"
- name: Build with the brick
if: steps.enabled.outputs.skip != 'true'
run: ./gradlew build --no-daemon
- name: Script tests and compose render with the brick
if: steps.enabled.outputs.skip != 'true'
run: |
scripts/tests/run.sh
make config
make prod-config PROD_ENV_FILE=scripts/tests/fixtures/env.prod.test
- name: Remove the brick and prove nothing is left
if: steps.enabled.outputs.skip != 'true'
env:
BRICK: ${{ matrix.brick }}
run: |
scripts/brick.sh remove "$BRICK"
git status --porcelain
test -z "$(git status --porcelain)"
image:
name: Build image
needs: [build, security-fast]
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
outputs:
version: ${{ steps.ver.outputs.version }}
minor: ${{ steps.ver.outputs.minor }}
revision: ${{ github.sha }}
image: ${{ steps.ver.outputs.image }}
digest: ${{ steps.push.outputs.digest }}
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
fetch-depth: 0
- uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
with:
name: jar
path: services/app/build/libs/
- name: Derive version and image name
id: ver
env:
REPO: ${{ github.repository }}
run: |
version="$(scripts/next-version.sh)"
{
echo "version=${version}"
echo "minor=${version%.*}"
# GHCR rejects uppercase paths; GitHub owners may have them.
echo "image=ghcr.io/${REPO,,}"
} >> "$GITHUB_OUTPUT"
echo "next release: v${version}"
# Multi-arch without emulating the compiler: the jar is portable, only the JRE layer differs per platform.
- uses: docker/setup-qemu-action@99012661954931238ded8c8b007157a8430204e1 # v4.4.0
- uses: docker/setup-buildx-action@f87e5991a6d7451dcb8d9637bfbc97413f497069 # v4.4.1
- name: Log in to GHCR
if: github.event_name == 'push'
uses: docker/login-action@dbcb813823bdd20940b903addbd779551569679f # v4.6.0
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Tags and labels
id: meta
uses: docker/metadata-action@dc802804100637a589fabce1cb79ff13a1411302 # v6.2.0
with:
images: ${{ steps.ver.outputs.image }}
tags: |
type=raw,value=latest,enable={{is_default_branch}}
type=raw,value=${{ steps.ver.outputs.version }},enable={{is_default_branch}}
type=raw,value=${{ steps.ver.outputs.minor }},enable={{is_default_branch}}
type=sha,prefix=sha-,format=short
labels: |
org.opencontainers.image.version=${{ steps.ver.outputs.version }}
- name: Build and push
id: push
uses: docker/build-push-action@c3c9e263c25d99ce0380d002d59b67737d91b0dc # v7.4.0
with:
context: .
file: services/app/Dockerfile
platforms: linux/amd64,linux/arm64
build-args: |
APP_VERSION=${{ steps.ver.outputs.version }}
APP_REVISION=${{ github.sha }}
# Pull requests validate the Dockerfile without publishing anything.
push: ${{ github.event_name == 'push' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
provenance: false
release:
name: Tag and release
needs: image
if: github.event_name == 'push'
runs-on: ubuntu-latest
permissions:
contents: write
steps:
# Credentials stay: this job pushes the release tag with the job's own contents:write token.
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 # zizmor: ignore[artipacked]
with:
fetch-depth: 0
- name: Push tag
env:
VERSION: ${{ needs.image.outputs.version }}
run: |
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git tag -a "v${VERSION}" -m "Release v${VERSION}"
git push origin "v${VERSION}"
- name: Previous release tag
id: prev
run: |
# Image tags carry no "v": what the release notes print is what APP_IMAGE_TAG takes.
prev="$(git tag -l 'v[0-9]*' --sort=-v:refname | sed -n '2p' | sed 's/^v//')"
echo "tag=${prev}" >> "$GITHUB_OUTPUT"
- uses: softprops/action-gh-release@efb35369e0ad2afab669f228072c1b0d510eae64 # v3.0.3
with:
tag_name: v${{ needs.image.outputs.version }}
name: v${{ needs.image.outputs.version }}
generate_release_notes: true
body: |
**Image:** `${{ needs.image.outputs.image }}:${{ needs.image.outputs.version }}`
**Digest:** `${{ needs.image.outputs.digest }}`
**Revision:** `${{ github.sha }}`
Watchtower on the VPS picks this up within a minute. To roll back to the previous release, on the VPS:
```bash
make prod-rollback TAG=${{ steps.prev.outputs.tag != '' && steps.prev.outputs.tag || 'sha-xxxxxxx' }}
```
See docs/deployment.md §5 for when a schema change makes a rollback unsafe.
# Publication is not deployment. The host pulls on its own schedule, and a stalled Watchtower is the known failure
# mode of that design, so the GitHub Deployment is resolved only by observing the host serving the new revision.
# Enable by setting the repository variable PRODUCTION_URL (https://your.domain); without it this job skips.
deploy:
name: Track rollout
needs: [image, release]
if: github.event_name == 'push'
runs-on: ubuntu-latest
permissions:
deployments: write
steps:
- name: Is a production host configured?
id: cfg
env:
PRODUCTION_URL: ${{ vars.PRODUCTION_URL }}
run: |
if [ -z "${PRODUCTION_URL}" ]; then
echo "::notice::PRODUCTION_URL repository variable not set; skipping rollout tracking."
echo "configured=false" >> "$GITHUB_OUTPUT"
else
echo "configured=true" >> "$GITHUB_OUTPUT"
fi
- name: Open deployment
if: steps.cfg.outputs.configured == 'true'
id: deployment
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
env:
PRODUCTION_URL: ${{ vars.PRODUCTION_URL }}
with:
script: |
const { data } = await github.rest.repos.createDeployment({
owner: context.repo.owner, repo: context.repo.repo, ref: context.sha,
environment: 'production', description: 'Published; waiting for the host to pull it',
auto_merge: false, required_contexts: [],
});
await github.rest.repos.createDeploymentStatus({
owner: context.repo.owner, repo: context.repo.repo, deployment_id: data.id,
state: 'in_progress', environment_url: process.env.PRODUCTION_URL,
});
core.setOutput('id', data.id);
- name: Wait for every replica to serve the new revision
if: steps.cfg.outputs.configured == 'true'
id: wait
env:
PRODUCTION_URL: ${{ vars.PRODUCTION_URL }}
EXPECTED: ${{ github.sha }}
TIMEOUT_SECONDS: "900"
run: |
deadline=$(( $(date +%s) + TIMEOUT_SECONDS ))
while [ "$(date +%s)" -lt "$deadline" ]; do
ok=true; seen=""
# Several samples per round: while a rollout is in flight one replica still answers with the old revision.
for _ in 1 2 3 4; do
got="$(curl -fsS --max-time 10 "${PRODUCTION_URL%/}/version" | jq -r .revision 2>/dev/null || echo "")"
seen="${seen} ${got:-?}"
[ "$got" = "$EXPECTED" ] || ok=false
done
if [ "$ok" = true ]; then echo "host serves ${EXPECTED}"; exit 0; fi
echo "waiting; saw:${seen}"
sleep 30
done
echo "::error::host did not report ${EXPECTED} within ${TIMEOUT_SECONDS}s. The image is published; check Watchtower: make prod-logs SERVICE=watchtower"
exit 1
- name: Close deployment
if: always() && steps.cfg.outputs.configured == 'true' && steps.deployment.outputs.id != ''
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
env:
PRODUCTION_URL: ${{ vars.PRODUCTION_URL }}
OUTCOME: ${{ steps.wait.outcome }}
DEPLOYMENT_ID: ${{ steps.deployment.outputs.id }}
with:
script: |
const ok = process.env.OUTCOME === 'success';
await github.rest.repos.createDeploymentStatus({
owner: context.repo.owner, repo: context.repo.repo,
deployment_id: Number(process.env.DEPLOYMENT_ID),
state: ok ? 'success' : 'failure', environment_url: process.env.PRODUCTION_URL,
description: ok ? 'Host is serving this revision' : 'Published, but the host did not pick it up in time',
});