-
Notifications
You must be signed in to change notification settings - Fork 11
444 lines (403 loc) · 17.8 KB
/
docker-build.yml
File metadata and controls
444 lines (403 loc) · 17.8 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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
name: DockerBuild
on:
# The documentation for 'merge_group' leaves a lot to be desired, but
# it appears as if they are more similar to a 'push' then a 'pull_request'
# when it comes to the general details about how the function and what
# information you have available when they are running.
merge_group:
types: [checks_requested]
push:
branches:
- develop
- main
tags:
- "*"
# tag is pr-<number>
pull_request:
release:
types:
- created
- published
- released
concurrency:
# This concurrency group ensures that only one job in the group runs at a time.
# If a new job is triggered, the previous one will be canceled.
group: docker-${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: ${{ github.event_name != 'release' || github.ref != 'refs/heads/develop' || github.ref != 'refs/heads/main' || !startsWith(github.ref, 'refs/tags/v') }}
env:
REGISTRY_PROD_ADDR: ghcr.io
IMAGE_NAME: ${{ github.repository }}/cloudzero-agent
UNTESTED_IMAGE_NAME: "Cloudzero/untested-cloudzero/untested-cloudzero-agent"
jobs:
docker-build:
name: Build Docker image for ${{ matrix.platform }}
strategy:
fail-fast: false
matrix:
platform:
- linux/amd64
- linux/arm64
runs-on: ${{ matrix.platform == 'linux/amd64' && 'ubuntu-latest' || matrix.platform == 'linux/arm64' && 'ubuntu-24.04-arm' }}
permissions:
contents: read
packages: write
id-token: write
outputs:
image-repo: ${{ env.REGISTRY_PROD_ADDR }}
image-path: ${{ env.UNTESTED_IMAGE_NAME }}
image-tag: ${{ steps.meta.outputs.version }}
steps:
# Checkout the repository code
- name: SETUP - Checkout
id: checkout_code
uses: actions/checkout@v5
- name: Prepare environment for current platform
id: prepare
run: |
platform="${{ matrix.platform }}"
echo "PLATFORM_PAIR=${platform//\//-}" >> "$GITHUB_ENV"
- name: Set up Docker Context for Buildx
id: buildx-context
run: |
docker context create builders
- # Install buildx for multi-platform builds
name: SETUP - Docker Buildx
id: install_buildx
uses: docker/setup-buildx-action@4d04d5d9486b7bd6fa91e7baf45bbb4f8b9deedd # v4.0.0
with:
endpoint: builders
driver-opts: network=host
platforms: ${{ matrix.platform }}
- name: Get raw repo name
run: |
echo "REPO_NAME=${GITHUB_REPOSITORY#"$GITHUB_REPOSITORY_OWNER"/}" >> "$GITHUB_ENV"
export REPO_NAME="${GITHUB_REPOSITORY#"$GITHUB_REPOSITORY_OWNER"/}"
# Format the image names for OCI compliance (all lowercase)
- name: INPUT PREP - image name formatting
id: image_name
run: |
IMAGE_NAME="${{ env.IMAGE_NAME }}"
echo "UNTESTED_IMAGE_NAME=${UNTESTED_IMAGE_NAME,,}" >>"${GITHUB_ENV}"
echo "IMAGE_NAME=${IMAGE_NAME,,}" >>"${GITHUB_ENV}"
# Extract metadata (tags, labels) the docker image build
# No tags of other metadata at this stage, as we are pushing by digest
- name: INPUT PREP - Basic Docker metadata for initial build
id: meta
uses: docker/metadata-action@030e881283bb7a6894de51c315a6bfe6a94e05cf # v6.0.0
with:
# ONLY use the untested registry address for the image until it is tested
images: ${{ env.REGISTRY_PROD_ADDR }}/${{ env.UNTESTED_IMAGE_NAME }}
- name: Login to GitHub Container Registry
uses: docker/login-action@v4
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: INPUT PREP - Set build time revision
run: |
REVISION=$(git rev-parse --short HEAD)
TAG="${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}"
BUILD_TIME=$(date -u +'%Y-%m-%dT%H:%M:%SZ')
{
echo "REVISION=${REVISION}"
echo "TAG=${TAG}"
echo "BUILD_TIME=${BUILD_TIME}"
} >> "${GITHUB_ENV}"
- name: TEST - Build image
id: build_image
uses: docker/build-push-action@bcafcacb16a39f128d818304e6c9c0c18556b85f # v7.1.0
env:
VALIDATOR_DOCKERFILE: docker/Dockerfile
VALIDATOR_CONTEXT: .
with:
platforms: ${{ matrix.platform }}
outputs: type=image,name=${{ env.REGISTRY_PROD_ADDR }}/${{ env.UNTESTED_IMAGE_NAME }},push-by-digest=true,name-canonical=true,push=true,oci-mediatypes=true
cache-from: type=gha,scope=${{ github.repository }}-${{ github.ref_name }}-${{ matrix.platform }}
cache-to: type=gha,mode=max,scope=${{ github.repository }}-${{ github.ref_name }}-${{ matrix.platform }}
context: ${{ env.VALIDATOR_CONTEXT }}
file: ${{ env.VALIDATOR_DOCKERFILE }}
build-args: |
BUILD_TIME=${{ env.BUILD_TIME }}
REVISION=${{ env.REVISION }}
TAG=${{ env.TAG }}
- name: Export digest
run: |
mkdir -p ${{ runner.temp }}/digests
digest="${{ steps.build_image.outputs.digest }}"
touch "${{ runner.temp }}/digests/${digest#sha256:}"
- name: Upload digest
uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f # v7.0.0
with:
name: digests-${{ env.PLATFORM_PAIR }}
path: ${{ runner.temp }}/digests/*
if-no-files-found: error
retention-days: 1
docker-merge:
# This job merges the Docker manifests for the different platforms built in the previous job.
name: Merge Docker manifests
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
id-token: write
needs: ["docker-build"]
steps:
- name: Download digests
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
with:
path: ${{ runner.temp }}/digests
pattern: digests-*
merge-multiple: true
# Format the image names for OCI compliance (all lowercase)
- name: INPUT PREP - image name formatting
id: image_name
run: |
IMAGE_NAME="${{ env.IMAGE_NAME }}"
echo "UNTESTED_IMAGE_NAME=${UNTESTED_IMAGE_NAME,,}" >>"${GITHUB_ENV}"
echo "IMAGE_NAME=${IMAGE_NAME,,}" >>"${GITHUB_ENV}"
- name: Login to GitHub Container Registry
uses: docker/login-action@v4
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@4d04d5d9486b7bd6fa91e7baf45bbb4f8b9deedd # v4.0.0
with:
driver-opts: |
network=host
# Extract metadata (tags, labels) the docker image build
# This is the full metadata for the manifest
- name: INPUT PREP - Extract Docker metadata from git repository
id: meta
uses: docker/metadata-action@030e881283bb7a6894de51c315a6bfe6a94e05cf # v6.0.0
env:
VALIDATOR_IMAGE_DESCRIPTION: "CloudZero Agent Validator"
with:
# ONLY use the untested registry address for the image until it is tested
images: ${{ env.REGISTRY_PROD_ADDR }}/${{ env.UNTESTED_IMAGE_NAME }}
# Tag generation rules:
# 1. branch name (used for develop or main)
# 2. PR number (used for PRs)
# 3. version to match the semver pattern for the chart
tags: |
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
labels: |
maintainer=CloudZero
org.opencontainers.image.description=${{ env.VALIDATOR_IMAGE_DESCRIPTION }}
org.opencontainers.image.vendor=CloudZero
image.name=${{ env.REGISTRY_PROD_ADDR }}/${{ env.IMAGE_NAME }}
# https://github.com/docker/metadata-action?tab=readme-ov-file#latest-tag
# should only occur when a semver or raw when we are on master
flavor: |
latest=false
- name: Get execution timestamp with RFC3339 format
# This step gets the current execution timestamp in RFC3339 format.
# It uses the date command to get the current UTC time and formats it as a string.
# The timestamp is used for annotating the Docker manifest list.
id: timestamp
run: |
echo "timestamp=$(date -u +"%Y-%m-%dT%H:%M:%SZ")" >> "$GITHUB_OUTPUT"
- name: Create manifest list and push
# This step creates a manifest list for the Docker images built for different platforms.
# It uses the docker buildx imagetools create command to create the manifest list.
# The manifest list is annotated with metadata such as description, creation timestamp, and source URL.
# The annotations are obtained from the metadata generated in the previous steps.
# The manifest list is pushed to the GitHub Container Registry (GHCR) with the specified tags.
working-directory: ${{ runner.temp }}/digests
id: manifest-annotate
continue-on-error: true
run: |
ls -lFa
# Create arrays for arguments to avoid word splitting issues
readarray -t tags < <(jq -cr '.tags[]' <<< "$DOCKER_METADATA_OUTPUT_JSON")
readarray -t digests < <(printf '%s\n' *)
# Build the command with proper argument arrays
docker_args=()
for tag in "${tags[@]}"; do
docker_args+=("-t" "$tag")
done
for digest in "${digests[@]}"; do
docker_args+=("${{ env.REGISTRY_PROD_ADDR }}/${{ env.UNTESTED_IMAGE_NAME }}@sha256:$digest")
done
docker buildx imagetools create \
"${docker_args[@]}" \
--annotation='index:org.opencontainers.image.description=${{ github.event.repository.description }}' \
--annotation='index:org.opencontainers.image.created=${{ steps.timestamp.outputs.timestamp }}' \
--annotation='index:org.opencontainers.image.url=${{ github.event.repository.url }}' \
--annotation='index:org.opencontainers.image.source=${{ github.event.repository.url }}'
- name: Create manifest list and push without annotations
# This step creates a manifest list for the Docker images built for different platforms.
# It uses the docker buildx imagetools create command to create the manifest list.
# The manifest list is created without annotations if the previous step fails.
# The manifest list is pushed to the GitHub Container Registry (GHCR) with the specified tags.
if: steps.manifest-annotate.outcome == 'failure'
working-directory: /tmp/digests
run: |
# Create arrays for arguments to avoid word splitting issues
readarray -t tags < <(jq -cr '.tags[]' <<< "$DOCKER_METADATA_OUTPUT_JSON")
readarray -t digests < <(printf '%s\n' *)
# Build the command with proper argument arrays
docker_args=()
for tag in "${tags[@]}"; do
docker_args+=("-t" "$tag")
done
for digest in "${digests[@]}"; do
docker_args+=("${{ env.REGISTRY_PROD_ADDR }}/${{ env.UNTESTED_IMAGE_NAME }}@sha256:$digest")
done
docker buildx imagetools create "${docker_args[@]}"
- name: Inspect image
# This step inspects the created manifest list to verify its contents.
# It uses the docker buildx imagetools inspect command to display information about the manifest list.
# The inspection output will show the platforms and tags associated with the manifest list.
id: inspect
run: |
docker buildx imagetools inspect '${{ env.REGISTRY_PROD_ADDR }}/${{ env.UNTESTED_IMAGE_NAME }}:${{ steps.meta.outputs.version }}'
- name: SECURITY - Grype Docker Image Scan
uses: anchore/scan-action@v7
with:
image: ${{ env.REGISTRY_PROD_ADDR }}/${{ env.UNTESTED_IMAGE_NAME }}:${{ steps.meta.outputs.version }}
fail-build: true
severity-cutoff: high
k8s-version-matrix-tests:
# These should match the permissions in the called workflow.
permissions:
contents: read
packages: read
needs: ["docker-build", "docker-merge"]
uses: ./.github/workflows/test-matrix-k8s.yaml
with:
image-repo: ${{ needs.docker-build.outputs.image-repo }}
image-path: ${{ needs.docker-build.outputs.image-path }}
image-tag: ${{ needs.docker-build.outputs.image-tag }}
secrets: inherit
replicated-compatibility-matrix-tests:
# These should match the permissions in the called workflow.
permissions:
contents: read
packages: read
needs: ["docker-build", "docker-merge"]
# We primarily want to run this when we are in a merge queue.
if: github.event_name == 'merge_group'
uses: ./.github/workflows/test-matrix-replicated-k8s-clusters.yaml
with:
image-repo: ${{ needs.docker-build.outputs.image-repo }}
image-path: ${{ needs.docker-build.outputs.image-path }}
image-tag: ${{ needs.docker-build.outputs.image-tag }}
secrets: inherit
test-versions-status-reporting:
runs-on: ubuntu-latest
permissions:
statuses: write
needs: ["k8s-version-matrix-tests"]
if: ${{ always() }}
steps:
- uses: spkane/commit-status-updater@a794d785537a8c22dec94d11ceb034021a8fd114
if: ${{ needs.k8s-version-matrix-tests.outputs.version-test-status == 'success' }}
with:
name: "k8s-version-matrix-tests"
status: "success"
- uses: spkane/commit-status-updater@a794d785537a8c22dec94d11ceb034021a8fd114
if: ${{ needs.k8s-version-matrix-tests.outputs.version-test-status != 'success' }}
with:
name: "k8s-version-matrix-tests"
status: "failure"
test-replicated-status-reporting:
runs-on: ubuntu-latest
permissions:
statuses: write
needs: ["replicated-compatibility-matrix-tests"]
if: ${{ always() && github.event_name == 'merge_group' }}
steps:
- uses: spkane/commit-status-updater@a794d785537a8c22dec94d11ceb034021a8fd114
if: ${{ needs.replicated-compatibility-matrix-tests.outputs.cluster-test-status == 'success' }}
with:
name: "replicated-compatibility-matrix-tests"
status: "success"
- uses: spkane/commit-status-updater@a794d785537a8c22dec94d11ceb034021a8fd114
if: ${{ needs.replicated-compatibility-matrix-tests.outputs.cluster-test-status != 'success' }}
with:
name: "replicated-compatibility-matrix-tests"
status: "failure"
###########################################################################
# PRODUCTION ONLY STEPS BEYOND THIS POINT
#
release-image:
needs:
[
"docker-build",
"docker-merge",
"k8s-version-matrix-tests",
"replicated-compatibility-matrix-tests",
]
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
id-token: write
steps:
# Format the image names for OCI compliance (all lowercase)
- name: INPUT PREP - image name formatting
id: image_name
run: |
IMAGE_NAME="${{ env.IMAGE_NAME }}"
echo "UNTESTED_IMAGE_NAME=${UNTESTED_IMAGE_NAME,,}" >>"${GITHUB_ENV}"
echo "IMAGE_NAME=${IMAGE_NAME,,}" >>"${GITHUB_ENV}"
# install regctl for registry management operations
- name: PRODUCTION STEP - Install Regctl for registry management
if: github.event_name == 'release' || github.ref == 'refs/heads/develop' || github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v')
id: install_regctl
uses: iarekylew00t/regctl-installer@v4
# Login to the production registry
- name: PRODUCTION STEP - login to container registry
if: github.event_name == 'release' || github.ref == 'refs/heads/develop' || github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v')
id: prod_registry_login
run: |
echo "${{ secrets.GITHUB_TOKEN }}" | \
regctl registry login ${{ env.REGISTRY_PROD_ADDR }} \
--user "${{ github.actor }}" \
--pass-stdin
# Promote the untested image to production
# only allow on main, develop branches, or a version tag
- name: PRODUCTION STEP - Publish Image to Production
if: github.event_name == 'release' || github.ref == 'refs/heads/develop' || github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v')
id: prod_publish_image
run: |
regctl image copy \
${{ env.REGISTRY_PROD_ADDR }}/${{ env.UNTESTED_IMAGE_NAME }}:${{ needs.docker-build.outputs.image-tag }} \
${{ env.REGISTRY_PROD_ADDR }}/${{ env.IMAGE_NAME }}:${{ needs.docker-build.outputs.image-tag }}
if [[ ${{ needs.docker-build.outputs.image-tag }} =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
regctl image copy \
${{ env.REGISTRY_PROD_ADDR }}/${{ env.UNTESTED_IMAGE_NAME }}:${{ needs.docker-build.outputs.image-tag }} \
${{ env.REGISTRY_PROD_ADDR }}/${{ env.IMAGE_NAME }}:latest
fi
docker-build-status-reporting:
runs-on: ubuntu-latest
permissions:
statuses: write
needs:
[
"docker-build",
"docker-merge",
"k8s-version-matrix-tests",
"replicated-compatibility-matrix-tests",
"release-image",
]
if: ${{ always() }}
steps:
- uses: spkane/commit-status-updater@a794d785537a8c22dec94d11ceb034021a8fd114
if: |
!contains(needs.*.result, 'failure') &&
!contains(needs.*.result, 'cancelled')
with:
name: "docker-build"
status: "success"
- uses: spkane/commit-status-updater@a794d785537a8c22dec94d11ceb034021a8fd114
if: |
contains(needs.*.result, 'failure') ||
contains(needs.*.result, 'cancelled')
with:
name: "docker-build"
status: "failure"