-
Notifications
You must be signed in to change notification settings - Fork 15
358 lines (319 loc) · 12.4 KB
/
docker_build.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
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
#
# Author: Hari Sekhon
# Date: 2022-01-27 18:38:27 +0000 (Thu, 27 Jan 2022)
#
# vim:ts=2:sts=2:sw=2:et
#
# https://github.com/HariSekhon/GitHub-Actions
#
# If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback
#
# https://www.linkedin.com/in/HariSekhon
#
# ============================================================================ #
# D o c k e r B u i l d
# ============================================================================ #
# Docker Build, Tag & Push to one or more repos
#
# Supports:
#
# - DockerHub
# - GHCR - GitHub Container Registry
# - ACR - Azure Container Registry
# - ECR - AWS Elastic Container Registry
# - GCR - Google Container Registry
# - GAR - Google Artifact Registry
# - GitLab Registry
# - Quay.io Registry
---
name: Docker Build
on: # yamllint disable-line rule:truthy
workflow_call:
inputs:
repo_tags:
description: full repository:tag list to push to, space or line separated eg. username/repo:<tag> (defaults to DockerHub), ghcr.io/$GITHUB_REPOSITORY_OWNER/$IMAGE:latest,
type: string
required: true
context:
description: the Docker context directory for the docker build
type: string
default: .
file:
description: the Dockerfile to build from. Defaults to Dockerfile inside the context. If specifying context, needs to include the context path prefix
type: string
#default: Dockerfile # breaks auto-pathing when specifying context, you'd have to remember to set file to $context/Dockerfile yourself, better to omit this
max-cache:
description: Use external cache image to capture multi-stage build cache, uses GHCR as a max cache external registry (ECR doesn't support this at time of writing)
type: boolean
default: false
build-cache:
description: The build cache tag to use
type: string
default: buildcache
dockerfile-repo:
description: A separate repo where the Dockerfile for the build can be found. Will be checked out first to a directory of the same name
type: string
required: false
dockerfile-ref:
description: The branch from dockerfile-repo to checkout (defaults to the default branch if not specified)
type: string
required: false
debug:
type: string
required: false
default: false
secrets:
ACR_REGISTRY:
# <registry-name>.azurecr.io
required: false
AZURE_CLIENT_ID:
required: false
AZURE_CLIENT_SECRET:
required: false
AWS_ACCESS_KEY_ID:
required: false
AWS_SECRET_ACCESS_KEY:
required: false
DOCKERHUB_USER:
required: false
DOCKERHUB_TOKEN:
required: false
GAR_JSON_KEY:
required: false
GAR_REGISTRY:
# europe-docker.pkg.dev / us-docker.pkg.dev / asia-docker.pkg.dev
required: false
GCR_JSON_KEY:
required: false
GCR_REGISTRY:
# gcr.io / eu.gcr.io
required: false
GITLAB_USER:
required: false
GITLAB_PASSWORD:
required: false
permissions:
contents: read
packages: write
defaults:
run:
shell: bash -euxo pipefail {0}
env:
REPO_TAGS: ${{ inputs.repo_tags }}
BUILDCACHE: ${{ inputs.build-cache }}
DOCKERFILE_REPO: ${{ inputs.dockerfile-repo }}
DOCKERHUB_USER: ${{ secrets.DOCKERHUB_USER }}
DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }}
ACR_REGISTRY: ${{ secrets.ACR_REGISTRY }}
AZURE_CLIENT_ID: ${{ secrets.AZURE_CLIENT_ID }}
AZURE_CLIENT_SECRET: ${{ secrets.AZURE_CLIENT_SECRET }}
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
GAR_JSON_KEY: ${{ secrets.GAR_JSON_KEY }}
GAR_REGISTRY: ${{ secrets.GAR_JSON_KEY }}
GCR_JSON_KEY: ${{ secrets.GCR_JSON_KEY }}
GCR_REGISTRY: ${{ secrets.GCR_REGISTRY }}
GITLAB_USER: ${{ secrets.GITLAB_USER }}
GITLAB_PASSWORD: ${{ secrets.GITLAB_PASSWORD }}
QUAY_USER: ${{ secrets.QUAY_USER }}
QUAY_TOKEN: ${{ secrets.QUAY_TOKEN }}
DEBUG: ${{ inputs.debug == true || github.event.inputs.debug == 'true' || '' }}
jobs:
# XXX: workaround for GitHub Actions not supporting secrets in step conditionals:
#
# https://github.com/github-community/community/discussions/17983
#
# https://github.com/actions/runner/issues/520
#
determine_registries:
name: Determine Registries
runs-on: ubuntu-latest
outputs:
dockerhub: ${{ steps.setvar.outputs.dockerhub }}
ghcr: ${{ steps.setvar.outputs.ghcr }}
ecr: ${{ steps.setvar.outputs.ecr }}
ecr_registry: ${{ steps.setvar.outputs.ecr_registry }}
acr: ${{ steps.setvar.outputs.acr }}
gcr: ${{ steps.setvar.outputs.gcr }}
gar: ${{ steps.setvar.outputs.gar }}
gitlab: ${{ steps.setvar.outputs.gitlab }}
quay: ${{ steps.setvar.outputs.quay }}
steps:
- name: Set output variables
id: setvar
run: |
if [ -n "$DOCKERHUB_USER" ] &&
[ -n "$DOCKERHUB_TOKEN" ]; then
echo "DockerHub registry credentials set"
echo "dockerhub=true" >> $GITHUB_OUTPUT
fi
if [ -n "$ACR_REGISTRY" ]] &&
[ -n "$AZURE_CLIENT_ID" ]] &&
[ -n "$AZURE_CLIENT_SECRET" ]] &&
grep -qi '\.azurecr\.io' <<< "$REPO_TAGS"; then
echo "Azure Container Registry credentials set"
echo "acr=true" >> $GITHUB_OUTPUT
fi
if [ -n "$AWS_ACCESS_KEY_ID" ]] &&
[ -n "$AWS_SECRET_ACCESS_KEY" ]] &&
grep -qi '\.amazonaws\.com' <<< "$REPO_TAGS"; then
echo "AWS ECR registry credentials set"
echo "ecr=true" >> $GITHUB_OUTPUT
AWS_ECR_REGISTRY="$(grep -om1 '[^[:space:]]*\.amazonaws\.com' <<< "$REPO_TAGS")"
echo "ecr_registry=$AWS_ECR_REGISTRY" >> $GITHUB_OUTPUT
fi
if [ -n "$GAR_JSON_KEY" ]] &&
[ -n "$GAR_REGISTRY" ]] &&
grep -qi '\.pkg\.dev' <<< "$REPO_TAGS"; then
echo "Google Artifact Registry credentials set"
echo "gar=true" >> $GITHUB_OUTPUT
fi
if [ -n "$GCR_JSON_KEY" ]] &&
[ -n "$GCR_REGISTRY" ]] &&
grep -qi '\.gcr\.io' <<< "$REPO_TAGS"; then
echo "Google Container Registry credentials set"
echo "gcr=true" >> $GITHUB_OUTPUT
fi
if [ -n "$GITLAB_USER" ]] &&
[ -n "$GITLAB_PASSWORD" ]] &&
grep -qi '\.gitlab\.com' <<< "$REPO_TAGS"; then
echo "GitLab registry credentials set"
echo "gitlab=true" >> $GITHUB_OUTPUT
fi
if [ -n "$QUAY_USER" ]] &&
[ -n "$QUAY_TOKEN" ]] &&
grep -qi '\.quay\.io' <<< "$REPO_TAGS"; then
echo "Quay.io registry credentials set"
echo "quay=true" >> $GITHUB_OUTPUT
fi
docker_build:
name: Docker Build
needs: determine_registries
runs-on: ubuntu-latest
# better done in the calling workflow to prevent manual docker builds on the wrong branches
#if: github.ref_name == 'master' || github.ref_name == 'main' || contains(github.ref_name, 'docker')
steps:
- name: Environment
run: env | sort
- uses: actions/checkout@v3
with:
submodules: 'true' # requires Git 2.18+ to be installed first
- name: Generate environment variable DOCKERFILE_CHECKOUT_PATH
if: inputs.dockerfile-repo
run: |
DOCKERFILE_CHECKOUT_PATH="$DOCKERFILE_REPO"
DOCKERFILE_CHECKOUT_PATH="${DOCKERFILE_CHECKOUT_PATH##*/}"
echo "DOCKERFILE_CHECKOUT_PATH=$DOCKERFILE_CHECKOUT_PATH" >> "$GITHUB_ENV"
- name: Generate environment variable AWS_ECR_REGISTRY
#if: contains(inputs.repo_tags, '.amazonaws.com')
if: needs.determine_registries.outputs.ecr
env:
REGISTRY: needs.determine_registries.outputs.ecr_registry
run: echo "AWS_ECR_REGISTRY=$REGISTRY" >> "$GITHUB_ENV"
- name: Git Checkout Dockerfile repo
if: inputs.dockerfile-repo
uses: actions/checkout@v3
with:
repository: ${{ inputs.dockerfile-repo }}
ref: ${{ inputs.dockerfile-ref }}
submodules: 'true' # requires Git 2.18+ to be installed first
path: ${{ env.DOCKERFILE_CHECKOUT_PATH }}
- name: Docker Login GHCR
env:
TOKEN: ${{github.token}}
run: docker login ghcr.io -u "$GITHUB_REPOSITORY_OWNER" --password-stdin <<< "$TOKEN"
- name: Docker Login to DockerHub
if: needs.determine_registries.outputs.dockerhub == 'true'
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKERHUB_USER }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Login to ACR
if: needs.determine_registries.outputs.acr == 'true'
uses: docker/login-action@v1
with:
registry: ${{ secrets.ACR_REGISTRY }}
username: ${{ secrets.AZURE_CLIENT_ID }}
password: ${{ secrets.AZURE_CLIENT_SECRET }}
- name: Login to AWS ECR
if: needs.determine_registries.outputs.ecr == 'true'
uses: docker/login-action@v1
with:
registry: ${{ env.AWS_ECR_REGISTRY }}
username: ${{ secrets.AWS_ACCESS_KEY_ID }}
password: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
- name: Login to GCR
if: needs.determine_registries.outputs.gcr == 'true'
uses: docker/login-action@v1
with:
registry: ${{ secrets.GCR_REGISTRY }}
username: _json_key
password: ${{ secrets.GCR_JSON_KEY }}
- name: Login to GAR
if: needs.determine_registries.outputs.gar == 'true'
uses: docker/login-action@v1
with:
registry: ${{ secrets.GAR_REGISTRY }}
username: _json_key
password: ${{ secrets.GAR_JSON_KEY }}
- name: Login to GitLab
if: needs.determine_registries.outputs.gitlab == 'true'
uses: docker/login-action@v1
with:
registry: registry.gitlab.com
username: ${{ secrets.GITLAB_USER }}
password: ${{ secrets.GITLAB_PASSWORD }}
- name: Login to Quay.io
if: needs.determine_registries.outputs.quay == 'true'
uses: docker/login-action@v1
with:
registry: quay.io
username: ${{ secrets.QUAY_USER }}
password: ${{ secrets.QUAY_TOKEN }}
- name: Generate environment variable CACHE_FROM
run: |
echo "CACHE_FROM<<EOF" >> "$GITHUB_ENV"
for repo_tag in $(tr '\n' ' ' <<< "$REPO_TAGS"); do
echo "type=registry,ref=$repo_tag" >> "$GITHUB_ENV"
done
echo "EOF" >> "$GITHUB_ENV"
- name: Generate environment variable DOCKER_IMAGE_CACHE
if: ${{ inputs.max-cache }}
env:
REPO: ${{ github.repository }}
run: echo "DOCKER_IMAGE_CACHE=ghcr.io/$REPO" >> "$GITHUB_ENV"
#run: echo "DOCKER_IMAGE_CACHE=$AWS_ECR_REGISTRY/$APP-cache" >> $GITHUB_ENV # doesn't work, ECR simply doesn't support it
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
# XXX: Normal build, use inline cache when max-cache input is not set to 'true' - fine for most purposes except for multi-stage builds
- name: Docker Build & Push
if: ${{ ! inputs.max-cache }}
uses: docker/build-push-action@v2
with:
context: ${{ inputs.context }}
file: ${{ inputs.file }}
tags: ${{ env.REPO_TAGS }}
push: true
cache-from: ${{ env.CACHE_FROM }}
cache-to: type=inline
# call like so:
#
# jobs:
# docker_build:
# uses: HariSekhon/GitHub-Actions/.github/workflows/docker_build.yaml@master
# with:
# max-cache: true
# ...
#
- name: Docker Build & Push with external build cache
if: ${{ inputs.max-cache }}
uses: docker/build-push-action@v2
with:
context: ${{ inputs.context }}
file: ${{ inputs.file }}
tags: ${{ env.REPO_TAGS }}
push: true
cache-from: |
type=registry,ref=${{env.REPO}}:${{env.BUILDCACHE}}
${{ env.CACHE_FROM }}
cache-to: type=registry,ref=${{env.DOCKER_IMAGE_CACHE}}:${{env.BUILDCACHE}},mode=max # XXX: fails to push to ECR which doesn't support external registry cache export, so set to GHCR for cache instead