forked from bazelbuild/continuous-integration
-
Notifications
You must be signed in to change notification settings - Fork 0
280 lines (240 loc) · 8.7 KB
/
build-ci-docker-images.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
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
# ## build-ci-docker-images
#
# Action to build and deploy Docker images from the CI Dockerfiles in the repo.
#
# ### Action Triggers
#
# The action is triggered by a push to the `main`/`master` or `testing` branch when it
# affects the CI `Dockerfile`s. It can also be triggered manually via the
# Actions web UI, the GH REST API or the GH CLI tool, e.g.
#
# gh workflow run build-ci-docker-images
#
# #### `push`
#
# When triggered by a push, it will dynamically determine the context and the
# targets for each `Dockerfile`, automatically removing some targets by default
# (`RE_TARGET_EXCLUDE` and `testimage`) and it will build a Docker image for
# each remaining target.
#
# For every image built it will push to the registry three image tags:
# * a `sha` tag (the short hash of the commit that triggered the push)
# * a `date` tag with the current date in ISO format (`YYYYMMDD`)
# * a `latest` tag
#
# #### `workflow_dispatch` (manual)
#
# When triggered by hand, the workflow will run by default "like a `push` run
# in `dry-run` mode".
#
# That is, it will still dynamically determine the context and the targets for
# each `Dockerfile` and it will automatically remove `RE_TARGET_EXCLUDE` but:
#
# 1. it won't exclude `testimage`, rather it will have `RE_TARGET_INCLUDE` set
# to only `testimage` by default (a very simple image to exercise the build
# that doesn't take much compute)
#
# 2. it will only build `linux/amd64` by default (to further reduce testing
# times and cost)
#
# 2. it will build the `testimage` but **it won't tag `latest` or push any of
# the image tags to the registry**.
#
# These defaults can be overriden by setting the `workflow_dispatch` input
# variables: `RE_TARGET_EXCLUDE`, `RE_TARGET_INCLUDE`, `TAG_DATE`, `TAG_LATEST`
# and `PUSH`. E.g.:
#
# gh workflow run build-ci-docker-images \
# -f RE_TARGET_INCLUDE=ubuntu2404 -f TAG_DATE=20241101
name: build-ci-docker-images
on:
push:
branches:
- main
- master
- testing
paths:
# NOTE: keep in-sync with env.SPARSE_PATH
- buildkite/docker/*/Dockerfile
workflow_dispatch:
inputs:
RE_TARGET_EXCLUDE:
description: |-
Filter out Docker targets matching this extended regex pattern
# NOTE: keep in-sync with env.RE_TARGET_EXCLUDE
default: ^centos7|^ubuntu16|nojdk
RE_TARGET_INCLUDE:
description: |-
Select Docker targets matching this extended regex pattern
# NOTE: keep in-sync with env.TEST_IMAGE
default: testimage
PLATFORMS:
default: linux/amd64
TAG_DATE:
description: Tag image date in ISO format (YYYYMMDD)
TAG_LATEST:
description: Tag image version as 'latest'
default: false
PUSH:
description: Push images to registry
default: false
env:
REGISTRY: ghcr.io
SPARSE_PATH: buildkite/docker
TEST_IMAGE: testimage
GH_EVENT_NAME: ${{ github.event_name }}
GH_REF_NAME: ${{ github.ref_name }}
GH_REPO: ${{ github.repository }}
GH_SHA: ${{ github.sha }}
GH_SHA_BEFORE: ${{ github.event.before }}
RE_TARGET_EXCLUDE: ${{ inputs.RE_TARGET_EXCLUDE || '^centos7|^ubuntu16|nojdk' }}
RE_TARGET_INCLUDE: ${{ inputs.RE_TARGET_INCLUDE }}
PLATFORMS: ${{ inputs.PLATFORMS || 'linux/amd64,linux/arm64' }}
TAG_DATE: ${{ inputs.TAG_DATE }}
TAG_LATEST: ${{ inputs.TAG_LATEST }}
PUSH: ${{ github.event_name == 'push' || inputs.PUSH }}
jobs:
setup-targets:
runs-on: ubuntu-latest
defaults:
run:
shell: bash --noprofile --norc -euo pipefail {0}
outputs:
targets: ${{ steps.define_targets.outputs.targets }}
targets_length: ${{ steps.define_targets.outputs.targets_length }}
steps:
- name: Sparse checkout SPARSE_PATH
uses: actions/checkout@v4
with:
sparse-checkout-cone-mode: false
sparse-checkout: |-
${{ env.SPARSE_PATH }}
- name: Get DOCKERFILES
run: |-
# NOTE:
# GH_SHA_BEFORE is empty on pushing the first commit of a new branch
# or when running manually via workflow_dispatch
if [[ -z $GH_SHA_BEFORE ]]; then
DOCKERFILES="$(ls $SPARSE_PATH/*/Dockerfile)"
else
DOCKERFILES="$(
git diff --name-only $GH_SHA_BEFORE $GH_SHA |
grep Dockerfile || {
# NOTE:
# this grep could fail if e.g. we are force-pushing a stack
# of commits where one or more commits do change Dockerfiles
# but there's no change to any Dockerfile between the last
# commit and this forced push.
echo "WARNING: EMPTY grep" >&2
true
}
)"
fi
DOCKERFILES_JSON="$(echo -n "$DOCKERFILES" | jq -R '.' | jq -sc '.' )"
echo "DOCKERFILES_JSON=$DOCKERFILES_JSON"
echo "DOCKERFILES_JSON=$DOCKERFILES_JSON" >> $GITHUB_ENV
- name: Define targets
id: define_targets
run: |-
set -euo pipefail
if [[ "$GH_EVENT_NAME" == "push" ]]; then
RE_TARGET_EXCLUDE="$RE_TARGET_EXCLUDE|$TEST_IMAGE"
fi
echo '>>> WTF'
echo "$DOCKERFILES_JSON" | jq -r 'join(" ")' || echo 'FAIL jq 1'
echo '>>> WTF'
DOCKERFILES=("$(echo "$DOCKERFILES_JSON" | jq -r 'join(" ")')")
echo "DOCKERFILES=${DOCKERFILES[@]}"
echo "DOCKERFILES_LENGTH=${#DOCKERFILES[@]}"
if [[ ${#DOCKERFILES[@]} -gt 0 ]]; then
TARGETS_LS="$(
grep -i '^FROM .* AS ' ${DOCKERFILES[@]} |
awk '{print $NF}' |
grep -E "$RE_TARGET_INCLUDE" |
grep -vE "$RE_TARGET_EXCLUDE" |
jq -R '.'
)"
else
TARGETS_LS=""
fi
TARGETS="$(echo "$TARGETS_LS" | jq -sc '.')"
TARGETS_LENGTH="$(echo "$TARGETS_LS" | jq -s 'length')"
echo "targets=$TARGETS"
echo "targets_length=$TARGETS_LENGTH"
echo "targets=$TARGETS" >> "$GITHUB_OUTPUT"
echo "targets_length=$TARGETS_LENGTH" >> "$GITHUB_OUTPUT"
build-and-publish-docker-images:
needs: setup-targets
if: ${{ false && needs.setup-targets.outputs.targets_length > 0 }}
runs-on: ubuntu-latest
strategy:
matrix:
target: ${{ fromJson(needs.setup-targets.outputs.targets) }}
defaults:
run:
shell: bash --noprofile --norc -euo pipefail {0}
steps:
- name: Sparse checkout SPARSE_PATH
uses: actions/checkout@v4
with:
sparse-checkout-cone-mode: false
sparse-checkout: |-
${{ env.SPARSE_PATH }}
- name: Set up dynamic env
env:
MATRIX_TARGET: ${{ matrix.target }}
run: |-
declare -A TAGS
TAGS[sha]="${GH_SHA::7}"
TAGS[date]="$TAG_DATE"
# set default date value if TAG_DATE is not set, is empty
# or is an empty string
if [[ -z "${TAGS[date]+isset}" || -z "${TAGS[date]// }" ]]; then
TAGS[date]="$(date +%Y%m%d)"
fi
if [[ "$GH_EVENT_NAME" == "push" || "$TAG_LATEST" == "true" ]]; then
TAGS[latest]="latest"
fi
if [[ "$REGISTRY" == "gcr.io" ]]; then
IMAGE_PREFIX="bazel-public"
elif [[ "$REGISTRY" == "ghcr.io" ]]; then
IMAGE_PREFIX="$GH_REPO"
else
echo "Invalid registry: $REGISTRY"
exit 1
fi
if [[ "$GH_REF_NAME" == "testing" ]]; then
IMAGE_PREFIX="$IMAGE_PREFIX/testing"
fi
IMAGE_NAME="$REGISTRY/$IMAGE_PREFIX/$MATRIX_TARGET"
DISTRO="${MATRIX_TARGET%%-*}"
CONTEXT="$SPARSE_PATH/$DISTRO"
{
for tag in ${!TAGS[@]}; do
echo "IMAGE_TAG_${tag^^}=$IMAGE_NAME:${TAGS[$tag]}"
done
echo "CONTEXT=$CONTEXT"
} >> $GITHUB_ENV
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to Registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push
uses: docker/build-push-action@v6
with:
context: ${{ env.CONTEXT }}
target: ${{ matrix.target }}
platforms: ${{ env.PLATFORMS }}
tags: |-
${{ env.IMAGE_TAG_SHA }}
${{ env.IMAGE_TAG_DATE }}
${{ env.IMAGE_TAG_LATEST }}
labels: |-
org.opencontainers.image.source=${{ github.repositoryUrl }}
push: ${{ env.PUSH }}