-
Notifications
You must be signed in to change notification settings - Fork 6
358 lines (318 loc) · 11.6 KB
/
docker.yml
File metadata and controls
358 lines (318 loc) · 11.6 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
name: Docker
on:
workflow_call:
inputs:
tag:
required: true
type: string
platforms:
required: true
type: string
assert:
type: boolean
default: false
flow_name:
required: true
type: string
trail_name:
required: true
type: string
kosli_org:
required: true
type: string
checkout_ref:
required: false
type: string
default: ''
report_to_kosli:
required: false
type: string
default: 'none'
secrets:
slack_channel:
required: true
slack_webhook:
required: true
kosli_api_token:
required: true
snyk_token:
required: true
env:
IMAGE: ghcr.io/kosli-dev/cli
# KOSLI_DRY_RUN: "True"
# Ordinarily we declare KOSLI_ORG and KOSLI_FLOW here but
# this interferes with the CLI integration tests
jobs:
# ---------------------------------------------------------------------------
# Job 1: Resolve the platforms input into a JSON matrix.
#
# Why a separate job: workflow-level matrix entries must be static or come
# from job outputs; they cannot be computed inline from an input string.
#
# To support a new platform, add one entry to RUNNER_MAP below.
# ---------------------------------------------------------------------------
prepare:
name: Prepare build matrix
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.set-matrix.outputs.matrix }}
steps:
- name: Harden Runner
uses: step-security/harden-runner@fe104658747b27e96e4f7e80cd0a94068e53901d # v2.16.1
with:
egress-policy: audit
- name: Build matrix JSON from platforms input
id: set-matrix
env:
PLATFORMS: ${{ inputs.platforms }}
RUNNER_MAP: |
{
"linux/amd64": "ubuntu-latest",
"linux/arm64": "ubuntu-24.04-arm"
}
run: |
MATRIX=$(echo "$PLATFORMS" \
| tr ',' '\n' \
| jq -Rsc --argjson map "$RUNNER_MAP" '
split("\n") |
map(select(length > 0) | ltrimstr(" ") | rtrimstr(" ")) |
map(
. as $p |
if ($map | has($p)) then {
platform: $p,
runner: $map[$p],
slug: ($p | gsub("/"; "-"))
} else
error("Unsupported platform \($p). Add it to RUNNER_MAP in docker.yml.")
end
)
')
echo "matrix=$MATRIX" >> $GITHUB_OUTPUT
# ---------------------------------------------------------------------------
# Job 2: Build each platform on its native runner, push by digest.
#
# Native runners: building linux/arm64 via QEMU on an amd64 runner is
# 5-10x slower than running it natively. Each leg pushes its image by digest
# (not by tag) so the two images live in the registry without overwriting
# each other. Digests are passed to the merge job via artifacts.
# ---------------------------------------------------------------------------
build:
name: Docker Build (${{ matrix.platform }})
needs: [prepare]
runs-on: ${{ matrix.runner }}
permissions:
id-token: write
contents: read
packages: write
strategy:
fail-fast: false
matrix:
include: ${{ fromJson(needs.prepare.outputs.matrix) }}
steps:
- name: Harden Runner
uses: step-security/harden-runner@fe104658747b27e96e4f7e80cd0a94068e53901d # v2.16.1
with:
egress-policy: audit
- uses: actions/checkout@v6
with:
fetch-depth: 3
ref: ${{ inputs.checkout_ref || github.sha }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v4
# No QEMU needed — each runner is already native for its platform
- name: Login to GitHub Container Registry
uses: docker/login-action@v4
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push by digest
id: build
uses: docker/build-push-action@v7
with:
context: .
# Push without a tag — the merge job creates the final manifest.
platforms: ${{ matrix.platform }}
outputs: type=image,name=${{ env.IMAGE }},push-by-digest=true,name-canonical=true,push=true
# Per-arch cache keeps amd64 and arm64 independent so a change on
# one arch doesn't bust the other's cache.
cache-from: type=registry,ref=${{ env.IMAGE }}:buildcache-${{ matrix.slug }}
cache-to: type=registry,ref=${{ env.IMAGE }}:buildcache-${{ matrix.slug }},mode=max
- name: Export digest to file
run: |
if [ -z "$DIGEST" ]; then echo "::error::Build digest is empty"; exit 1; fi
mkdir -p /tmp/digests
touch "/tmp/digests/${DIGEST#sha256:}"
env:
DIGEST: ${{ steps.build.outputs.digest }}
- name: Upload digest artifact
uses: actions/upload-artifact@v7
with:
name: digest-${{ matrix.slug }}
path: /tmp/digests/*
if-no-files-found: error
retention-days: 1
# ---------------------------------------------------------------------------
# Job 3: Merge per-arch digests into a single manifest, then run all
# post-build steps (SBOM, Snyk, Kosli attestation, smoke test) once.
# ---------------------------------------------------------------------------
merge:
name: Docker Merge & Attest
runs-on: ubuntu-latest
needs: [build]
permissions:
id-token: write
contents: write
attestations: write
packages: write
artifact-metadata: write
steps:
- name: Harden Runner
uses: step-security/harden-runner@fe104658747b27e96e4f7e80cd0a94068e53901d # v2.16.1
with:
egress-policy: audit
- uses: actions/checkout@v6
with:
fetch-depth: 3
ref: ${{ inputs.checkout_ref || github.sha }}
- uses: actions/setup-go@v6
with:
go-version-file: '.go-version'
check-latest: true
- name: Download all digest artifacts
uses: actions/download-artifact@v7
with:
path: /tmp/digests
pattern: digest-*
merge-multiple: true
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v4
- name: Login to GitHub Container Registry
uses: docker/login-action@v4
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: setup-kosli-cli
uses: kosli-dev/setup-cli-action@v3
with:
version: ${{ vars.KOSLI_CLI_VERSION }}
- name: Create manifest and push
id: docker_build
env:
IMAGE: ${{ env.IMAGE }}
TAG: ${{ inputs.tag }}
run: |
# Read only files that look like sha256 hex digests — guards against
# any stray files that might land in /tmp/digests unexpectedly.
DIGEST_ARGS=$(find /tmp/digests -maxdepth 1 -type f \
| grep -E '/[0-9a-f]{64}$' \
| xargs -I{} basename {} \
| sed "s|^|${IMAGE}@sha256:|" \
| xargs)
if [ -z "$DIGEST_ARGS" ]; then
echo "::error::No valid digest files found in /tmp/digests"
exit 1
fi
docker buildx imagetools create \
--tag "${IMAGE}:${TAG}" \
${DIGEST_ARGS}
DIGEST=$(docker buildx imagetools inspect "${IMAGE}:${TAG}" \
--format '{{json .Manifest.Digest}}' | tr -d '"')
echo "digest=${DIGEST}" >> $GITHUB_OUTPUT
- name: Make the image fingerprint available for following steps
env:
BUILD_DIGEST: ${{ steps.docker_build.outputs.digest }}
run: |
ARTIFACT_SHA=$(echo "${BUILD_DIGEST}" | sed 's/.*://')
echo "FINGERPRINT=$ARTIFACT_SHA" >> ${GITHUB_ENV}
- name: Attest Build Provenance
uses: actions/attest@v4
with:
subject-name: ${{ env.IMAGE }}
subject-digest: ${{ steps.docker_build.outputs.digest }}
push-to-registry: true
- name: Generate SBOM for the docker image
uses: anchore/sbom-action@v0
with:
image: ${{ env.IMAGE }}:${{ inputs.tag }}
format: 'spdx-json'
output-file: 'sbom.spdx.json'
upload-artifact: false
- name: Attest SBOM to Github
uses: actions/attest@v4
with:
sbom-path: 'sbom.spdx.json'
subject-name: ${{ env.IMAGE }}
subject-digest: ${{ steps.docker_build.outputs.digest }}
push-to-registry: true
- name: setup Snyk
uses: snyk/actions/setup@master
- name: Report Docker image to Kosli
if: ${{ inputs.report_to_kosli != 'none' }}
env:
KOSLI_API_TOKEN: ${{ secrets.kosli_api_token }}
run:
kosli attest artifact
${{ env.IMAGE }}:${{ inputs.tag }}
--flow ${{ inputs.flow_name }}
--trail ${{ inputs.trail_name }}
--name cli-docker
--fingerprint ${{ env.FINGERPRINT }}
--external-url sigstore=https://search.sigstore.dev/?hash=${{ env.FINGERPRINT }}
--org ${{ inputs.kosli_org }}
- name: Report SBOM to Kosli
if: ${{ inputs.report_to_kosli != 'none' }}
env:
KOSLI_API_TOKEN: ${{ secrets.kosli_api_token }}
run:
kosli attest generic
--flow ${{ inputs.flow_name }}
--trail ${{ inputs.trail_name }}
--name container-sbom
--fingerprint ${{ env.FINGERPRINT }}
--attachments sbom.spdx.json
--org ${{ inputs.kosli_org }}
- name: Run Snyk Container Test to scan the Docker image for vulnerabilities
env:
SNYK_TOKEN: ${{ secrets.snyk_token }}
run:
snyk container test ${{ env.IMAGE }}:${{ inputs.tag }}
--file=Dockerfile
--sarif
--policy-path=.snyk
--sarif-file-output=snyk-docker.json
- name: Report Snyk Docker scan results attestation to Kosli
if: ${{ inputs.report_to_kosli != 'none' && (success() || failure()) }}
env:
KOSLI_API_TOKEN: ${{ secrets.kosli_api_token }}
run:
kosli attest snyk
--flow ${{ inputs.flow_name }}
--trail ${{ inputs.trail_name }}
--fingerprint ${{ env.FINGERPRINT }}
--name snyk-container-test
--scan-results snyk-docker.json
--org ${{ inputs.kosli_org }}
- name: Smoke test the docker image to be sure it can connect to Kosli
id: smoke-test
env:
KOSLI_ORG: cyber-dojo
KOSLI_API_TOKEN: any-token-will-do
run:
docker run -e KOSLI_API_TOKEN
-e KOSLI_ORG --rm ${{ env.IMAGE }}:${{ inputs.tag }}
list environments
- name: Report Docker smoke test attestation to Kosli
if: ${{ inputs.report_to_kosli != 'none' && (success() || failure()) }}
env:
KOSLI_API_TOKEN: ${{ secrets.kosli_api_token }}
SMOKE_TEST_OUTCOME: ${{ steps.smoke-test.outcome }}
run:
kosli attest generic
--flow ${{ inputs.flow_name }}
--trail ${{ inputs.trail_name }}
--fingerprint ${{ env.FINGERPRINT }}
--name smoke-test
--compliant=${{ steps.smoke-test.outcome == 'success' }}
--org ${{ inputs.kosli_org }}