-
Notifications
You must be signed in to change notification settings - Fork 0
202 lines (192 loc) · 9.04 KB
/
image-publish-trivy.yaml
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
# Builds and uploads a docker image to multiple repositories and scans it with trivy (optional)
# Trivy scan can be disabled
# dockerhub_repository_owner: required when pushing to dockerhub
# quay_repository_owner: required when pushing to quay.io
# Image tag options:
# image_tag_generation: "ticket_from_branch" The ticket is extracted from the branch name (e.g. OPS-123-testing -> OPS-123)
# image_tag_generation: "commit_hash" Short hash of the commit is used as tag
# image_tag_generation: "version_git_tag" Git tag with version is used as tag
# image_tag_generation: "specified" The version specified with the image_tag input is used as tag
# image_tag_generation: "mmp_git_tag" The tag is derived from git tag with pattern "\d.\d.\d" as mayor.minor.patch version (e.g. infra-tools-1.3.6 -> 1.3.6)
# image_tag_generation: "mm_git_tag" The tag is derived from git tag with pattern "\d.\d" as mayor.minor version (e.g. infra-tools-1.3.6 -> 1.3)
# add_latest_tag: true/false "latest" gets added as additiontal image tag
name: Publish image (and run Trivy)
on:
workflow_call:
inputs:
image_name:
description: "Name of the image to build"
required: true
type: string
container_registry:
description: "Comma separated list of target container registries. Possible registries are ghcr.io (default), quay.io and dockerhub."
required: false
type: string
default: "ghcr.io"
dockerhub_repository_owner:
description: "The owner of the repository in dockerhub. Required when pushing to dockerhub."
required: false
type: string
quay_repository_owner:
description: "The owner of the repository in quay.io. Required when pushing to quay.io."
required: false
type: string
add_latest_tag:
description: "Whether latest should be added as image tag (default: false)"
required: false
type: boolean
default: false
image_tag_generation:
description: "Comma separated list of image tag generation strategies: ticket_from_branch, commit_hash, version_git_tag, specified, mmp_git_tag or mm_git_tag"
required: false
type: string
image_tag:
type: string
description: "Custom image tag (required if image_tag_generation: specified)"
required: false
context:
description: "Directory where the image is built, defaults to repository root"
required: false
default: "./"
type: string
run_trivy_scan:
description: "Whether a trivy scan should run on the uploaded image (default: true)"
required: false
type: boolean
default: true
trivy_severity:
description: 'Severity filter for trivy (default "CRITICAL,HIGH")'
required: false
default: "CRITICAL,HIGH"
type: string
fail_on_vulnerabilites:
description: "Whether the workflow should fail if trivy finds vulnerabilities"
required: false
default: true
type: boolean
ignore-unfixed:
description: "Ignore vulerabilities without fix"
required: false
default: true
type: boolean
report_location:
description: "If defined it overrides the reported location (normally <organization>/<image>) of the trivy findings to a specific file (e.g. Dockerfile)"
required: false
default: ""
type: string
target:
description: "If defined you specify a build stage to stop at when building a multi-stage Dockerfile"
required: false
default: ""
type: string
secrets:
DOCKER_USERNAME:
required: false
DOCKER_TOKEN:
required: false
QUAY_USERNAME:
required: false
QUAY_TOKEN:
required: false
jobs:
build_and_upload_image:
name: Publish image
runs-on: ubuntu-latest
outputs:
digest: ${{ steps.docker_build_push.outputs.digest }}
permissions:
packages: write
contents: read
steps:
- name: Check conditional inputs
run: |
if [[ ${{ contains(inputs.container_registry, 'dockerhub') }} && -z inputs.dockerhub_repository_owner ]]; then
echo "Error: when pushing to dockerhub a repository owner is required."
exit 1
elif [[ ${{ contains(inputs.container_registry, 'quay.io') }} && -z inputs.quay_repository_owner ]]; then
echo "Error: when pushing to quay.io a repository owner is required."
exit 1
fi
- name: Checkout Code
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 #v4.1.1
- name: Build image name and tags
id: docker_meta_img
uses: docker/metadata-action@8e5442c4ef9f78752691e2d8f8d19755c6f78e81 #v5.5.1
with:
images: |
name=ghcr.io/${{ github.repository_owner }}/${{ inputs.image_name }},enable=${{ contains(inputs.container_registry, 'ghcr.io') }}
name=docker.io/${{ inputs.dockerhub_repository_owner }}/${{ inputs.image_name }},enable=${{ contains(inputs.container_registry, 'dockerhub') }}
name=quay.io/${{ inputs.quay_repository_owner }}/${{ inputs.image_name }},enable=${{ contains(inputs.container_registry, 'quay.io') }}
tags: |
type=match,value={{branch}},pattern=^\w+?-\d+,enable=${{ contains(inputs.image_tag_generation, 'ticket_from_branch') }}
type=match,value=${{ github.head_ref || ''}},pattern=^\w+?-\d+,enable=${{ contains(inputs.image_tag_generation, 'ticket_from_branch') }}
type=match,pattern=\d.\d.\d,enable=${{ contains(inputs.image_tag_generation, 'mmp_git_tag') }}
type=match,pattern=\d.\d,enable=${{ contains(inputs.image_tag_generation, 'mm_git_tag') }}
type=sha,enable=${{ contains(inputs.image_tag_generation, 'commit_hash') }}
type=pep440,pattern={{version}},enable=${{ contains(inputs.image_tag_generation, 'version_git_tag') }}
type=raw,value=${{ inputs.image_tag }},enable=${{ contains(inputs.image_tag_generation, 'specified') }}
flavor: |
latest=${{ inputs.add_latest_tag }}
- name: Log into ghcr.io
uses: docker/login-action@343f7c4344506bcbf9b4de18042ae17996df046d #v3.0.0
if: ${{ contains(inputs.container_registry, 'ghcr.io') }}
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Log into dockerhub
uses: docker/login-action@343f7c4344506bcbf9b4de18042ae17996df046d #v3.0.0
if: ${{ contains(inputs.container_registry, 'dockerhub') }}
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_TOKEN }}
- name: Log into quay.io
uses: docker/login-action@343f7c4344506bcbf9b4de18042ae17996df046d #v3.0.0
if: ${{ contains(inputs.container_registry, 'quay.io') }}
with:
registry: quay.io
username: ${{ secrets.QUAY_USERNAME }}
password: ${{ secrets.QUAY_TOKEN }}
- name: Build and push ${{ inputs.image_name }} to ${{ inputs.container_registry }}
id: docker_build_push
uses: docker/build-push-action@4a13e500e55cf31b7a5d59a38ab2040ab0f42f56 #v5.1.0
with:
context: ${{ inputs.context }}
platforms: linux/amd64
push: true
tags: ${{ steps.docker_meta_img.outputs.tags }}
labels: ${{ steps.docker_meta_img.outputs.labels }}
target: ${{ inputs.target }}
pre_scan:
runs-on: ubuntu-latest
if: ${{ inputs.run_trivy_scan }}
needs: build_and_upload_image
outputs:
registry_and_owner: ${{ steps.registry_and_owner.outputs.registry_and_owner }}
steps:
- name: Derive registry and owner for image to scan
id: registry_and_owner
run: |
if [[ ${{ contains(inputs.container_registry, 'dockerhub') }} == true ]]; then
registry_and_owner=docker.io/${{ inputs.dockerhub_repository_owner }}
elif [[ ${{ contains(inputs.container_registry, 'quay.io') }} == true ]]; then
registry_and_owner=quay.io/${{ inputs.quay_repository_owner }}
elif [[ ${{ contains(inputs.container_registry, 'ghcr.io') }} == true ]]; then
registry_and_owner=ghcr.io/${{ github.repository_owner }}
fi
echo "registry_and_owner=$registry_and_owner" >> $GITHUB_OUTPUT
trivy_scan:
name: Trivy scan for uploaded image
# Wait for image upload
needs: [build_and_upload_image, pre_scan]
if: ${{ inputs.run_trivy_scan }}
permissions:
packages: read
security-events: write
uses: dBildungsplattform/dbp-github-workflows/.github/workflows/check-trivy.yaml@7
with:
image_ref: ${{ needs.pre_scan.outputs.registry_and_owner }}/${{ inputs.image_name }}@${{ needs.build_and_upload_image.outputs.digest }}
severity: ${{ inputs.trivy_severity }}
fail_on_vulnerabilites: ${{ inputs.fail_on_vulnerabilites }}
ignore-unfixed: ${{ inputs.ignore-unfixed }}
report_location: ${{ inputs.report_location }}