-
-
Notifications
You must be signed in to change notification settings - Fork 0
294 lines (255 loc) · 10.3 KB
/
ci.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
---
# yaml-language-server: $schema=https://json.schemastore.org/github-workflow.json
name: ci
on:
push:
paths:
- '**/Dockerfile'
- '**/toolsets/*.json'
- '**/.github/workflows/ci.yml'
- '**/docker-bake.hcl'
pull_request:
branches: [main]
workflow_dispatch:
permissions:
contents: read
packages: write
pull-requests: write
env:
REGISTRY: ${{ vars.DOCKERHUB_USER && 'docker.io' || 'ghcr.io' }}
REGISTRY_USER: ${{ vars.DOCKERHUB_USER || github.repository_owner }}
SHA: ${{ github.event.pull_request.head.sha || github.event.after || github.sha }}
BUILDKIT_PROGRESS: plain
jobs:
test:
runs-on: ubuntu-latest
env:
REGISTRY: ${{ vars.DOCKERHUB_USER && 'docker.io' || 'ghcr.io' }}
outputs:
platforms: ${{ steps.platforms.outputs.matrix }}
targets: ${{ steps.targets.outputs.matrix }}
steps:
# kics-scan ignore-line
- uses: actions/checkout@v4
# kics-scan ignore-line
- uses: docker/[email protected]
name: Set up QEMU
- name: Set up Docker Buildx
# kics-scan ignore-line
uses: docker/[email protected]
- name: get docker binary path
id: docker-binary-path
run: echo "DOCKER_BINARY_PATH=$(which docker)" >> "$GITHUB_OUTPUT"
- name: bake-file with empty env
id: bake-file-no-env
run: env -i ${{ steps.docker-binary-path.outputs.DOCKER_BINARY_PATH }} buildx bake --print
- name: verify tag ends on -local
run: |
BAKE_TAG="$(env -i ${{ steps.docker-binary-path.outputs.DOCKER_BINARY_PATH }} buildx bake --print | jq -r '[.target[].tags[]][0]')"
[[ "${BAKE_TAG}" == *"-local" ]] || exit 1
# - name: bake-file with test ref
# id: bake-file-test-tag
# run: printf "BAKE_OUTPUT=%s\n" "$(env -i ${{ steps.docker-binary-path.outputs.DOCKER_BINARY_PATH }} buildx bake --print | jq -c)" >> "$GITHUB_OUTPUT"
# env:
# GITHUB_BASE_REF: 'test'
# - name: verify tag ends on -test
# run: |
# BAKE_TAG="$(echo '${{ steps.bake-file-test-tag.outputs.BAKE_OUTPUT }}' | jq -r '[.target[].tags[]][0]')"
# [[ "${BAKE_TAG}" == *"-test" ]] || exit 1
- name: bake-file
run: docker buildx bake --print
env:
GITHUB_SHA: ${{ env.SHA }}
- name: validate tag
run: |
BAKE_TAG="$(docker buildx bake --print | jq -r '[.target[].tags[]][0]')"
[[ "${BAKE_TAG}" == *"-${{ github.ref_name}}" ]] || exit 1
- name: Create platform matrix
id: platforms
run: printf "matrix=%s\n" "$(docker buildx bake linux-platforms --print | jq -cr '.target."linux-platforms".platforms')" >>"${GITHUB_OUTPUT}"
- name: Show platforms
run: echo '${{ steps.platforms.outputs.matrix }}' | jq
- name: Create target matrix
id: targets
run: printf "matrix=%s\n" "$(docker buildx bake --print | jq -cr ".group.ubuntu.targets")" >>"${GITHUB_OUTPUT}"
- name: Show targets
run: echo '${{ steps.targets.outputs.matrix }}' | jq
build:
needs: test
runs-on: ubuntu-latest
strategy:
matrix:
platforms: ${{ fromJson(needs.test.outputs.platforms) }}
targets: ${{ fromJson(needs.test.outputs.targets) }}
steps:
- uses: actions/checkout@v4
# - name: Free up disk space
# # kics-scan ignore-line
# uses: ./.github/actions/free-space
# with:
# deleteDotnet: 'true'
# deleteAndroid: 'true'
# kics-scan ignore-line
- uses: docker/[email protected]
name: Set up QEMU
# kics-scan ignore-line
- uses: docker/[email protected]
name: Set up Docker Buildx
# with:
# buildkitd-flags: "--debug"
# # driver: ${{ github.event_name == 'pull_request' && 'docker' || 'docker-container' }}
# # driver: docker-container
# # driver-opts: image=moby/buildkit:v0.12.2
# Login against a container registry
# https://github.com/docker/login-action
# kics-scan ignore-line
- uses: docker/[email protected]
name: Login to ${{ env.REGISTRY }}
with:
registry: ${{ env.REGISTRY }}
username: ${{ vars.DOCKERHUB_USER || github.repository_owner }}
password: ${{ secrets.DOCKERHUB_TOKEN || secrets.GITHUB_TOKEN }}
logout: true
# Get the distro version to use for the cache tag
- name: get distro version
id: get_version
run: |
distro_version="${{ matrix.targets }}"
echo "distro_version=${distro_version/ubuntu-act-/}" >> "$GITHUB_OUTPUT"
# Get the architecture to use for the cache tag
- name: get arch
id: cache-arch
run: >-
printf "cache_arch=%s\n" "$(
docker run
--quiet
--platform ${{ matrix.platforms }}
--rm
"${PULL_IMAGE}"
/bin/bash -c "dpkg --print-architecture"
)" >> "$GITHUB_OUTPUT"
env:
PULL_IMAGE: ubuntu:${{ steps.get_version.outputs.distro_version }}
# Bake the image
# kics-scan ignore-line
- uses: docker/[email protected]
name: Build and Push
id: bake
with:
files: docker-bake.hcl
targets: ${{ matrix.targets }}
set: |
*.tags=
*.platform=${{ matrix.platforms }}
*.cache-from=type=registry,ref=${{ env.REGISTRY_IMAGE }}:cache-${{ steps.get_version.outputs.distro_version }}-${{ steps.cache-arch.outputs.cache_arch }}
*.cache-to=type=registry,ref=${{ env.REGISTRY_IMAGE }}:cache-${{ steps.get_version.outputs.distro_version }}-${{ steps.cache-arch.outputs.cache_arch }},mode=max
*.output=type=image,"name=${{ env.REGISTRY_IMAGE }}",push-by-digest=true,name-canonical=true,push=${{ github.event_name != 'pull_request' }}
env:
REGISTRY_IMAGE: ${{ format('{0}/{1}/ubuntu-act', env.REGISTRY, env.REGISTRY_USER) }}
TARGET_PLATFORM_PAIR: ${{ format('{0}-{1}', matrix.targets, steps.cache-arch.outputs.cache_arch) }}
- name: Export digest
run: |
mkdir -p /tmp/digests/${{ matrix.targets }}
digest="$(echo '${{ steps.bake.outputs.metadata }}' | jq -r '."${{ matrix.targets }}"["containerimage.digest"]')"
touch "/tmp/digests/${{ matrix.targets }}/${digest#sha256:}"
- name: Upload digest
uses: actions/upload-artifact@v3
with:
name: digests
path: /tmp/digests/${{ matrix.targets }}/*
if-no-files-found: error
retention-days: 1
# # vulnerability scanning to verify PRs
# - name: Docker Scout
# id: docker-scout
# uses: docker/[email protected]
# if: github.event_name == 'pull_request'
# with:
# platform: ${{ matrix.platforms }}
# command: quickview
# image: ${{ env.FROM_IMAGE_PATH }}
# type: archive
# to: ${{ env.TO_TAG }}
# ignore-unchanged: true
# only-severities: critical
# write-comment: ${{ github.actor != 'nektos/act' }}
# summary: ${{ github.actor != 'nektos/act' }}
# github-token: ${{ secrets.GITHUB_TOKEN }}
# organization: ${{ vars.DOCKERHUB_USER || github.repository_owner }}
merge:
runs-on: ubuntu-latest
if: github.event_name != 'pull_request'
strategy:
matrix:
targets: ${{ fromJson(needs.test.outputs.targets) }}
needs:
- test
- build
steps:
# - name: Download meta bake definition
# uses: actions/download-artifact@v3
# with:
# name: bake-meta
# path: /tmp
- uses: actions/checkout@v4
- name: Download digests
uses: actions/download-artifact@v3
with:
name: digests
path: /tmp/digests/${{ matrix.targets }}
- name: Set up Docker Buildx
# kics-scan ignore-line
uses: docker/[email protected]
# with:
# driver: ${{ github.event_name == 'pull_request' && 'docker' || 'docker-container' }}
# driver: docker-container
# driver-opts: image=moby/buildkit:v0.12.2
# install: true
# use: true
# cleanup: true
# Login against a container registry
# https://github.com/docker/login-action
# kics-scan ignore-line
- uses: docker/[email protected]
name: Login to ${{ env.REGISTRY }}
with:
registry: ${{ env.REGISTRY }}
username: ${{ vars.DOCKERHUB_USER || github.repository_owner }}
password: ${{ secrets.DOCKERHUB_TOKEN || secrets.GITHUB_TOKEN }}
logout: true
- name: bake metadata
id: bake-meta
run: docker buildx bake ${{ matrix.targets }} --print > /tmp/${{ matrix.targets }}.json
- name: Create manifest list and push
working-directory: /tmp/digests/${{ matrix.targets }}
run: |
docker buildx imagetools create \
$(jq -cr '.target[].tags | map(select(startswith("${{ env.REGISTRY}}")) | "-t " + .) | join(" ")' </tmp/${{ matrix.targets }}.json) \
$(printf '${{ env.REGISTRY_IMAGE }}@sha256:%s ' *)
env:
REGISTRY_IMAGE: ${{ format('{0}/{1}/ubuntu-act', env.REGISTRY, env.REGISTRY_USER) }}
- name: Inspect image
run: docker buildx imagetools inspect "$(jq -r '.target."${{ matrix.targets }}".tags[0]' < /tmp/${{ matrix.targets }}.json)"
approve-pr:
name: Approve PR
runs-on: ubuntu-latest
needs: [build]
if: github.actor != 'nektos/act' && contains(fromJson('["mauwii","dependabot[bot]"]'), github.triggering_actor) && github.event_name == 'pull_request' && needs.build.result == 'success'
permissions:
contents: read
pull-requests: write
actions: write
steps:
# approve the PR (there is still a code-owner review necessary)
- name: Approve PR
run: gh pr review --approve "$PR_URL"
env:
PR_URL: ${{github.event.pull_request.html_url}}
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
# auto merge dependabot PRs
- name: Merge DependaBot
if: github.actor == 'dependabot[bot]' && needs.build.result == 'success'
run: gh pr merge --auto --merge "$PR_URL"
env:
PR_URL: ${{github.event.pull_request.html_url}}
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}