-
Notifications
You must be signed in to change notification settings - Fork 0
333 lines (293 loc) · 12.5 KB
/
ci-spring-boot-build-publish-image.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
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
name: Build and publish Docker image
on:
workflow_call:
inputs:
image-name:
description: Name of Docker image
required: false
type: string
image-pack:
description: Docker image pack of builder-jammy-tiny, builder-jammy-base or builder-jammy-full
default: builder-jammy-tiny
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: "11"
required: false
type: string
container-scan-offline-mode:
description: Container-scan is default download updated CVE definitions via Trivy, enable offline on download problems
default: false
type: boolean
slack-channel-id:
description: Team channel id
default: ""
required: false
type: string
application-path:
default: "./"
required: false
type: string
cache-path:
default: "**/pom.xml"
required: false
type: string
update-versions:
default: "true"
required: false
type: string
setup-npm-auth:
description: Configure private NPM registry authentication
default: false
required: false
type: boolean
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-actions/json-to-summary@bb702913dc7fc7ba16f6a2ce32624ba9eaf54262 # [email protected]
with:
json: ${{ toJson(inputs) }}
title: "Inputs"
build-publish-image:
runs-on: ubuntu-latest
env:
DOCKLE_HOST: "unix:///var/run/docker.sock"
TRIVY_OFFLINE_SCAN: ${{ inputs.container-scan-offline-mode }}
outputs:
image-tag: ${{ steps.set-image-tag.outputs.image-tag }}
image-digest: ${{ steps.set-image-digest.outputs.image-digest }}
permissions:
id-token: write
contents: write
packages: read
steps:
- name: Set image tag
id: set-image-tag
run: |
image_tag=$(date +'%Y-%m-%d-%H%M')-${GITHUB_SHA::8}
echo "image-tag=$image_tag" >> "$GITHUB_OUTPUT"
echo "- Image tag: $image_tag" >> "$GITHUB_STEP_SUMMARY"
- name: Set image name
id: set-image-name
run: |
image_name=${{ secrets.REGISTRY_URL }}/${{ inputs.image-name || github.event.repository.name }}
echo "image-name=$image_name" >> "$GITHUB_OUTPUT"
echo "- Image name: $image_name" >> "$GITHUB_STEP_SUMMARY"
- name: Checkout repository
uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # [email protected]
- name: Check if YAML files exist
id: yaml-files-exists
run: |
if [ -f "${{ inputs.application-path }}src/main/resources/application*.y*ml" ]; then
echo "file-exists=true" >> "$GITHUB_OUTPUT"
echo "- \`${{ inputs.application-path }}src/main/resources/application*.y*ml\` file exists :white_check_mark:" >> "$GITHUB_STEP_SUMMARY"
else
echo "file-exists=false" >> "$GITHUB_OUTPUT"
{
echo "- \`${{ inputs.application-path }}src/main/resources/application*.y*ml\` file exists :x:"
echo " - Skipping yamllint"
} >> "$GITHUB_STEP_SUMMARY"
fi
- name: Run yamllint
if: steps.yaml-files-exists.outputs.file_exists == 'true'
uses: ibiqlik/action-yamllint@2576378a8e339169678f9939646ee3ee325e845c # [email protected]
with:
file_or_dir: ${{ inputs.application-path }}src/main/resources/application*.y*ml
config_data: |
extends: default
rules:
empty-lines: disable
line-length:
max: 150
level: warning
- name: Set up JDK ${{ inputs.java-version }}
uses: actions/setup-java@b36c23c0d998641eff861008f374ee103c25ac73 # [email protected]
with:
distribution: "liberica"
java-version: ${{ inputs.java-version }}
cache: maven
cache-dependency-path: ${{ inputs.cache-path }}
- name: Maven environment setup
uses: s4u/maven-settings-action@7802f6aec16c9098b4798ad1f1d8ac75198194bd # [email protected]
with:
servers: |
[{
"id": "github-oidc-sdk",
"username": "${{ secrets.GH_PACKAGES_READ_USER }}",
"password": "${{ secrets.GH_PACKAGES_READ_PAT }}"
},
{
"id": "github",
"username": "${{ secrets.GH_PACKAGES_READ_USER }}",
"password": "${{ secrets.GH_PACKAGES_READ_PAT }}"
}]
- name: Maven update version used in application.yaml
run: |
if [ "${{ inputs.update-versions }}" == "true" ]; then
mvn versions:set -B -DnewVersion="${{ steps.set-image-tag.outputs.image-tag }}"
echo "- \`mvn versions\` was executed" >> "$GITHUB_STEP_SUMMARY"
else
echo "- \`mvn versions\` was not executed" >> "$GITHUB_STEP_SUMMARY"
fi
- name: Configure private NPM registry authentication
if: ${{ inputs.setup-npm-auth == true }}
uses: actions/setup-node@0a44ba7841725637a19e28fa30b79a866c81b0a6 # [email protected]
with:
node-version: '20.x'
registry-url: 'https://npm.pkg.github.com'
scope: '@felleslosninger'
env:
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Build image with Maven/Spring Boot
run: |
mvn -B spring-boot:build-image \
--file ${{ inputs.application-path }}pom.xml \
-Dspring-boot.build-image.imageName=${{ steps.set-image-name.outputs.image-name }}:${{ steps.set-image-tag.outputs.image-tag }} \
-Dspring-boot.build-image.builder=paketobuildpacks/${{ inputs.image-pack }} \
-Dspring-boot.build-image.createdDate=now
- name: Set trivyignore env if file exists
id: set-trivyignore-path
run: |
if [ -f "${{ inputs.application-path }}.trivyignore" ]; then
trivyignore_path=${{ inputs.application-path }}.trivyignore
echo "trivyignore-path=$trivyignore_path" >> "$GITHUB_OUTPUT"
echo "- Trivy ignore path: $trivyignore_path" >> "$GITHUB_STEP_SUMMARY"
fi
- name: Run Trivy vulnerability scanner
uses: aquasecurity/trivy-action@915b19bbe73b92a6cf82a1bc12b087c9a19a5fe2 # [email protected]
id: trivy-primary
env:
TRIVY_DB_REPOSITORY: ghcr.io/aquasecurity/trivy-db:2,public.ecr.aws/aquasecurity/trivy-db:2
TRIVY_JAVA_DB_REPOSITORY: ghcr.io/aquasecurity/trivy-java-db:1,public.ecr.aws/aquasecurity/trivy-java-db:1
with:
image-ref: ${{ steps.set-image-name.outputs.image-name }}:${{ steps.set-image-tag.outputs.image-tag }}
exit-code: "1"
severity: "CRITICAL,HIGH"
trivyignores: ${{ steps.set-trivyignore-path.outputs.trivyignore-path }}
- name: Create SBOM artifact name
id: sbom-name
run: |
SBOM_NAME=$(echo ${{ steps.set-image-name.outputs.image-name }} | tr '/' '-')
echo "sbom-artifact-id=$SBOM_NAME" >> "$GITHUB_OUTPUT"
echo "- SBOM: $SBOM_NAME" >> "$GITHUB_STEP_SUMMARY"
- name: Run Trivy SBOM generation
uses: aquasecurity/trivy-action@915b19bbe73b92a6cf82a1bc12b087c9a19a5fe2 # [email protected]
env:
TRIVY_DB_REPOSITORY: ghcr.io/aquasecurity/trivy-db:2,public.ecr.aws/aquasecurity/trivy-db:2
TRIVY_JAVA_DB_REPOSITORY: ghcr.io/aquasecurity/trivy-java-db:1,public.ecr.aws/aquasecurity/trivy-java-db:1
with:
scan-type: image
format: github
image-ref: "${{ steps.set-image-name.outputs.image-name }}:${{ steps.set-image-tag.outputs.image-tag }}"
output: "sbom-${{ steps.sbom-name.outputs.sbom-artifact-id }}-${{ steps.set-image-tag.outputs.image-tag }}.json"
trivyignores: ${{ steps.set-trivyignore-path.outputs.trivyignore-path }}
github-pat: ${{ secrets.GITHUB_TOKEN }}
- name: Upload trivy report as a Github artifact
uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882 # [email protected]
with:
name: "sbom-${{ steps.sbom-name.outputs.sbom-artifact-id }}-${{ steps.set-image-tag.outputs.image-tag }}"
path: "${{ github.workspace }}/sbom-${{ steps.sbom-name.outputs.sbom-artifact-id }}-${{ steps.set-image-tag.outputs.image-tag }}.json"
- name: Login to Azure
uses: azure/login@v2
with:
client-id: ${{ secrets.AZURE_CLIENT_ID }}
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
- name: Login to Azure Container Registry
run: az acr login --name "$ACR_NAME"
env:
ACR_NAME: ${{ vars.ACR_NAME }}
- name: Push image
run: docker push ${{ steps.set-image-name.outputs.image-name }}:${{ steps.set-image-tag.outputs.image-tag }}
- name: Set image digest
id: set-image-digest
run: |
image_digest=$(docker inspect \
--format='{{.RepoDigests}}' ${{ steps.set-image-name.outputs.image-name }}:${{ steps.set-image-tag.outputs.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: Install Cosign
if: ${{ inputs.image-signing == true }}
uses: sigstore/cosign-installer@dc72c7d5c4d10cd6bcb8cf6e3fd625a9e5e537da # [email protected]
- name: Generate Provenance Metadata
if: ${{ inputs.image-signing == true }}
run: |
# Generate provenance data according to spec https://slsa.dev/spec/v1.0/provenance
cat <<EOF > ./provenance.json
{
"_type": "https://slsa.dev/provenance/v1",
"subject": [
{
"name": "${{ steps.set-image-name.outputs.image-name }}",
"digest": {
"sha256": "${DIGEST}"
}
}
],
"buildDefinition": {
"buildType": "https://github.com/github-actions/build",
"externalParameters": {
"image": "${{ steps.set-image-name.outputs.image-name }}:${{ steps.set-image-tag.outputs.image-tag }}",
"commit": "${{ github.sha }}",
"author": "${{ github.actor }}",
"job": "${{ github.job }}",
"repositoryOwnerId": "${{ github.repository_owner_id }}"
}
},
"runDetails": {
"builder": {
"id": "${{ github.workflow_ref }}"
},
"metadata": {
"invocationId": "${{ github.run_id }}",
"startedOn": "$(date -u +"%Y-%m-%dT%H:%M:%SZ")"
}
},
"materials": [
{
"uri": "git+https://github.com/${{ github.repository }}.git",
"digest": {
"sha1": "${{ github.sha }}"
}
}
]
}
EOF
- name: Sign the image
if: ${{ inputs.image-signing == true }}
run: |
cosign sign --yes "${{ steps.set-image-name.outputs.image-name }}@${DIGEST}"
cosign attest --yes --predicate ./provenance.json --type slsaprovenance1 "${{ steps.set-image-name.outputs.image-name }}@${DIGEST}"
env:
DIGEST: ${{ steps.set-image-digest.outputs.image-digest }}
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@bb702913dc7fc7ba16f6a2ce32624ba9eaf54262 # [email protected]
with:
slack-channel-id: ${{ matrix.slack-channel }}
slack-bot-token: ${{ secrets.SLACK_CICD_NOTIFICATION_TOKEN }}
failed: "true"