-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathci-spring-boot-build-publish-image.yml
More file actions
337 lines (315 loc) · 13 KB
/
Copy pathci-spring-boot-build-publish-image.yml
File metadata and controls
337 lines (315 loc) · 13 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
# Workflow Name: Build/publish Spring Boot container image
#
# Owner: Digdir Platform team
#
# Purpose:
# This is the golden path workflow for building and publishing Spring Boot
# container images. The workflow should not be used directly, but called from
#. the ci-build-publish-image workflow, which will handle input validation and
# trigger this workflow with the correct parameters. The workflow will build
# a Spring Boot image based on the 'application-path' and 'module-name' input
#. parameters, which determines whether to build from a module or the root
# application. The workflow will use Paketo buildpacks for building the
# image, and the specific builder image can be configured through inputs.
# After building the image, the workflow will run Trivy vulnerability scans,
# push the image to the specified container registry, and optionally sign the
# image. The workflow will also set outputs for the image version and digest,
# which can be used in subsequent steps or workflows. If any step fails, a
# Slack notification will be sent to the specified channel(s).
#
# Flow:
# 1. Set image metadata (name and tag)
# 2. Checkout repository
# 3. Set up JDK
#. 4. Optionally update versions in application.yaml with the generated
# image tag
# 5. Build Spring Boot image using Paketo buildpacks. The image will be
# built based on the application path or module name (if provided)
# 6. Run Trivy vulnerability scan on the built image
# 7. Login to container registry
# 8. Push image to container registry
# 9. Set image digest output
# 10. Run Trivy SBOM generation
# 11. Optionally sign the image
# 12. If any step fails, send Slack notification to specified channel(s)
#
# Trigger:
# Triggered by workflow calls (workflow_call)
#
# Inputs:
# See input section
#
# Outputs:
# See output section
#
name: Build and publish Spring Boot container image
permissions: {}
on:
workflow_call:
inputs:
image-name:
description: Name of Docker image
required: false
type: string
image-pack:
description: Paketo builder image to use. See https://paketo.io/docs/reference/builders-reference/ for choices
default: builder-noble-java-tiny
required: false
type: string
image-pack-tag:
description: Docker image pack version tag
default: "latest"
required: false
type: string
image-signing:
description: Flag to toggle image signing on/off - default off
default: true
required: false
type: boolean
java-version:
description: Main version of java
default: "25"
required: false
type: string
java-distribution:
description: Java distribution to use
default: "liberica"
required: false
type: string
slack-channel-id:
description: Team channel id
default: ""
required: false
type: string
application-path:
description: Path to the application to build, relative to the repository root
default: "./"
required: false
type: string
module-name:
description: Name of the module to build, if using a multi-module Maven project. If not provided, the workflow will attempt to build the application based on the application-path input
default: ""
required: false
type: string
image-tag:
description: Custom image tag, if not provided a tag will be auto-generated
default: ""
required: false
type: string
cache-path:
description: Path to cache dependencies, relative to the repository root
default: "**/pom.xml"
required: false
type: string
update-versions:
description: Flag to toggle whether to update versions in application.yaml with the generated image tag
default: "true"
required: false
type: string
container-registry:
description: Container Registry url
default: "creiddev.azurecr.io"
required: false
type: string
sp-container-registry-client-id:
description: Service Principal used for container registry in inputs
required: false
default: "AZURE_CLIENT_ID"
type: string
trivy-library-disable-scan:
description: Disable Trivy library scan
type: boolean
required: false
default: false
trivy-library-ignore-unfixed:
description: Ignore unfixed vulnerabilities in Trivy library scan
type: boolean
required: false
default: true
trivy-library-severity:
description: When to fail the scan with Trivy library vulnerabilities
type: string
required: false
default: 'HIGH,CRITICAL'
trivy-os-disable-scan:
description: Disable Trivy OS scan
type: boolean
required: false
default: false
trivy-os-ignore-unfixed:
description: Ignore unfixed vulnerabilities in Trivy OS scan
type: boolean
required: false
default: true
trivy-os-severity:
description: When to fail the scan with Trivy OS vulnerabilities
type: string
required: false
default: 'CRITICAL'
trivy-version:
description: Version of Trivy to use for scanning
type: string
required: false
default: ''
outputs:
image-version:
description: "Docker image version"
value: ${{ jobs.build-publish-image.outputs.image-tag }}
image-digest:
description: "Docker image SHA256 digest"
value: ${{ jobs.build-publish-image.outputs.image-digest }}
jobs:
inputs-to-summary:
runs-on: ubuntu-latest
steps:
- name: Write inputs to summary
uses: felleslosninger/github-workflows/.github/actions/json-to-summary@main
with:
json-payload: ${{ toJson(inputs) }}
build-publish-image:
runs-on: ubuntu-latest
outputs:
image-tag: ${{ steps.image-metadata.outputs.image-tag }}
image-digest: ${{ steps.set-image-digest.outputs.image-digest }}
permissions:
id-token: write
contents: write
packages: write
steps:
- name: Set image metadata
id: image-metadata
uses: felleslosninger/github-workflows/.github/actions/image-metadata@main
with:
image-name: ${{ inputs.image-name }}
container-registry: ${{ inputs.container-registry }}
image-tag: ${{ inputs.image-tag }}
- name: Checkout repository
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # pin@v6.0.2
- name: Set up JDK ${{ inputs.java-version }}
uses: actions/setup-java@be666c2fcd27ec809703dec50e508c2fdc7f6654 # pin@v5.2.0
with:
distribution: "${{ inputs.java-distribution }}"
java-version: ${{ inputs.java-version }}
cache: maven
cache-dependency-path: ${{ inputs.cache-path }}
server-id: github
server-username: GH_PACKAGES_READ_USER
server-password: GH_PACKAGES_READ_PAT
- name: Maven update version used in application.yaml
env:
GH_PACKAGES_READ_USER: ${{ secrets.GH_PACKAGES_READ_USER }}
GH_PACKAGES_READ_PAT: ${{ secrets.GH_PACKAGES_READ_PAT }}
UPDATE_VERSIONS: ${{ inputs.update-versions }}
IMAGE_TAG: ${{ steps.image-metadata.outputs.image-tag }}
run: |
if [ "$UPDATE_VERSIONS" == "true" ]; then
mvn versions:set -B -DnewVersion="$IMAGE_TAG"
echo "- \`mvn versions\` was executed" >> "$GITHUB_STEP_SUMMARY"
else
echo "- \`mvn versions\` was not executed" >> "$GITHUB_STEP_SUMMARY"
fi
- name: Build image with Maven/Spring Boot (module-name)
if: inputs.module-name != ''
env:
GH_PACKAGES_READ_USER: ${{ secrets.GH_PACKAGES_READ_USER }}
GH_PACKAGES_READ_PAT: ${{ secrets.GH_PACKAGES_READ_PAT }}
MODULE_NAME: ${{ inputs.module-name }}
IMAGE_NAME: ${{ steps.image-metadata.outputs.image-name }}
IMAGE_TAG: ${{ steps.image-metadata.outputs.image-tag }}
IMAGE_PACK: ${{ inputs.image-pack }}
IMAGE_PACK_TAG: ${{ inputs.image-pack-tag }}
run: |
mvn install -B spring-boot:build-image \
-pl "$MODULE_NAME" -am \
-Dspring-boot.build-image.imageName="$IMAGE_NAME:$IMAGE_TAG" \
-Dspring-boot.build-image.builder="paketobuildpacks/$IMAGE_PACK:$IMAGE_PACK_TAG" \
-Dspring-boot.build-image.createdDate=now
- name: Build image with Maven/Spring Boot (application-path)
if: inputs.module-name == ''
env:
GH_PACKAGES_READ_USER: ${{ secrets.GH_PACKAGES_READ_USER }}
GH_PACKAGES_READ_PAT: ${{ secrets.GH_PACKAGES_READ_PAT }}
APP_PATH: ${{ inputs.application-path }}
IMAGE_NAME: ${{ steps.image-metadata.outputs.image-name }}
IMAGE_TAG: ${{ steps.image-metadata.outputs.image-tag }}
IMAGE_PACK: ${{ inputs.image-pack }}
IMAGE_PACK_TAG: ${{ inputs.image-pack-tag }}
run: |
mvn -B spring-boot:build-image \
--file "${APP_PATH}pom.xml" \
-Dspring-boot.build-image.imageName="$IMAGE_NAME:$IMAGE_TAG" \
-Dspring-boot.build-image.builder="paketobuildpacks/$IMAGE_PACK:$IMAGE_PACK_TAG" \
-Dspring-boot.build-image.createdDate=now
- name: Run Trivy vulnerability scanner
uses: felleslosninger/github-workflows/.github/actions/trivy-scan@main
with:
image-ref: ${{ steps.image-metadata.outputs.image-name }}:${{ steps.image-metadata.outputs.image-tag }}
application-path: ${{ inputs.application-path }}
library-disable-scan: ${{ inputs.trivy-library-disable-scan }}
library-ignore-unfixed: ${{ inputs.trivy-library-ignore-unfixed }}
library-severity: ${{ inputs.trivy-library-severity }}
os-disable-scan: ${{ inputs.trivy-os-disable-scan }}
os-ignore-unfixed: ${{ inputs.trivy-os-ignore-unfixed }}
os-severity: ${{ inputs.trivy-os-severity }}
trivy-version: ${{ inputs.trivy-version }}
# Login to GHCR if container-registry starts with ghcr.io
- name: Login to GitHub Container Registry
if: contains(inputs.container-registry, 'ghcr.io')
uses: docker/login-action@4907a6ddec9925e35a0a9e82d7399ccc52663121 # pin@v4.1.0
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: ACR login
if: contains(inputs.container-registry, 'azurecr.io')
uses: felleslosninger/github-workflows/.github/actions/acr-login@main
with:
client-id: ${{ secrets[inputs.sp-container-registry-client-id] }}
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
acr-name: ${{ inputs.container-registry }}
- name: Push image
env:
IMAGE_NAME: ${{ steps.image-metadata.outputs.image-name }}
IMAGE_TAG: ${{ steps.image-metadata.outputs.image-tag }}
run: docker push "$IMAGE_NAME:$IMAGE_TAG"
- name: Set image digest
id: set-image-digest
env:
IMAGE_NAME: ${{ steps.image-metadata.outputs.image-name }}
IMAGE_TAG: ${{ steps.image-metadata.outputs.image-tag }}
run: |
image_digest=$(docker inspect \
--format='{{.RepoDigests}}' "$IMAGE_NAME:$IMAGE_TAG" \
| cut -d '@' -f 2 \
| cut -d ']' -f 1)
echo "image-digest=$image_digest" >> "$GITHUB_OUTPUT"
echo "- Image digest: $image_digest" >> "$GITHUB_STEP_SUMMARY"
- name: Run Trivy SBOM generation
uses: felleslosninger/github-workflows/.github/actions/trivy-sbom@main
with:
scan-type: image
artifact-id: ${{ steps.image-metadata.outputs.image-name }}
image-ref: "${{ steps.image-metadata.outputs.image-name }}@${{ steps.set-image-digest.outputs.image-digest }}"
version: ${{ steps.image-metadata.outputs.image-tag }}
# This is already done in Trivy vuln scan step
skip-setup: true
- name: Image signing
if: inputs.image-signing == true
uses: felleslosninger/github-workflows/.github/actions/image-signing@main
with:
image: ${{ steps.image-metadata.outputs.image-name }}:${{ steps.image-metadata.outputs.image-tag }}
notify-on-errors:
runs-on: ubuntu-latest
needs: [build-publish-image]
if: always() && contains(needs.*.result, 'failure')
strategy:
matrix:
slack-channel: ["${{ inputs.slack-channel-id }}", "C05G4B8R2GG"]
steps:
- name: Send Slack notification
uses: felleslosninger/github-actions/send-slack-notification@d7888fecafbf48e5845c8a0c76389f71c517ee6d # pin@v0.8.0
with:
slack-channel-id: ${{ matrix.slack-channel }}
slack-bot-token: ${{ secrets.SLACK_CICD_NOTIFICATION_TOKEN }}
failed: "true"