-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
358 lines (328 loc) · 12.4 KB
/
Copy pathdocker.yml
File metadata and controls
358 lines (328 loc) · 12.4 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
# Builds and publishes the multi-arch Docker image
#
# Triggered by:
# - On git tag push, publishes to :X.Y.Z, :X.Y, and :latest
# - On manual trigger, rebuilds and updates :latest or tag if specified
# - On weekly cron, rebuilds :latest from master for upstream patches
#
# The workflow will:
# - Builds multi-arch (amd64, arm64, armv7) in parallel on native runners
# - Trivy scans + reports security issues, and fails cron on CRITICAL CVEs
# - Publishes to GHCR, and to Docker Hub if creds are configured
# - Attests both the build provenance and SBOM and publishes to GHCR
# - Uploads per-arch digests as artifacts, and writes a pretty job summary
name: 🐳 Docker
on:
workflow_dispatch:
inputs:
tag:
description: 'Tag to build (empty = build current ref as :latest. tag must exist in git)'
required: false
default: ''
push:
tags: ['*.*.*']
schedule:
- cron: '0 4 * * 0'
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}-${{ inputs.tag }}
cancel-in-progress: false
permissions:
contents: read # least-privilege default; jobs elevate as needed
env:
DH_IMAGE: ${{ vars.DOCKER_REPO || 'lissy93/dashy' }}
GH_IMAGE: ghcr.io/${{ github.repository }}
jobs:
build:
name: 🔨 Build (${{ matrix.arch }})
timeout-minutes: 30
permissions:
contents: read # checkout
packages: write # push image by digest to GHCR
security-events: write # upload Trivy SARIF to code scanning
env:
DOCKER_BUILD_SUMMARY: "false"
DOCKER_BUILD_RECORD_UPLOAD: "false"
strategy:
fail-fast: false
matrix:
include:
- platform: linux/amd64
runner: ubuntu-latest
arch: amd64
- platform: linux/arm64
runner: ubuntu-24.04-arm
arch: arm64
- platform: linux/arm/v7
runner: ubuntu-latest
arch: armv7
runs-on: ${{ matrix.runner }}
steps:
- name: 🛎️ Checkout
uses: actions/checkout@v6
with:
ref: ${{ inputs.tag || github.ref }}
- name: 🏷️ Resolve build version
id: version
env:
INPUT_TAG: ${{ inputs.tag }}
EVENT_NAME: ${{ github.event_name }}
REF_NAME: ${{ github.ref_name }}
run: |
set -euo pipefail
if [ -n "$INPUT_TAG" ]; then
v="$INPUT_TAG"
elif [ "$EVENT_NAME" = "push" ]; then
v="$REF_NAME"
else
v="latest"
fi
echo "value=$v" >> "$GITHUB_OUTPUT"
echo "revision=$(git rev-parse HEAD)" >> "$GITHUB_OUTPUT"
- name: 🔧 Set up QEMU
if: matrix.arch == 'armv7'
uses: docker/setup-qemu-action@v4
with:
platforms: linux/arm/v7
- name: 🔧 Set up Buildx
uses: docker/setup-buildx-action@v4
- name: 🔑 Login to GHCR
uses: docker/login-action@v4
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: ⏱️ Capture build timestamp
id: timestamp
run: echo "iso=$(date -u +'%Y-%m-%dT%H:%M:%SZ')" >> "$GITHUB_OUTPUT"
- name: 🔨 Build image (load for scan)
uses: docker/build-push-action@v7
with:
context: .
file: ./Dockerfile
platforms: ${{ matrix.platform }}
cache-from: type=gha,scope=${{ matrix.arch }}
cache-to: type=gha,scope=${{ matrix.arch }},mode=max
load: true
tags: dashy-scan:${{ matrix.arch }}
provenance: false
build-args: |
VERSION=${{ steps.version.outputs.value }}
REVISION=${{ steps.version.outputs.revision }}
CREATED=${{ steps.timestamp.outputs.iso }}
- name: 🛡️ Trivy vulnerability scan
id: scan
uses: aquasecurity/trivy-action@v0.36.0
env:
TRIVY_DB_REPOSITORY: ghcr.io/aquasecurity/trivy-db:2
TRIVY_JAVA_DB_REPOSITORY: ghcr.io/aquasecurity/trivy-java-db:1
with:
version: v0.70.0
image-ref: dashy-scan:${{ matrix.arch }}
severity: CRITICAL
ignore-unfixed: true
exit-code: ${{ github.event_name == 'schedule' && '1' || '0' }}
vuln-type: 'os,library'
format: 'sarif'
output: 'trivy-${{ matrix.arch }}.sarif'
timeout: '10m'
# If CVEs found, print them in human readable table so i can read and address them
- name: 📋 List blocking CVEs (on scan failure)
if: always() && steps.scan.outcome == 'failure'
uses: aquasecurity/trivy-action@v0.36.0
env:
TRIVY_DB_REPOSITORY: ghcr.io/aquasecurity/trivy-db:2
TRIVY_JAVA_DB_REPOSITORY: ghcr.io/aquasecurity/trivy-java-db:1
with:
version: v0.70.0
image-ref: dashy-scan:${{ matrix.arch }}
severity: CRITICAL
ignore-unfixed: true
exit-code: '0'
vuln-type: 'os,library'
format: 'table'
timeout: '10m'
- name: 📤 Upload Trivy SARIF
if: always() && hashFiles(format('trivy-{0}.sarif', matrix.arch)) != ''
uses: github/codeql-action/upload-sarif@v4
with:
sarif_file: trivy-${{ matrix.arch }}.sarif
category: trivy-${{ matrix.arch }}
- name: 🚀 Push by digest
id: push
uses: docker/build-push-action@v7
with:
context: .
file: ./Dockerfile
platforms: ${{ matrix.platform }}
cache-from: type=gha,scope=${{ matrix.arch }}
outputs: type=image,name=${{ env.GH_IMAGE }},push-by-digest=true,name-canonical=true,push=true
provenance: false
build-args: |
VERSION=${{ steps.version.outputs.value }}
REVISION=${{ steps.version.outputs.revision }}
CREATED=${{ steps.timestamp.outputs.iso }}
- name: 🧬 Write digest
env:
DIGEST: ${{ steps.push.outputs.digest }}
DIGESTS_DIR: ${{ runner.temp }}/digests
ARCH: ${{ matrix.arch }}
run: |
mkdir -p "$DIGESTS_DIR"
echo "$DIGEST" > "$DIGESTS_DIR/$ARCH"
- name: 📤 Upload digest
uses: actions/upload-artifact@v7
with:
name: digest-${{ matrix.arch }}
path: ${{ runner.temp }}/digests/${{ matrix.arch }}
if-no-files-found: error
retention-days: 1
merge:
name: 🧩 Merge & Push Manifests
needs: build
timeout-minutes: 30
runs-on: ubuntu-latest
permissions:
contents: read # least-privilege baseline
packages: write # push manifest + attestations to GHCR
id-token: write # OIDC token for keyless attestation signing
attestations: write # write SBOM + provenance attestations
env:
HAS_DH: ${{ vars.DOCKER_USERNAME != '' && secrets.DOCKER_PASSWORD != '' }}
SEMVER_VALUE: ${{ inputs.tag || github.ref_name }}
SEMVER_ENABLE: ${{ github.event_name == 'push' || inputs.tag != '' }}
LATEST_ENABLE: ${{ inputs.tag == '' }}
steps:
- name: 📥 Download digests
uses: actions/download-artifact@v8
with:
path: ${{ runner.temp }}/digests
pattern: digest-*
merge-multiple: true
- name: 🔧 Set up Buildx
uses: docker/setup-buildx-action@v4
- name: 🔑 Login to GHCR
uses: docker/login-action@v4
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: 🔑 Login to Docker Hub
if: env.HAS_DH == 'true'
uses: docker/login-action@v4
with:
username: ${{ vars.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: 🗂️ Generate tags
id: meta
uses: docker/metadata-action@v6
with:
images: |
${{ env.GH_IMAGE }}
${{ env.HAS_DH == 'true' && env.DH_IMAGE || '' }}
tags: |
type=raw,value=latest,enable=${{ env.LATEST_ENABLE }}
type=semver,pattern={{version}},value=${{ env.SEMVER_VALUE }},enable=${{ env.SEMVER_ENABLE }}
type=semver,pattern={{major}}.{{minor}},value=${{ env.SEMVER_VALUE }},enable=${{ env.SEMVER_ENABLE }}
type=semver,pattern={{major}}.x,value=${{ env.SEMVER_VALUE }},enable=${{ env.SEMVER_ENABLE }}
flavor: |
latest=false
- name: 🧩 Create & push manifest
id: manifest
working-directory: ${{ runner.temp }}/digests
run: |
set -euo pipefail
TAGS=()
while IFS= read -r tag; do TAGS+=(-t "$tag"); done \
< <(jq -r '.tags[]' <<< "$DOCKER_METADATA_OUTPUT_JSON")
SOURCES=()
for f in *; do SOURCES+=("${GH_IMAGE}@$(cat "$f")"); done
docker buildx imagetools create "${TAGS[@]}" "${SOURCES[@]}"
PRIMARY=$(jq -r --arg img "$GH_IMAGE" \
'[.tags[] | select(startswith($img + ":"))] | first // empty' \
<<< "$DOCKER_METADATA_OUTPUT_JSON")
DIGEST=$(docker buildx imagetools inspect "$PRIMARY" --format '{{.Manifest.Digest}}')
echo "primary_tag=$PRIMARY" >> "$GITHUB_OUTPUT"
echo "digest=$DIGEST" >> "$GITHUB_OUTPUT"
- name: 🔐 Generate SBOM (SPDX)
uses: anchore/sbom-action@v0.24.0
with:
image: ${{ steps.manifest.outputs.primary_tag }}
format: spdx-json
output-file: sbom.spdx.json
upload-artifact: false
- name: 🪪 Attest SBOM
id: attest_sbom
uses: actions/attest@v4
continue-on-error: true
with:
subject-name: ${{ env.GH_IMAGE }}
subject-digest: ${{ steps.manifest.outputs.digest }}
sbom-path: sbom.spdx.json
push-to-registry: true
show-summary: false
- name: 🛡️ Attest build provenance
id: attest_provenance
uses: actions/attest-build-provenance@v4
continue-on-error: true
with:
subject-name: ${{ env.GH_IMAGE }}
subject-digest: ${{ steps.manifest.outputs.digest }}
push-to-registry: true
show-summary: false
- name: 📋 Job summary
if: always()
continue-on-error: true
env:
SBOM_OUTCOME: ${{ steps.attest_sbom.outcome }}
SBOM_URL: ${{ steps.attest_sbom.outputs.attestation-url }}
PROV_OUTCOME: ${{ steps.attest_provenance.outcome }}
PROV_URL: ${{ steps.attest_provenance.outputs.attestation-url }}
MANIFEST: ${{ steps.manifest.outputs.digest }}
PRIMARY: ${{ steps.manifest.outputs.primary_tag }}
TAGS_JSON: ${{ steps.meta.outputs.json }}
DIGESTS_DIR: ${{ runner.temp }}/digests
run: |
set -euo pipefail
repo="${PRIMARY%%:*}"
attest() {
case "$2" in
success) [ -n "$3" ] && echo "- ✅ $1 attested ([view]($3))" || echo "- ✅ $1 attested" ;;
failure) echo "- ⚠️ $1 attestation failed" ;;
*) echo "- ⏭️ $1 attestation skipped" ;;
esac
}
{
echo "## Attestations"
echo
attest "SBOM" "$SBOM_OUTCOME" "$SBOM_URL"
attest "Build provenance" "$PROV_OUTCOME" "$PROV_URL"
echo
echo "---"
echo
echo "## Build Info"
echo
echo "| Arch | Size | Layers | Digest |"
echo "|---|---|---|---|"
for arch in amd64 arm64 armv7; do
f="$DIGESTS_DIR/$arch"
[ -f "$f" ] || continue
digest=$(cat "$f")
raw=$(docker buildx imagetools inspect "$repo@$digest" --raw 2>/dev/null || echo '{}')
bytes=$(jq '[.layers[]?.size // 0] | add // 0' <<< "$raw")
size=$(numfmt --to=iec --suffix=B "$bytes" 2>/dev/null || echo "${bytes}B")
layers=$(jq '.layers // [] | length' <<< "$raw")
echo "| \`$arch\` | $size | $layers | \`$digest\` |"
done
echo
echo "---"
echo
echo "## Docker Image"
echo
echo "**Manifest:** \`$MANIFEST\`"
echo
echo "The following tags have been updated and published:"
echo
echo '```'
if [ -n "${TAGS_JSON:-}" ]; then jq -r '.tags[]?' <<< "$TAGS_JSON"; fi
echo '```'
} >> "$GITHUB_STEP_SUMMARY"