-
Notifications
You must be signed in to change notification settings - Fork 0
277 lines (246 loc) · 9.15 KB
/
image_build_multi.yml
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
name: Container Image Build
on:
workflow_call:
inputs:
image_name:
description: "Name of image, without tags. Not required if image_tags specified."
required: false
type: string
image_tags:
description: "Default=the images are automatically tagged. Override tags with space separated list."
required: false
type: string
default: ""
extra_build_args:
description: "Space separated list of extra build args to use for the image."
required: false
type: string
registry:
description: "Override GHCR to use an external reg."
required: false
type: string
default: "ghcr.io"
context:
description: "Root directory to start the build from."
required: false
type: string
default: "."
dockerfile:
description: "Name of dockerfile, relative to context dir."
required: false
type: string
default: "Dockerfile"
build_target:
description: "The target to built to (default to end of the Dockerfile)."
required: false
type: string
default: ""
push:
description: "Override prevent pushing the image."
required: false
type: boolean
default: true
cache:
description: "Use GHCR caching. Default true. Set this false if registry is not ghcr.io."
required: false
type: boolean
default: true
scan_dockerfile:
description: "Enable dockerfile vulnerability scanning, prior to build."
required: false
type: boolean
default: true
scan_image:
description: "Enable image vulnerability scan, after build."
required: false
type: boolean
default: true
skip_cve:
description: "Skip specific CVE from checkcov (override rules)."
required: false
type: string
# Skip mandatory user creation, allow ROOT usage
default: "CKV_DOCKER_8,CKV_DOCKER_2,CKV_DOCKER_3,CKV_DOCKER_5"
outputs:
image_name:
description: "The final full image reference."
value: ${{ jobs.build-images.outputs.image_name }}
image_tag:
description: "The final image tag."
value: ${{ jobs.build-images.outputs.image_tag }}
jobs:
scan-dockerfile:
if: ${{ inputs.scan_dockerfile }}
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Dockerfile Vulnerabilities
uses: bridgecrewio/checkov-action@v12
with:
file: ${{ inputs.context }}/${{ inputs.dockerfile }} # only run on file
dockerfile_path: ${{ inputs.context }}/${{ inputs.dockerfile }}
quiet: true # show only failed checks
skip_check: ${{ inputs.skip_cve }}
build-images:
runs-on: ubuntu-latest
environment:
name: ${{ github.ref_name }}
outputs:
image_name: ${{ steps.get_image_name.outputs.image_name }}
image_tag: ${{ steps.get_image_name.outputs.image_tag }}
permissions:
contents: read
packages: write
strategy:
fail-fast: false
matrix:
platform:
- linux/amd64
- linux/arm/v6
- linux/arm/v7
- linux/arm64
steps:
- name: Checkout repository
uses: actions/checkout@v4
- id: vars_and_secrets
name: Vars and Secrets to Env
env:
VARS_CONTEXT: ${{ toJson(vars) }}
SECRETS_CONTEXT: ${{ toJson(secrets) }}
run: |
# Random delimeter string for security
delim=$(dd if=/dev/urandom bs=15 count=1 status=none | base64)
# Func to check if json empty
is_empty_json() { [[ -z "$1" || "$1" = '{}' ]]; }
# Func to parse JSON with multiline strings, using delimeter (Github specific)
to_envs() { jq -r "to_entries[] | \"\(.key)<<$delim\n\(.value)\n$delim\n\""; }
# Set values to GITHUB_ENV, if present
if is_empty_json "${VARS_CONTEXT}"; then
echo "${VARS_CONTEXT}" | to_envs >> $GITHUB_ENV
fi
# Set values to GITHUB_ENV, if present
if is_empty_json "${SECRETS_CONTEXT}"; then
echo "${SECRETS_CONTEXT}" | to_envs >> $GITHUB_ENV
fi
- name: Log in to the Container registry
uses: docker/login-action@v3
with:
registry: ${{ inputs.registry }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Configure Image Tags
if: ${{ inputs.image_tags == '' }}
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ inputs.image_name }}
tags: |
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
- name: Setup Buildx
uses: docker/setup-buildx-action@v3
- name: Build Container Image
id: container-build
uses: docker/build-push-action@v6
with:
context: ${{ inputs.context }}
file: ${{ inputs.context }}/${{ inputs.dockerfile }}
target: ${{ inputs.build_target }}
# push: ${{ inputs.push }}
tags: ${{ inputs.image_tags || steps.meta.outputs.tags }}
# TODO fix to allow passing all env vars and secrets (currently hardcoded)
# vars_and_secrets correctly loads to `env.` context, but
# passing into extra_build_args as build-args doesn't work
build-args: |
APP_VERSION=${{ github.ref_name }}
COMMIT_REF=${{ github.sha }}
VITE_API_URL=${{ vars.VITE_API_URL }}
VITE_SYNC_URL=${{ vars.VITE_SYNC_URL }}
NODE_ENV=${{ vars.NODE_ENV || 'production' }}
MONITORING=${{ vars.MONITORING }}
${{ inputs.extra_build_args }}
labels: ${{ steps.meta.outputs.labels }}
# cache-from: ${{ inputs.cache == 'true' && 'type=gha' || '' }}
# cache-to: ${{ inputs.cache == 'true' && 'type=gha,mode=max' || '' }}
platforms: ${{ matrix.platform }}
outputs: type=image,name=${{ inputs.image_name }},push-by-digest=true,name-canonical=true,push=true
- name: Export digest
run: |
mkdir -p /tmp/digests
digest="${{ steps.build-images.outputs.digest }}"
touch "/tmp/digests/${digest#sha256:}"
- name: Upload digest
uses: actions/upload-artifact@v4
with:
path: /tmp/digests/*
name: digest
if-no-files-found: error
retention-days: 1
- id: get_image_name
name: Get First Image Name
run: |
image_names=${{ fromJSON(steps.container-build.outputs.metadata)['image.name'] }}
IFS=',' read -ra images_array <<< "$image_names"
first_image=${images_array[0]}
echo "Image Name: ${first_image}"
echo "image_name=${first_image}" >> $GITHUB_OUTPUT
# Extract the tag (the part after the last colon)
image_tag="${first_image##*:}"
echo "image_tag=${image_tag}" >> $GITHUB_OUTPUT
echo "Image Tag: $image_tag"
merge-manifest:
runs-on: ubuntu-latest
needs: [build-images]
steps:
- name: Download Img Digests
uses: actions/download-artifact@v4
with:
path: /tmp/digests
- name: Setup Buildx
uses: docker/setup-buildx-action@v3
- name: Configure Image Tags
if: ${{ inputs.image_tags == '' }}
id: set-tags
uses: docker/metadata-action@v5
with:
images: ${{ inputs.image_name }}
tags: |
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
- name: Log in to the Container registry
uses: docker/login-action@v3
with:
registry: ${{ inputs.registry }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Create Manifest List
working-directory: /tmp/digests
run: |
docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
$(printf '${{ inputs.image_name }}@sha256:%s ' *)
- name: Inspect Image
run: |
docker buildx imagetools inspect ${{ inputs.image_name }}:${{ steps.set-tags.outputs.version }}
scan-image:
needs: [build-images]
runs-on: ubuntu-latest
if: ${{ inputs.scan_image }}
steps:
- name: Scan Container Image
uses: aquasecurity/[email protected]
with:
image-ref: ${{ needs.build-images.outputs.image_name }}
format: "table"
exit-code: "1"
ignore-unfixed: true
vuln-type: "os,library"
severity: "CRITICAL"
# Avoid rate-limiting, see: https://github.com/aquasecurity/trivy-action/issues/389
env:
TRIVY_DB_REPOSITORY: ghcr.io/aquasecurity/trivy-db,public.ecr.aws/aquasecurity/trivy-db
TRIVY_JAVA_DB_REPOSITORY: ghcr.io/aquasecurity/trivy-java-db,public.ecr.aws/aquasecurity/trivy-java-db