forked from whoan/docker-build-with-cache-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entrypoint.sh
executable file
·108 lines (89 loc) · 2.6 KB
/
entrypoint.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
#!/usr/bin/env bash
set -e
# helper functions
_exit_if_empty() {
local var_name=${1}
local var_value=${2}
if [ -z "$var_value" ]; then
echo "Missing input $var_name" >&2
exit 1
fi
}
_get_max_stage_number() {
sed -nr 's/^([0-9]+): Pulling from.+/\1/p' "$PULL_STAGES_LOG" |
sort -n |
tail -n 1
}
_get_stages() {
grep -EB1 '^Step [0-9]+/[0-9]+ : FROM' "$BUILD_LOG" |
sed -rn 's/ *-*> (.+)/\1/p'
}
_get_full_image_name() {
echo ${INPUT_REGISTRY:+$INPUT_REGISTRY/}${INPUT_IMAGE_NAME}
}
# action steps
check_required_input() {
_exit_if_empty USERNAME "${INPUT_USERNAME}"
_exit_if_empty PASSWORD "${INPUT_PASSWORD}"
_exit_if_empty IMAGE_NAME "${INPUT_IMAGE_NAME}"
_exit_if_empty IMAGE_TAG "${INPUT_IMAGE_TAG}"
}
login_to_registry() {
echo "${INPUT_PASSWORD}" | docker login -u "${INPUT_USERNAME}" --password-stdin "${INPUT_REGISTRY}"
}
pull_cached_stages() {
docker pull --all-tags "$(_get_full_image_name)"-stages 2> /dev/null | tee "$PULL_STAGES_LOG" || true
}
build_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_image_name)-stages:{1..$max_stage}")
echo "Use cache: $cache_from"
fi
# build image using cache
set -x
docker build \
$cache_from \
--tag "$(_get_full_image_name)":${INPUT_IMAGE_TAG} \
--file ${INPUT_CONTEXT}/${INPUT_DOCKERFILE} \
${INPUT_BUILD_EXTRA_ARGS} \
${INPUT_CONTEXT} | tee "$BUILD_LOG"
set +x
}
push_git_tag() {
[[ "$GITHUB_REF" =~ /tags/ ]] || return 0
local git_tag=${GITHUB_REF##*/tags/}
local image_with_git_tag
image_with_git_tag="$(_get_full_image_name)":$git_tag
docker tag "$(_get_full_image_name)":${INPUT_IMAGE_TAG} "$image_with_git_tag"
docker push "$image_with_git_tag"
}
push_image_and_stages() {
# push image
docker push "$(_get_full_image_name)":${INPUT_IMAGE_TAG}
push_git_tag
# push each building stage
stage_number=1
for stage in $(_get_stages); do
stage_image=$(_get_full_image_name)-stages:$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)
stage_image=$(_get_full_image_name)-stages:$stage_number
docker tag "$(_get_full_image_name)":${INPUT_IMAGE_TAG} $stage_image
docker push $stage_image
}
logout_from_registry() {
docker logout "${INPUT_REGISTRY}"
}
check_required_input
login_to_registry
pull_cached_stages
build_image
if [ "$INPUT_PUSH_IMAGE_AND_STAGES" = true ]; then
push_image_and_stages
fi
logout_from_registry