-
Notifications
You must be signed in to change notification settings - Fork 1
170 lines (157 loc) · 6.51 KB
/
docker-build.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
name: DockerBuild
on:
push:
branches:
- develop
- main
tags:
- '*'
# tag is pr-<number>
pull_request:
release:
types:
- created
- published
- released
env:
REGISTRY_LOCAL_ADDR: localhost:5000
REGISTRY_PROD_ADDR: ghcr.io
IMAGE_NAME: ${{ github.repository }}/cloudzero-agent-validator
jobs:
docker:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
id-token: write
services:
registry:
image: registry:2
ports:
- 5000:5000
steps:
# Checkout the repository code
- name: SETUP - Checkout
id: checkout_code
uses: actions/checkout@v4
- # Install buildx for multi-platform builds
name: SETUP - Docker Buildx
id: install_buildx
uses: docker/setup-buildx-action@6524bf65af31da8d45b59e8c27de4bd072b392f5 # v3.8.0
with:
driver-opts: network=host
# Sanity Check: Validate the k8s and Registry is Running
- name: SANITY CHECK - Registry are running
id: validate_kind_install
run: |
docker pull busybox
docker tag busybox ${{ env.REGISTRY_LOCAL_ADDR }}/localbusybox
docker push ${{ env.REGISTRY_LOCAL_ADDR }}/localbusybox
# Format the image name to OCI compatable format
- name: INPUT PREP - image name formatting
id: image_name
run: |
IMAGE_NAME=${{ env.IMAGE_NAME }}
echo "IMAGE_NAME=${IMAGE_NAME,,}" >>${GITHUB_ENV}
# Extract metadata (tags, labels) the docker image build
- name: INPUT PREP - Extract Docker metadata from git repository
id: meta
uses: docker/metadata-action@369eb591f429131d6889c46b94e711f089e6ca96 # v5.6.1
env:
VALIDATOR_IMAGE_DESCRIPTION: "CloudZero Agent Validator"
with:
# ONLY use the local registry address for the image until it is tested
images: ${{ env.REGISTRY_LOCAL_ADDR }}/${{ env.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 whtn a semver or raw when we are on master
flavor: |
latest=false
- name: INPUT PREP - Set build time revision
run: |
REVISION=$(git rev-parse --short HEAD)
TAG=$(echo "${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}")
BUILD_TIME=$(date -u +'%Y-%m-%dT%H:%M:%SZ')
echo "REVISION=${REVISION}" >>${GITHUB_ENV}
echo "TAG=${TAG}" >>${GITHUB_ENV}
echo "BUILD_TIME=${BUILD_TIME}" >>${GITHUB_ENV}
- name: TEST - Build image
id: build_image
uses: docker/build-push-action@48aba3b46d1b1fec4febb7c5d0c644b249a11355 # v6.10.0
env:
PLATFORMS: "linux/amd64,linux/arm64"
VALIDATOR_DOCKERFILE: docker/Dockerfile
VALIDATOR_CONTEXT: .
with:
push: true
context: ${{ env.VALIDATOR_CONTEXT }}
file: ${{ env.VALIDATOR_DOCKERFILE }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
platforms: ${{ env.PLATFORMS }}
cache-from: type=gha
cache-to: type=gha,mode=max
build-args: |
BUILD_TIME=${{ env.BUILD_TIME }}
REVISION=${{ env.REVISION }}
TAG=${{ env.TAG }}
- name: SECURITY - Grype Docker Image Scan
uses: anchore/scan-action@v6
with:
image: ${{ env.REGISTRY_LOCAL_ADDR }}/${{ env.IMAGE_NAME }}:${{ steps.meta.outputs.version }}
fail-build: true
severity-cutoff: high
- name: SECURITY - Trivy Docker Image Scan
uses: aquasecurity/trivy-action@0.29.0
with:
image-ref: ${{ env.REGISTRY_LOCAL_ADDR }}/${{ env.IMAGE_NAME }}:${{ steps.meta.outputs.version }}
format: 'table'
exit-code: '1'
ignore-unfixed: true
vuln-type: 'os,library'
severity: 'CRITICAL,HIGH'
###########################################################################
# PRODUCTION ONLY STEPS BEYOND THIS POINT
#
# 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@v3
# Login to product docker 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
# Copy the image from the local registry
# to the production registry (retagging at the same time)
# 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 registry set --tls=disabled ${{ env.REGISTRY_LOCAL_ADDR }}
regctl image copy \
${{ env.REGISTRY_LOCAL_ADDR }}/${{ env.IMAGE_NAME }}:${{ steps.meta.outputs.version }} \
${{ env.REGISTRY_PROD_ADDR }}/${{ env.IMAGE_NAME }}:${{ steps.meta.outputs.version }}
if [[ ${{ steps.meta.outputs.version }} =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
regctl image copy \
${{ env.REGISTRY_LOCAL_ADDR }}/${{ env.IMAGE_NAME }}:${{ steps.meta.outputs.version }} \
${{ env.REGISTRY_PROD_ADDR }}/${{ env.IMAGE_NAME }}:latest
fi