forked from whoan/docker-build-with-cache-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entrypoint.sh
executable file
·251 lines (214 loc) · 6.19 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
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
#!/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_docker_hub() {
[ -z "$INPUT_REGISTRY" ] || [[ "$INPUT_REGISTRY" =~ \.docker\.(com|io)(/|$) ]]
}
_is_github_registry() {
[ "$INPUT_REGISTRY" = docker.pkg.github.com ]
}
_is_gcloud_registry() {
[ "$INPUT_REGISTRY" = gcr.io ]
}
_is_aws_ecr() {
[[ $INPUT_REGISTRY =~ ^.+\.dkr\.ecr\.([a-z0-9-]+)\.amazonaws\.com$ ]]
is_aws_ecr=$?
aws_region=${BASH_REMATCH[1]}
return $is_aws_ecr
}
_image_name_contains_namespace() {
[[ "$INPUT_IMAGE_NAME" =~ / ]]
}
_set_namespace() {
if ! _image_name_contains_namespace; then
if _is_docker_hub; then
NAMESPACE=$INPUT_USERNAME
elif _is_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
fi
# aws-ecr does not need a namespace
fi
# set namespace to all lower, capital letters are not supported
NAMESPACE=${NAMESPACE,,}
}
_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/}${NAMESPACE:+$NAMESPACE/}${INPUT_IMAGE_NAME}
}
_tag() {
local tag
tag="${1:?You must provide a 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"
}
_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_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)
echo -e "\nPushing stage: $stage_number"
stage_image=$(_get_full_image_name)-stages:$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 \
amazon/aws-cli:2.0.7 --region $aws_region "$@"
}
_login_to_aws_ecr() {
_aws ecr get-authorization-token --output text --query 'authorizationData[].authorizationToken' | base64 -d | cut -d: -f2 | docker login --username AWS --password-stdin $INPUT_REGISTRY
}
_create_aws_ecr_repos() {
_aws ecr create-repository --repository-name "$INPUT_IMAGE_NAME" 2>&1 | grep -v RepositoryAlreadyExistsException
_aws ecr create-repository --repository-name "$INPUT_IMAGE_NAME"-stages 2>&1 | grep -v RepositoryAlreadyExistsException
return 0
}
# action steps
init_variables() {
DUMMY_IMAGE_NAME=my_awesome_image
PULL_STAGES_LOG=pull-stages-output.log
BUILD_LOG=build-output.log
# 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_required_input() {
echo -e "\n[Action Step] Checking required input..."
_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
if _is_aws_ecr; then
_login_to_aws_ecr && _create_aws_ecr_repos && return 0
else
echo "${INPUT_PASSWORD}" | docker login -u "${INPUT_USERNAME}" --password-stdin "${INPUT_REGISTRY}" \
&& return 0
fi
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..."
docker pull --all-tags "$(_get_full_image_name)"-stages 2> /dev/null | tee "$PULL_STAGES_LOG" || true
}
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_image_name)-stages:{1..$max_stage}")
echo "Use cache: $cache_from"
fi
# build image using cache
set -o pipefail
set -x
docker build \
$cache_from \
--tag $DUMMY_IMAGE_NAME \
--file ${INPUT_CONTEXT}/${INPUT_DOCKERFILE} \
${INPUT_BUILD_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
echo "Tagging: $tag"
_tag $tag
done
}
push_image_and_stages() {
echo -e "\n[Action Step] Pushing image..."
if ! $INPUT_PUSH_IMAGE_AND_STAGES; then
echo "Not pushing" >&2
[ $INPUT_PUSH_IMAGE_AND_STAGES = false ]
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() {
if [ "$not_logged_in" ]; then
return
fi
echo -e "\n[Action Step] Log out from registry..."
docker logout "${INPUT_REGISTRY}"
}
# run the action
init_variables
check_required_input
login_to_registry
pull_cached_stages
build_image
tag_image
push_image_and_stages
logout_from_registry
echo "::set-output name=FULL_IMAGE_NAME::$(_get_full_image_name)"