-
Notifications
You must be signed in to change notification settings - Fork 0
296 lines (268 loc) · 11.2 KB
/
Copy pathdocker.yml
File metadata and controls
296 lines (268 loc) · 11.2 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
name: Build, Scan & Publish Docker Images
on:
# CI calls this reusable workflow only after its aggregate gate succeeds.
# Keeping publication in the same trusted push run avoids workflow_run's
# privileged-trigger boundary while preserving the all-tests-first gate.
workflow_call:
concurrency:
group: docker-release
# Let a release finish coherently. Cancelling during publication must never
# interrupt the final complete-release pointer update.
cancel-in-progress: false
env:
REGISTRY: ghcr.io
IMAGE_PREFIX: ghcr.io/openmapx
permissions:
contents: read
jobs:
gate:
name: Verify current trusted main commit
runs-on: ubuntu-latest
timeout-minutes: 5
permissions:
contents: read
outputs:
sha: ${{ steps.verify.outputs.sha }}
steps:
- name: Require the current trusted main commit
id: verify
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
with:
script: |
if (context.eventName !== "push" || context.ref !== "refs/heads/main") {
core.setFailed("Refusing a release outside a trusted main push run");
return;
}
const candidate = context.sha;
const { data: main } = await github.rest.git.getRef({
...context.repo,
ref: "heads/main",
});
if (candidate !== main.object.sha) {
core.setFailed(
`Candidate ${candidate} is not the current main commit ${main.object.sha}`,
);
return;
}
core.setOutput("sha", candidate);
build:
name: Build and scan ${{ matrix.app }}
needs: gate
runs-on: ubuntu-latest
timeout-minutes: 60
permissions:
contents: read
packages: write
security-events: write
strategy:
fail-fast: false
matrix:
include:
- app: api
context: .
dockerfile: apps/api/Dockerfile
- app: web
context: .
dockerfile: apps/web/Dockerfile
- app: data-manager
context: .
dockerfile: services/data-manager/Dockerfile
- app: ops-agent
context: .
dockerfile: apps/ops-agent/Dockerfile
- app: transitous-runner
context: .
dockerfile: apps/transitous-runner/Dockerfile
- app: transitous-tools
context: services/motis/tools/transitous
dockerfile: services/motis/tools/transitous/Dockerfile
- app: docs
context: docs
dockerfile: docs/Dockerfile
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
ref: ${{ needs.gate.outputs.sha }}
persist-credentials: false
# Buildx startup pulls its BuildKit image from Docker Hub. Retry that
# infrastructure fetch once, but never soften build or scan failures.
- name: Set up Docker Buildx
id: buildx
continue-on-error: true
uses: docker/setup-buildx-action@37fe631027851001ddb9b187196cc803df7f5f0e # v4.3.0
- name: Retry Docker Buildx setup
if: steps.buildx.outcome == 'failure'
uses: docker/setup-buildx-action@37fe631027851001ddb9b187196cc803df7f5f0e # v4.3.0
- name: Log in to the candidate registry
uses: docker/login-action@dbcb813823bdd20940b903addbd779551569679f # v4
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ github.token }}
# Push content-addressed candidate blobs only. No SHA or latest tag is
# visible until all matrix legs have passed the mandatory scan below.
- name: Build untagged candidate by digest
id: build
uses: docker/build-push-action@53b7df96c91f9c12dcc8a07bcb9ccacbed38856a # v7
with:
context: ${{ matrix.context }}
file: ${{ matrix.dockerfile }}
platforms: linux/amd64
build-args: ${{ matrix.app == 'web' && format('SW_BUILD_ID={0}', needs.gate.outputs.sha) || '' }}
outputs: type=image,name=${{ env.IMAGE_PREFIX }}/${{ matrix.app }},push-by-digest=true,name-canonical=true,push=true
labels: |
org.opencontainers.image.revision=${{ needs.gate.outputs.sha }}
org.opencontainers.image.source=${{ github.server_url }}/${{ github.repository }}
cache-from: type=gha,scope=${{ matrix.app }}
cache-to: type=gha,mode=max,scope=${{ matrix.app }}
provenance: mode=max
sbom: true
# Keep the complete finding set for review, including vulnerabilities
# for which the distro or upstream project has not published a fix.
- name: Audit exact candidate digest with Trivy
uses: aquasecurity/trivy-action@ed142fd0673e97e23eac54620cfb913e5ce36c25 # v0.36.0
with:
image-ref: ${{ env.IMAGE_PREFIX }}/${{ matrix.app }}@${{ steps.build.outputs.digest }}
format: sarif
output: trivy-${{ matrix.app }}.sarif
scanners: vuln
vuln-type: os,library
severity: CRITICAL,HIGH
limit-severities-for-sarif: true
ignore-unfixed: "false"
exit-code: "0"
# This is the promotion policy, not merely reporting: any actionable
# HIGH/CRITICAL OS or library vulnerability blocks every image tag.
# Unfixed advisories stay visible in the SARIF audit above, but cannot
# permanently deadlock releases when no remediation exists.
- name: Gate exact candidate digest with Trivy
uses: aquasecurity/trivy-action@ed142fd0673e97e23eac54620cfb913e5ce36c25 # v0.36.0
with:
image-ref: ${{ env.IMAGE_PREFIX }}/${{ matrix.app }}@${{ steps.build.outputs.digest }}
format: table
scanners: vuln
vuln-type: os,library
severity: CRITICAL,HIGH
ignore-unfixed: "true"
trivyignores: .trivyignore.yaml
exit-code: "1"
# Code-scanning upload needs GHAS in private repositories. Reporting may
# be unavailable, but both scans above are unconditional and the
# actionable-finding gate is fail-closed.
- name: Upload Trivy results
if: always() && (github.event.repository.visibility == 'public' || vars.ENABLE_CODE_SCANNING == 'true')
uses: github/codeql-action/upload-sarif@db488ddef3bf6cb639b32c2e9a7c0a7ea8271d28 # v4.37.8
with:
sarif_file: trivy-${{ matrix.app }}.sarif
category: trivy-${{ matrix.app }}
- name: Export approved digest
env:
DIGEST: ${{ steps.build.outputs.digest }}
run: |
if [[ ! "$DIGEST" =~ ^sha256:[0-9a-f]{64}$ ]]; then
echo "Invalid image digest: $DIGEST" >&2
exit 1
fi
mkdir -p digest
touch "digest/${DIGEST#sha256:}"
- name: Preserve approved digest for aggregate promotion
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7
with:
name: docker-digest-${{ matrix.app }}-${{ github.run_id }}-${{ github.run_attempt }}
path: digest/*
if-no-files-found: error
retention-days: 1
promote:
name: Publish coherent release
needs: [gate, build]
runs-on: ubuntu-latest
timeout-minutes: 15
permissions:
contents: read
packages: write
steps:
- name: Download every approved digest
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8
with:
pattern: docker-digest-*-${{ github.run_id }}-${{ github.run_attempt }}
path: digests
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@37fe631027851001ddb9b187196cc803df7f5f0e # v4.3.0
- name: Log in for promotion
uses: docker/login-action@dbcb813823bdd20940b903addbd779551569679f # v4
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ github.token }}
# SHA tags make each approved image independently addressable. The one
# release-manifest pointer below is the atomic deployment contract.
- name: Promote every scanned digest to SHA and assemble the release manifest
env:
IMAGE_PREFIX: ${{ env.IMAGE_PREFIX }}
RELEASE_MANIFEST_IMAGE: ${{ env.IMAGE_PREFIX }}/release-manifest
RELEASE_SHA: ${{ needs.gate.outputs.sha }}
run: |
mkdir -p release-manifest
first=true
image_lines=""
for app in api web data-manager ops-agent transitous-runner transitous-tools docs; do
shopt -s nullglob
digest_files=("digests/docker-digest-${app}-${GITHUB_RUN_ID}-${GITHUB_RUN_ATTEMPT}"/*)
if [ "${#digest_files[@]}" -ne 1 ] || [ ! -f "${digest_files[0]}" ]; then
echo "Expected exactly one approved digest artifact for ${app}" >&2
exit 1
fi
digest_file="${digest_files[0]}"
DIGEST="sha256:${digest_file##*/}"
if [[ ! "$DIGEST" =~ ^sha256:[0-9a-f]{64}$ ]]; then
echo "Invalid approved digest for ${app}: $DIGEST" >&2
exit 1
fi
IMAGE="${IMAGE_PREFIX}/${app}"
docker buildx imagetools create \
--tag "${IMAGE}:${RELEASE_SHA}" \
"${IMAGE}@${DIGEST}"
docker buildx imagetools inspect "${IMAGE}:${RELEASE_SHA}"
if [ "$first" = true ]; then
first=false
else
image_lines="${image_lines},\n"
fi
image_lines="${image_lines} \"${app}\": \"${IMAGE}@${DIGEST}\""
done
{
printf '{\n'
printf ' "schemaVersion": 1,\n'
printf ' "release": "%s",\n' "$RELEASE_SHA"
printf ' "images": {\n'
printf '%b\n' "$image_lines"
printf ' }\n'
printf '}\n'
} > release-manifest/release-manifest.json
cat > release-manifest/Dockerfile <<'EOF'
FROM scratch
COPY release-manifest.json /release-manifest.json
EOF
- name: Publish the immutable release manifest
env:
RELEASE_MANIFEST_IMAGE: ${{ env.IMAGE_PREFIX }}/release-manifest
RELEASE_SHA: ${{ needs.gate.outputs.sha }}
run: |
docker buildx build \
--file release-manifest/Dockerfile \
--tag "${RELEASE_MANIFEST_IMAGE}:${RELEASE_SHA}" \
--push \
release-manifest
docker buildx imagetools inspect "${RELEASE_MANIFEST_IMAGE}:${RELEASE_SHA}"
# This one registry tag is the atomic deployment switch. If building,
# scanning, any SHA promotion, or publishing the immutable manifest
# fails, it remains pointed at the last complete release.
- name: Advance the complete release pointer
env:
RELEASE_MANIFEST_IMAGE: ${{ env.IMAGE_PREFIX }}/release-manifest
RELEASE_SHA: ${{ needs.gate.outputs.sha }}
run: |
docker buildx imagetools create \
--tag "${RELEASE_MANIFEST_IMAGE}:latest" \
"${RELEASE_MANIFEST_IMAGE}:${RELEASE_SHA}"
docker buildx imagetools inspect "${RELEASE_MANIFEST_IMAGE}:latest"