-
Notifications
You must be signed in to change notification settings - Fork 4
/
action.yml
397 lines (358 loc) · 11.5 KB
/
action.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
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
name: Build and push Docker image
description: Deploy to Kubernetes using Helm
inputs:
type:
description: |
choose one of: docker, buildx, compose, bake
This will effect what some of the other inputs mean.
default: docker
repo:
description: Repository name, defaults to git repo name
required: false
registry:
description: Registry name, defaults to ecr if on aws.
required: false
platforms:
description: Build multi-arch image with BuildX
required: false
build-args:
description: List of build args like key=value.
required: false
tags:
description: Extra space delimited tags to add to the image
required: false
tag-strategy:
description: Space delimited computed tag strategy. Choose one or more of DATE, GIT_REF, and/or GIT_SHA. Date is in UTC.
required: false
default: GIT_REF GIT_SHA
target:
description: Target stage to build
required: false
context:
description: Build context
required: false
default: "."
dockerfile:
description: Dockerfile path
required: false
file:
description: Buildfile path. This if for compose or bake. Builder when using non default file or path.
required: false
output:
description: The output arg for docker and buildx.
required: false
push:
description: Push image to registry
required: false
default: "false"
cache:
description: Cache image layers if buildx enabled.
required: false
default: "true"
cache-scope:
description: The scope key to identify the cache. Useful when this is used more than once in a workflow.
required: false
default: buildkit
extra-args:
description: Extra args to pass to buildx
required: false
docker-username:
description: Docker user
required: false
docker-password:
description: Docker password
required: false
outputs:
image:
description: Docker image
value: ${{ steps.image-env.outputs.image }}
repo:
description: Repository Name
value: ${{ steps.image-env.outputs.repo }}
ref:
description: Branch name
value: ${{ steps.image-env.outputs.ref }}
uri:
description: Image URI
value: ${{ steps.image-env.outputs.uri }}
runs:
using: composite
steps:
# first login. Assume ECR for now.
- name: Login to Amazon ECR
id: login-ecr
if: env.AWS_ENABLED == 'true'
uses: aws-actions/amazon-ecr-login@v2
# if not ecr then login to dockerhub
- name: Login to DockerHub
id: login-dockerhub
if: env.AWS_ENABLED != 'true' && inputs.docker-username
uses: docker/login-action@v3
with:
username: ${{ inputs.docker-username }}
password: ${{ inputs.docker-password }}
# start with checking on the builder kind
- name: Builder Setup
id: setup
shell: bash
env:
BUILD_TYPE: ${{ inputs.type }}
run: |
# make available to other steps
echo "name=$BUILD_TYPE" >> $GITHUB_OUTPUT
# if BUILD_TYPE is buildx or bake
if [[ "$BUILD_TYPE" == "buildx" || "$BUILD_TYPE" == "bake" ]]; then
echo "buildx_enabled=true" >> $GITHUB_OUTPUT
else
echo "buildx_enabled=false" >> $GITHUB_OUTPUT
fi
# if BUILD_TYPE is compose or bake then build_from_file is enabled
if [[ "$BUILD_TYPE" == "compose" || "$BUILD_TYPE" == "bake" ]]; then
echo "build_from_file=true" >> $GITHUB_OUTPUT
else
echo "build_from_file=false" >> $GITHUB_OUTPUT
fi
# based on the builder, output the build command
case $BUILD_TYPE in
docker)
echo "build_cmd=build" >> $GITHUB_OUTPUT
;;
buildx)
echo "build_cmd=buildx build" >> $GITHUB_OUTPUT
;;
compose)
echo "build_cmd=compose build" >> $GITHUB_OUTPUT
;;
bake)
echo "build_cmd=buildx bake" >> $GITHUB_OUTPUT
;;
esac
# export useful image environment variables and tags
- name: Image Environment
id: image-env
shell: bash
env:
REGISTRY: ${{ inputs.registry }}
REPO: ${{ inputs.repo }}
run: |
# if AWS_ENABLED use ecr registry
if [[ "$AWS_ENABLED" == "true" ]]; then
REGISTRY="${{ steps.login-ecr.outputs.registry }}"
fi
# if neither registry or repo is set dockerhub naming style matching github
if [[ -z "$REGISTRY" && -z "$REPO" ]]; then
IMAGE="${GITHUB_REPOSITORY}"
REPO="$(basename $GITHUB_REPOSITORY)"
else
# otherwise use the repo name to match the image repo
if [[ -z "$REPO" ]]; then
REPO="$(basename $GITHUB_REPOSITORY)"
fi
# set the image name
IMAGE="${REGISTRY}/${REPO}"
fi
# export short sha for tags
GIT_SHA="$(git rev-parse --short HEAD)"
echo "Short sha is $GIT_SHA"
DATE="$(date -u +"%Y%m%d%H%M")"
# ref name
## TODO: maybe use this GITHUB_REF_NAME
# the sed part handles dependabot issues with slash names
GIT_REF="$(echo ${GITHUB_REF##*/} | sed -e 's/\//_/g')"
# this is for the output
echo "registry=${REGISTRY}" >> $GITHUB_OUTPUT
echo "image=${IMAGE}" >> $GITHUB_OUTPUT
echo "repo=${REPO}" >> $GITHUB_OUTPUT
echo "git_ref=${GIT_REF}" >> $GITHUB_OUTPUT
echo "git_sha=${GIT_SHA}" >> $GITHUB_OUTPUT
echo "uri=${IMAGE}:${GIT_SHA}" >> $GITHUB_OUTPUT
echo "date=${DATE}" >> GITHUB_OUTPUT
# setup the base image
- name: Setup CLI Args
id: cli-args
shell: bash
if: steps.setup.outputs.build_from_file == 'false'
env:
EXTRA_ARGS: ${{ inputs.extra-args }}
BUILD_ARGS: ${{ inputs.build-args }}
PUSH: ${{ inputs.push }}
DOCKERFILE: ${{ inputs.dockerfile }}
BUILDFILE: ${{ inputs.file }}
TARGET: ${{ inputs.target }}
CONTEXT: ${{ inputs.context }}
OUTPUT: ${{ inputs.output }}
CACHE: ${{ inputs.cache }}
CACHE_SCOPE: ${{ inputs.cache-scope }}
BUILDX_ENABLED: ${{ steps.setup.outputs.buildx_enabled }}
PLATFORMS: ${{ inputs.platforms }}
MULTIARCH: "false"
EXTRA_TAGS: ${{ inputs.tags }}
FANCY_TAGS: ${{ inputs.tag-strategy }}
IMAGE: ${{ steps.image-env.outputs.image }}
GIT_SHA: ${{ steps.image-env.outputs.git_sha }}
GIT_REF: ${{ steps.image-env.outputs.git_ref }}
DATE: ${{ steps.image-env.outputs.date }}
run: |
ARGS=($EXTRA_ARGS)
# add dockerfile param if given
if [[ -n "$DOCKERFILE" ]]; then
ARGS+=(--file "${DOCKERFILE}")
elif [[ -n "$BUILDFILE" ]]; then
ARGS+=(--file "${BUILDFILE}")
fi
# if target is set then add the arg
if [[ -n "$TARGET" ]]; then
ARGS+=(--target "${TARGET}")
fi
# if buildx is enabled then add the platforms
if [[ "$BUILDX_ENABLED" == "true" && -n "$PLATFORMS" ]]; then
ARGS+=(--platform "${PLATFORMS}")
MULTIARCH="true"
fi
# if cache is true
if [[ "$CACHE" == "true" ]]; then
CACHE_CONFIG="type=gha,scope=${CACHE_SCOPE}"
ARGS+=(--cache-to ${CACHE_CONFIG},mode=max)
ARGS+=(--cache-from ${CACHE_CONFIG})
fi
# add output if given
if [[ -n "$OUTPUT" ]]; then
ARGS+=(--output "${OUTPUT}")
fi
# if push is true then add the arg
if [[ "$PUSH" == "true" ]]; then
ARGS+=(--push)
fi
# add any custom build args
build_args=($BUILD_ARGS)
for arg in ${build_args[*]}; do
ARGS+=(--build-arg "${arg}")
done
# Add the fancy tags
COMPUTED_FANCY_TAGS=()
ALLOWED_FANCY_TAGS=(DATE GIT_REF GIT_SHA)
for tag in "${ALLOWED_FANCY_TAGS[@]}"; do
if [[ "${FANCY_TAGS}" =~ "$tag" ]]; then
COMPUTED_FANCY_TAGS+=("${!tag}")
fi
done
# add all the tags with image as args
TAGS=(
$EXTRA_TAGS
"${COMPUTED_FANCY_TAGS[*]}"
)
for tag in ${TAGS[*]}; do
ARGS+=(--tag "${IMAGE}:${tag}")
done
# finally add the context
ARGS+=("${CONTEXT}")
echo "multiarch=${MULTIARCH}" >> $GITHUB_OUTPUT
echo "tags=${TAGS[*]}" >> $GITHUB_OUTPUT
echo "args=${ARGS[*]}" >> $GITHUB_OUTPUT
- name: Setup Buildfile Args
id: file-args
shell: bash
if: steps.setup.outputs.build_from_file == 'true'
env:
EXTRA_ARGS: ${{ inputs.extra-args }}
PUSH: ${{ inputs.push }}
TARGET: ${{ inputs.target }}
CACHE: ${{ inputs.cache }}
BUILD_TYPE: ${{ inputs.type }}
BUILD_ARGS: ${{ inputs.build-args }}
BUILD_FILE: ${{ inputs.file }}
run: |
ARGS=($EXTRA_ARGS)
# if push is true then add the arg and the builder is bake
if [[ "$PUSH" == "true" ]]; then
ARGS+=(--push)
fi
# optionally disable cache
if [[ "$CACHE" == "false" ]]; then
ARGS+=(--no-cache)
fi
# if the builder is compose it can have build args too
if [[ "$BUILD_TYPE" == "compose" ]]; then
build_args=($BUILD_ARGS)
for arg in ${build_args[*]}; do
ARGS+=(--build-arg "${arg}")
done
fi
# if a build file is given then add it
if [[ -n "$BUILD_FILE" ]]; then
ARGS+=(--file "${BUILD_FILE}")
fi
# if target is set then add the arg
if [[ -n "$TARGET" ]]; then
ARGS+=("${TARGET}")
fi
echo "args=${ARGS[*]}" >> $GITHUB_OUTPUT
- name: Expose GitHub Runtime
if: steps.setup.outputs.buildx_enabled == 'true'
uses: crazy-max/ghaction-github-runtime@v3
- name: Setup QEMU
id: setup-qemu
if: steps.setup.outputs.buildx_enabled == 'true'
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
id: buildx
if: steps.setup.outputs.buildx_enabled == 'true'
uses: docker/setup-buildx-action@v3
- name: Build Docker Command
id: docker
shell: bash
env:
BUILD_CMD: ${{ steps.setup.outputs.build_cmd }}
CLI_ARGS: ${{ steps.cli-args.outputs.args || steps.file-args.outputs.args }}
run: |
DOCKER_COMMAND="docker ${BUILD_CMD} ${CLI_ARGS}"
echo "command=$DOCKER_COMMAND" >> $GITHUB_OUTPUT
- name: Docker Build Preview
id: preview
shell: bash
if: >-
steps.setup.outputs.build_from_file == 'true' &&
inputs.type != 'compose'
env:
REGISTRY: ${{ steps.image-env.outputs.registry }}
IMAGE: ${{ steps.image-env.outputs.image || '' }}
REPO: ${{ steps.image-env.outputs.repo }}
GIT_SHA: ${{ steps.image-env.outputs.git_sha }}
GIT_REF: ${{ steps.image-env.outputs.git_ref }}
run: ${{ steps.docker.outputs.command }} --print
- name: Docker Build
id: build
shell: bash
env:
REGISTRY: ${{ steps.image-env.outputs.registry }}
IMAGE: ${{ steps.image-env.outputs.image || '' }}
REPO: ${{ steps.image-env.outputs.repo }}
GIT_SHA: ${{ steps.image-env.outputs.git_sha }}
GIT_REF: ${{ steps.image-env.outputs.git_ref }}
run: ${{ steps.docker.outputs.command }}
- name: Image Summary
shell: bash
if: always()
env:
REPO: ${{ steps.image-env.outputs.repo || '' }}
REGISTRY: ${{ steps.image-env.outputs.registry || '' }}
IMAGE: ${{ steps.image-env.outputs.image || '' }}
TAGS: ${{ steps.cli-args.outputs.tags || '' }}
DOCKER_COMMAND: ${{ steps.docker.outputs.command }}
run: |
cat <<EOF >> $GITHUB_STEP_SUMMARY
## Docker Command
\`\`\`bash
$DOCKER_COMMAND
\`\`\`
## Image Summary
**Image**: ${IMAGE}
**Tags**:
EOF
for tag in $TAGS; do
cat <<EOF >> $GITHUB_STEP_SUMMARY
\`\`\`bash
${IMAGE}:${tag}
\`\`\`
EOF
done