-
Notifications
You must be signed in to change notification settings - Fork 0
253 lines (227 loc) · 8.69 KB
/
docker-build.yml
File metadata and controls
253 lines (227 loc) · 8.69 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
name: Build, Test and Push Docker Image
on:
workflow_call:
inputs:
build-args:
description: "Docker build arguments (multiline format: KEY1=value1\nKEY2=value2)"
default: ""
type: string
image-name:
description: "Name of Docker Image"
type: string
required: true
image-tag:
description: "Tag of Docker Image"
default: "latest"
type: string
dockerfile:
description: "Path to Dockerfile"
default: "Dockerfile"
type: string
context:
description: "Path to Docker Build Context"
default: "."
type: string
registry:
description: "Docker Registry"
default: "docker.io"
type: string
push:
description: "Push Docker Image to Registry"
default: false
type: boolean
security-scan:
description: "Enable Trivy Security Scan"
default: true
type: boolean
security-report:
description: 'Security Report Mode (`"sarif"` | `"comment"`; ignored if `security-scan: false`)'
default: "sarif"
type: string
trivy-version:
description: "Trivy security scanner version"
default: "v0.69.2"
type: string
hadolint:
description: "Enable Hadolint"
default: true
type: boolean
platform:
description: "Build platform"
default: "linux/amd64"
type: string
secrets:
username:
required: false
password:
required: false
outputs:
checksum:
description: "Checksum of the Docker image"
value: ${{ jobs.build.outputs.checksum }}
jobs:
build:
runs-on: ubuntu-latest
outputs:
checksum: ${{ steps.checksum.outputs.checksum }}
# avoid shell injection through string interpolation
env:
PLATFORM: ${{ inputs.platform }}
OCI_IMAGE: ${{ inputs.image-name }}:${{ inputs.image-tag }}
REPORT_MODE: ${{ inputs.security-report }}
steps:
- name: Validate platform input
run: |
if [ "$PLATFORM" != "linux/amd64" ] && [ "$PLATFORM" != "linux/arm64" ] ; then
echo "❌ Error: Invalid platform: $PLATFORM"
echo "Valid platforms are 'linux/amd64' or 'linux/arm64'"
exit 1
fi
echo "✅ Platform validation passed: $PLATFORM"
- name: Validate security report mode input
if: ${{ inputs.security-scan }}
run: |
if [ "$REPORT_MODE" != "comment" ] && [ "$REPORT_MODE" != "sarif" ] ; then
echo "❌ Error: Invalid security report mode: $REPORT_MODE"
echo "Valid modes are 'comment' or 'sarif'"
exit 1
fi
echo "✅ Security report mode validation passed: $REPORT_MODE"
- name: Checkout Repository
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Login to Docker Hub
if: ${{ inputs.push }}
uses: docker/login-action@v3
with:
registry: ${{ inputs.registry }}
username: ${{ secrets.username }}
password: ${{ secrets.password }}
- name: Build Docker image
uses: docker/build-push-action@v6
with:
build-args: ${{ inputs.build-args }}
context: ${{ inputs.context }}
file: ${{ inputs.dockerfile }}
platforms: ${{ inputs.platform }}
load: true # Make the image available on runner
push: false # Don't push yet, wait for security checks
tags: ${{ inputs.image-name }}:${{ inputs.image-tag }}
- name: Run Trivy vulnerability scanner
id: trivy
if: ${{ inputs.security-scan }}
uses: aquasecurity/trivy-action@0.34.1
with:
image-ref: ${{ inputs.image-name }}:${{ inputs.image-tag }}
format: ${{ (inputs.security-report == 'sarif' && 'sarif') || 'table' }}
vuln-type: "os,library"
hide-progress: true
output: ${{ (inputs.security-report == 'sarif' && 'trivy-results.sarif') || 'trivy.txt' }}
# following https://github.com/aquasecurity/trivy/discussions/10265
version: ${{ inputs.trivy-version }}
- name: Read Trivy report file
id: read_trivy
if: github.event_name == 'pull_request' && inputs.security-scan && inputs.security-report == 'comment'
run: |
echo "report<<EOF" >> "$GITHUB_OUTPUT"
cat trivy.txt >> "$GITHUB_OUTPUT"
echo "EOF" >> "$GITHUB_OUTPUT"
- name: Find existing Trivy comment
if: github.event_name == 'pull_request' && inputs.security-scan && inputs.security-report == 'comment'
id: find_trivy
uses: peter-evans/find-comment@v3
with:
issue-number: ${{ github.event.pull_request.number }}
comment-author: "github-actions[bot]"
body-includes: "Trivy Security ${{ inputs.platform }} Scan Results"
- name: Create or update Trivy comment
if: github.event_name == 'pull_request' && inputs.security-scan && inputs.security-report == 'comment'
uses: peter-evans/create-or-update-comment@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}
issue-number: ${{ github.event.pull_request.number }}
comment-id: ${{ steps.find_trivy.outputs.comment-id }}
edit-mode: replace
body: |
<!-- trivy-scan -->
### 🔒 Trivy Security ${{ inputs.platform }} Scan Results
<details><summary>Click to expand detailed results</summary>
```bash
${{ steps.read_trivy.outputs.report }}
```
</details>
- name: Upload Trivy scan results to GitHub Security tab
if: github.event_name == 'pull_request' && inputs.security-scan && inputs.security-report == 'sarif'
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: "trivy-results.sarif"
- name: Run Hadolint Dockerfile linter
id: hadolint
if: ${{ inputs.hadolint }}
uses: hadolint/hadolint-action@v3.1.0
with:
dockerfile: ${{ inputs.dockerfile }}
output-file: hadolint.txt
no-fail: true
- name: Read Hadolint report file
id: read_hadolint
if: ${{ inputs.hadolint }}
run: |
echo "report<<EOF" >> "$GITHUB_OUTPUT"
cat hadolint.txt >> "$GITHUB_OUTPUT"
echo "EOF" >> "$GITHUB_OUTPUT"
cat hadolint.txt
- name: Find existing Hadolint comment
id: find_hadolint
if: ${{ github.event_name == 'pull_request' && inputs.hadolint }}
uses: peter-evans/find-comment@v3
with:
issue-number: ${{ github.event.pull_request.number }}
comment-author: "github-actions[bot]"
body-includes: "Hadolint Dockerfile Lint Results"
- name: Create or update Hadolint comment
if: ${{ github.event_name == 'pull_request' && inputs.hadolint && steps.read_hadolint.outputs.report != '' }}
uses: peter-evans/create-or-update-comment@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}
issue-number: ${{ github.event.pull_request.number }}
comment-id: ${{ steps.find_hadolint.outputs.comment-id }}
edit-mode: replace
body: |
<!-- hadolint-scan -->
### 🐳 Hadolint Dockerfile Lint Results
<details><summary>Click to expand detailed results</summary>
```bash
${{ steps.read_hadolint.outputs.report }}
```
</details>
- name: Fail build on CRITICAL or HIGH vulnerabilities
if: ${{ inputs.security-scan }}
uses: aquasecurity/trivy-action@0.34.1
with:
image-ref: ${{ inputs.image-name }}:${{ inputs.image-tag }}
format: table
ignore-unfixed: true
vuln-type: "os,library"
severity: "CRITICAL,HIGH"
hide-progress: true
skip-setup-trivy: true
exit-code: 1
- name: Push Docker image
if: ${{ inputs.push }}
run: docker push "$OCI_IMAGE"
- name: Compute checksum of the Docker image
id: checksum
run: |
# Extract SHA256 digest and format as 0X...
INSPECT=$(docker image inspect "$OCI_IMAGE")
DIGEST=$(echo "$INSPECT" | jq -r 'if .[0].RepoDigests[0] then .[0].RepoDigests[0] | split("@sha256:")[1] else .[0].Id | split(":")[1] end')
echo "checksum=0x${DIGEST}" >> "$GITHUB_OUTPUT"
- name: Cleanup files
if: always()
run: |
rm -f trivy.txt trivy-results.sarif
docker image rm -f "$OCI_IMAGE"