-
Notifications
You must be signed in to change notification settings - Fork 0
272 lines (253 loc) · 9.67 KB
/
docker-build-push.yml
File metadata and controls
272 lines (253 loc) · 9.67 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
name: 'Docker Build, Scan, Sign & Push'
on:
workflow_call:
inputs:
context:
description: 'Build context path'
required: false
type: string
default: '.'
dockerfile:
description: 'Path to Dockerfile'
required: false
type: string
default: './Dockerfile'
image:
description: 'Image name (without registry/tag)'
required: true
type: string
registry:
description: 'Container registry URL'
required: false
type: string
default: 'ghcr.io'
platforms:
description: 'Target platforms (comma-separated)'
required: false
type: string
default: 'linux/amd64'
push:
description: 'Push image to registry'
required: false
type: boolean
default: true
scan:
description: 'Run Trivy vulnerability scan'
required: false
type: boolean
default: true
sign:
description: 'Sign image with Cosign (keyless OIDC)'
required: false
type: boolean
default: true
sbom:
description: 'Generate SBOM (Software Bill of Materials)'
required: false
type: boolean
default: true
cache-registry:
description: 'Use registry cache for faster builds'
required: false
type: boolean
default: true
build-args:
description: 'Build arguments (KEY=VALUE, one per line)'
required: false
type: string
default: ''
severity:
description: 'Minimum vulnerability severity to report'
required: false
type: string
default: 'HIGH,CRITICAL'
upload-sarif:
description: 'Upload SARIF to GitHub Security (requires Code Scanning enabled)'
required: false
type: boolean
default: false
runs-on:
description: 'Runner label to execute the job on'
required: false
type: string
default: 'homelab-runners'
secrets:
registry-username:
description: 'Registry username'
required: false
registry-password:
description: 'Registry password/token'
required: false
outputs:
digest:
description: 'Image digest (sha256:...)'
value: ${{ jobs.build.outputs.digest }}
tags:
description: 'Image tags (newline-separated)'
value: ${{ jobs.build.outputs.tags }}
sbom-path:
description: 'Path to SBOM artifact'
value: ${{ jobs.build.outputs.sbom-path }}
permissions:
contents: read
packages: write
id-token: write
security-events: write
actions: write # Required for artifact upload in SBOM generation
jobs:
build:
name: Build & Push Docker Image
runs-on: ${{ inputs.runs-on }}
timeout-minutes: 30
outputs:
digest: ${{ steps.build.outputs.digest }}
tags: ${{ steps.meta.outputs.tags }}
sbom-path: ${{ steps.sbom.outputs.path }}
steps:
- name: Checkout repository
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@c47758b77c9736f4b2ef4073d4d51994fabfe349 # v3.7.1
with:
driver-opts: |
image=moby/buildkit:latest
network=host
- name: Log in to container registry
if: inputs.push
uses: docker/login-action@9780b0c442fbb1117ed29e0efdff1e18412f7567 # v3.3.0
with:
registry: ${{ inputs.registry }}
username: ${{ secrets.registry-username || github.actor }}
password: ${{ secrets.registry-password || secrets.GITHUB_TOKEN }}
- name: Extract Docker metadata
id: meta
uses: docker/metadata-action@8e5442c4ef9f78752691e2d8f8d19755c6f78e81 # v5.5.1
with:
images: ${{ inputs.registry }}/${{ github.repository_owner }}/${{ inputs.image }}
tags: |
type=sha,prefix=sha-,format=short
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}
type=raw,value=latest,enable={{is_default_branch}}
labels: |
org.opencontainers.image.title=${{ inputs.image }}
org.opencontainers.image.description=Built from ${{ github.repository }}
org.opencontainers.image.vendor=${{ github.repository_owner }}
- name: Parse build arguments
id: build-args
if: inputs.build-args != ''
shell: bash
run: |
{
echo "args<<EOF"
echo "${{ inputs.build-args }}"
echo "EOF"
} >> "$GITHUB_OUTPUT"
- name: Build and push Docker image
id: build
uses: docker/build-push-action@4f58ea79222b3b9dc2c8bbdd6debcef730109a75 # v6.9.0
with:
context: ${{ inputs.context }}
file: ${{ inputs.dockerfile }}
platforms: ${{ inputs.platforms }}
push: ${{ inputs.push }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
build-args: ${{ steps.build-args.outputs.args }}
cache-from: ${{ inputs.cache-registry && format('type=registry,ref={0}/{1}/{2}:buildcache', inputs.registry, github.repository_owner, inputs.image) || 'type=gha' }}
cache-to: ${{ inputs.cache-registry && format('type=registry,ref={0}/{1}/{2}:buildcache,mode=max', inputs.registry, github.repository_owner, inputs.image) || 'type=gha,mode=max' }}
provenance: true
sbom: false
- name: Run Trivy vulnerability scanner (table output)
if: inputs.scan
uses: aquasecurity/trivy-action@57a97c7e7821a5776cebc9bb87c984fa69cba8f1 # v0.35.0
with:
image-ref: ${{ inputs.registry }}/${{ github.repository_owner }}/${{ inputs.image }}@${{ steps.build.outputs.digest }}
format: 'table'
severity: ${{ inputs.severity }}
timeout: '10m'
exit-code: '0' # Don't fail on vulnerabilities, just report
version: 'v0.69.3'
- name: Run Trivy vulnerability scanner (SARIF output)
if: inputs.scan && inputs.upload-sarif
uses: aquasecurity/trivy-action@57a97c7e7821a5776cebc9bb87c984fa69cba8f1 # v0.35.0
with:
image-ref: ${{ inputs.registry }}/${{ github.repository_owner }}/${{ inputs.image }}@${{ steps.build.outputs.digest }}
format: 'sarif'
output: 'trivy-results.sarif'
severity: ${{ inputs.severity }}
timeout: '10m'
version: 'v0.69.3'
- name: Upload Trivy scan results to GitHub Security
if: inputs.scan && inputs.upload-sarif
uses: github/codeql-action/upload-sarif@48ab28a6f5dbc2a99bf1e0131198dd8f1df78169 # v3.28.0
with:
sarif_file: 'trivy-results.sarif'
category: 'container-scan'
- name: Generate SBOM
if: inputs.sbom
id: sbom
uses: anchore/sbom-action@7ccf588e3cf3cc2611714c2eeae48550fbc17552 # v0.17.10
with:
image: ${{ inputs.registry }}/${{ github.repository_owner }}/${{ inputs.image }}@${{ steps.build.outputs.digest }}
format: 'spdx-json'
output-file: 'sbom.spdx.json'
upload-artifact: false # Disabled: reusable workflow artifact upload has issues
upload-release-assets: false
- name: Upload SBOM as artifact
if: inputs.sbom
uses: actions/upload-artifact@v4
with:
name: sbom-${{ inputs.image }}
path: sbom.spdx.json
retention-days: 30
- name: Install Cosign
if: inputs.sign
uses: sigstore/cosign-installer@dc72c7d5c4d10cd6bcb8cf6e3fd625a9e5e537da # v3.7.0
- name: Sign image with Cosign (keyless OIDC)
if: inputs.sign && inputs.push
env:
COSIGN_EXPERIMENTAL: 1
run: |
cosign sign --yes \
${{ inputs.registry }}/${{ github.repository_owner }}/${{ inputs.image }}@${{ steps.build.outputs.digest }}
- name: Verify signature
if: inputs.sign && inputs.push
env:
COSIGN_EXPERIMENTAL: 1
run: |
# For reusable workflows, the signature identity is the workflow file path
# not the calling repository, so we use a pattern matching either
cosign verify \
--certificate-identity-regexp="https://github.com/samuelho-dev/git-flow/.github/workflows/.*" \
--certificate-oidc-issuer="https://token.actions.githubusercontent.com" \
${{ inputs.registry }}/${{ github.repository_owner }}/${{ inputs.image }}@${{ steps.build.outputs.digest }}
- name: Generate build summary
shell: bash
run: |
{
echo "## 🐳 Docker Build Summary"
echo ""
echo "**Image:** \`${{ inputs.registry }}/${{ github.repository_owner }}/${{ inputs.image }}\`"
echo "**Digest:** \`${{ steps.build.outputs.digest }}\`"
echo "**Platforms:** \`${{ inputs.platforms }}\`"
echo ""
echo "### Tags"
echo "\`\`\`"
echo "${{ steps.meta.outputs.tags }}"
echo "\`\`\`"
echo ""
echo "### Security"
} >> "$GITHUB_STEP_SUMMARY"
if [ "${{ inputs.scan }}" = "true" ]; then
echo "✅ Vulnerability scan: Completed" >> "$GITHUB_STEP_SUMMARY"
fi
if [ "${{ inputs.sign }}" = "true" ]; then
echo "✅ Image signature: Verified" >> "$GITHUB_STEP_SUMMARY"
fi
if [ "${{ inputs.sbom }}" = "true" ]; then
echo "✅ SBOM: Generated" >> "$GITHUB_STEP_SUMMARY"
fi