forked from whoan/docker-build-with-cache-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker-build.sh
executable file
·382 lines (324 loc) · 9.57 KB
/
docker-build.sh
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
#!/usr/bin/env bash
set -e
# helper functions
_has_value() {
local var_name=${1}
local var_value=${2}
if [ -z "$var_value" ]; then
echo "INFO: Missing value $var_name" >&2
return 1
fi
}
_is_digitalocean_registry() {
[ "$INPUT_REGISTRY" = registry.digitalocean.com ]
}
_is_docker_hub() {
[ -z "$INPUT_REGISTRY" ] || [[ "$INPUT_REGISTRY" =~ \.docker\.(com|io)(/|$) ]]
}
_is_old_github_registry() {
[ "$INPUT_REGISTRY" = docker.pkg.github.com ]
}
_is_new_github_registry() {
[ "$INPUT_REGISTRY" = ghcr.io ]
}
_is_gcloud_registry() {
[[ "$INPUT_REGISTRY" =~ ^(.+\.)?gcr\.io$ ]]
}
_is_aws_ecr() {
_is_aws_ecr_private || _is_aws_ecr_public
}
_is_aws_ecr_private() {
[[ "$INPUT_REGISTRY" =~ ^.+\.dkr\.ecr\.([a-z0-9-]+)\.amazonaws\.com$ ]]
}
_is_aws_ecr_public() {
[[ "$INPUT_REGISTRY" =~ ^public.ecr.aws$ ]]
}
_get_aws_region() {
_is_aws_ecr_public && echo "us-east-1" && return
# tied to _is_aws_ecr_private implementation
_is_aws_ecr_private && echo "${BASH_REMATCH[1]}" && return
echo "Could not get AWS region" >&2
}
_image_name_contains_namespace() {
[[ "$INPUT_IMAGE_NAME" =~ / ]]
}
_set_namespace() {
if ! _image_name_contains_namespace; then
if _is_docker_hub || _is_new_github_registry; then
NAMESPACE=$INPUT_USERNAME
elif _is_old_github_registry; then
NAMESPACE=$GITHUB_REPOSITORY
elif _is_gcloud_registry; then
# take project_id from Json Key
NAMESPACE=$(echo "${INPUT_PASSWORD}" | sed -rn 's@.+project_id" *: *"([^"]+).+@\1@p' 2> /dev/null)
[ "$NAMESPACE" ] || return 1
elif _is_aws_ecr_public; then
NAMESPACE=$(_aws_get_public_ecr_registry_name)
fi
# aws-ecr (private) does not need a namespace
fi
# set namespace to all lower, capital letters are not supported
NAMESPACE=${NAMESPACE,,}
}
_get_max_stage_number() {
(( ${#tags[@]} == 0 )) || echo "${tags[-1]}"
}
_get_stages() {
grep -EB1 '^Step [0-9]+/[0-9]+ : FROM' "$BUILD_LOG" |
sed -rn 's/ *-*> (.+)/\1/p'
}
_get_image_namespace() {
echo ${INPUT_REGISTRY:+$INPUT_REGISTRY/}${NAMESPACE:+$NAMESPACE/}
}
_get_full_image_name() {
echo "$(_get_image_namespace)${INPUT_IMAGE_NAME}"
}
_get_stages_image_name() {
echo "${INPUT_STAGES_IMAGE_NAME:-${INPUT_IMAGE_NAME}-stages}"
}
_get_full_stages_image_name() {
echo "$(_get_image_namespace)$(_get_stages_image_name)"
}
_tag() {
local tag
tag="${1:?You must provide a tag}"
echo "Tag: $(_get_full_image_name):$tag"
docker tag "$DUMMY_IMAGE_NAME" "$(_get_full_image_name):$tag"
}
_push() {
local tag
tag="${1:?You must provide a tag}"
docker push "$(_get_full_image_name):$tag"
}
_must_push() {
if [ "$INPUT_PUSH_IMAGE_AND_STAGES" = on:push ]; then
[ "$GITHUB_EVENT_NAME" = push ]
return
fi
if [ "$INPUT_PUSH_IMAGE_AND_STAGES" = on:pull_request ]; then
[ "$GITHUB_EVENT_NAME" = pull_request ]
return
fi
$INPUT_PUSH_IMAGE_AND_STAGES
}
_push_git_tag() {
[[ "$GITHUB_REF" =~ /tags/ ]] || return 0
local git_tag=${GITHUB_REF##*/tags/}
echo -e "\nPushing git tag: $git_tag"
_tag "$git_tag"
_push "$git_tag"
}
_push_image_tags() {
local tag
for tag in "${INPUT_IMAGE_TAG[@]}"; do
echo "Pushing: $tag"
_push "$tag"
done
if [ "$INPUT_PUSH_GIT_TAG" = true ]; then
_push_git_tag
fi
}
_push_image_stages() {
local stage_number=1
local stage_image
for stage in $(_get_stages); do
echo -e "\nPushing stage: $stage_number"
stage_image=$(_get_full_stages_image_name):$stage_number
docker tag "$stage" "$stage_image"
docker push "$stage_image"
stage_number=$(( stage_number+1 ))
done
# push the image itself as a stage (the last one)
echo -e "\nPushing stage: $stage_number"
stage_image=$(_get_full_stages_image_name):$stage_number
docker tag "$DUMMY_IMAGE_NAME" "$stage_image"
docker push "$stage_image"
}
_aws() {
docker run --rm \
--env AWS_ACCESS_KEY_ID="$INPUT_USERNAME" \
--env AWS_SECRET_ACCESS_KEY="$INPUT_PASSWORD" \
--env AWS_SESSION_TOKEN="$INPUT_SESSION" \
amazon/aws-cli:2.1.14 --region "$(_get_aws_region)" "$@"
}
_aws_get_public_ecr_registry_name() {
_aws ecr-public describe-registries --output=text --query 'registries[0].aliases[0].name'
}
_aws_get_image_tags() {
mapfile -t tags < <(_aws ecr-public describe-image-tags --repository-name "$INPUT_IMAGE_NAME"-stages | jq ".imageTagDetails[].imageTag")
tags=( "${tags[@]#\"}" )
tags=( "${tags[@]%\"}" )
}
_login_to_aws_ecr() {
local array="[]"
if _is_aws_ecr_public; then
local ecr_public_suffix=-public
array=""
fi
_aws ecr${ecr_public_suffix} get-authorization-token --output text --query "authorizationData${array}.authorizationToken" |
base64 -d | cut -d: -f2 | docker login --username AWS --password-stdin "$INPUT_REGISTRY"
}
_create_aws_ecr_repos() {
if ! _must_push; then
return 0
fi
if _is_aws_ecr_public; then
local ecr_public_suffix=-public
fi
echo -e "\n[Action Step - AWS] Creating repositories (if needed)..."
_aws ecr${ecr_public_suffix} create-repository --repository-name "$INPUT_IMAGE_NAME" 2>&1 | grep -v RepositoryAlreadyExistsException
_aws ecr${ecr_public_suffix} create-repository --repository-name "$(_get_stages_image_name)" 2>&1 | grep -v RepositoryAlreadyExistsException
return 0
}
_docker_login() {
if _is_aws_ecr; then
{ _login_to_aws_ecr && _create_aws_ecr_repos; } || return 1
else
echo "${INPUT_PASSWORD}" | docker login -u "${INPUT_USERNAME}" --password-stdin "${INPUT_REGISTRY%%/*}" || return 1
fi
trap logout_from_registry EXIT
}
_parse_extra_args() {
# non-json
if ! [[ $INPUT_BUILD_EXTRA_ARGS =~ ^\{ ]]; then
return
fi
# json
declare -ga extra_args=()
local key
local value
while read -r key; do
for value in $(jq --raw-output "[.\"$key\"] | flatten[]" <<<"${INPUT_BUILD_EXTRA_ARGS}"); do
extra_args+=("$key")
extra_args+=("${value//\\n/
}")
done
done < <(jq --raw-output "keys[]" <<<"${INPUT_BUILD_EXTRA_ARGS}")
INPUT_BUILD_EXTRA_ARGS=""
}
# action steps
init_variables() {
DUMMY_IMAGE_NAME="$INPUT_IMAGE_NAME":tmp_tag_ignore
BUILD_LOG=build-output.log
if _is_aws_ecr; then
if [ -z "$INPUT_USERNAME" ]; then
INPUT_USERNAME=$AWS_ACCESS_KEY_ID
INPUT_PASSWORD=$AWS_SECRET_ACCESS_KEY
fi
if [ -z "$INPUT_SESSION" ]; then
INPUT_SESSION=$AWS_SESSION_TOKEN
fi
fi
if _is_digitalocean_registry; then
echo "Detected digitalocean registry."
INPUT_USERNAME=$DOCTL_TOKEN
INPUT_PASSWORD=$DOCTL_TOKEN
fi
# split tags (to allow multiple comma-separated tags)
IFS=, read -ra INPUT_IMAGE_TAG <<< "$INPUT_IMAGE_TAG"
if ! _set_namespace; then
echo "Could not set namespace" >&2
exit 1
fi
}
check_aws_cli() {
if _is_aws_ecr; then
_aws --version
fi
}
check_required_input() {
echo -e "\n[Action Step] Checking required input..."
#shellcheck disable=SC2128
_has_value IMAGE_NAME "${INPUT_IMAGE_NAME}" \
&& _has_value IMAGE_TAG "${INPUT_IMAGE_TAG}" \
&& return
exit 1
}
login_to_registry() {
echo -e "\n[Action Step] Log in to registry..."
if _has_value USERNAME "${INPUT_USERNAME}" && _has_value PASSWORD "${INPUT_PASSWORD}"; then
_docker_login && return 0
echo "Could not log in (please check credentials)" >&2
else
echo "No credentials provided" >&2
fi
not_logged_in=true
echo "INFO: Won't be able to pull from private repos, nor to push to public/private repos" >&2
}
pull_cached_stages() {
if [ "$INPUT_PULL_IMAGE_AND_STAGES" != true ]; then
return
fi
echo -e "\n[Action Step] Pulling image..."
if _is_aws_ecr_public; then
_aws_get_image_tags
local tag
for tag in "${tags[@]}"; do
docker pull "$(_get_full_stages_image_name)":"$tag" || true
done
else
local PULL_STAGES_LOG=pull-stages-output.log
docker pull --all-tags "$(_get_full_stages_image_name)" | tee "$PULL_STAGES_LOG" || true
mapfile -t tags < <(sed -nr 's/^([0-9]+): Pulling from.+/\1/p' "$PULL_STAGES_LOG" | sort -n)
if (( ${#tags[@]} == 0 )); then
echo "Expected error ^ if this is the first time you build the image" >&2
fi
fi
}
build_image() {
echo -e "\n[Action Step] Building image..."
max_stage=$(_get_max_stage_number)
# create param to use (multiple) --cache-from options
if [ "$max_stage" ]; then
cache_from=$(eval "echo --cache-from=$(_get_full_stages_image_name):{1..$max_stage}")
fi
_parse_extra_args
# build image using cache
set -o pipefail
set -x
# shellcheck disable=SC2068,SC2086
docker build \
$cache_from \
--tag "$DUMMY_IMAGE_NAME" \
--file "${INPUT_CONTEXT}"/"${INPUT_DOCKERFILE}" \
${INPUT_BUILD_EXTRA_ARGS} \
"${extra_args[@]}" \
"${INPUT_CONTEXT}" | tee "$BUILD_LOG"
set +x
}
tag_image() {
echo -e "\n[Action Step] Tagging image..."
local tag
for tag in "${INPUT_IMAGE_TAG[@]}"; do
_tag "$tag"
done
}
push_image_and_stages() {
echo -e "\n[Action Step] Pushing image..."
if ! _must_push; then
echo "Not pushing" >&2
# only stop the action on errors of custom commands set in the input variable
[ "$INPUT_PUSH_IMAGE_AND_STAGES" = false ] || [[ "$INPUT_PUSH_IMAGE_AND_STAGES" =~ ^on: ]]
return
fi
if [ "$not_logged_in" ]; then
echo "ERROR: Can't push when not logged in to registry. Set \"push_image_and_stages: false\" if you don't want to push" >&2
return 1
fi
_push_image_tags
_push_image_stages
}
logout_from_registry() {
echo -e "\n[Action Step] Log out from registry..."
docker logout "${INPUT_REGISTRY}"
}
# run the action
check_aws_cli
init_variables
check_required_input
login_to_registry
pull_cached_stages
build_image
tag_image
push_image_and_stages
echo "FULL_IMAGE_NAME=$(_get_full_image_name)" >> "$GITHUB_OUTPUT"