-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathstdlib.bash
88 lines (71 loc) · 1.92 KB
/
stdlib.bash
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
echoerr() {
echo "$@" 1>&2;
}
log_fatal() {
echoerr "In $(pwd)"
echoerr "${@}"
# use the last argument as the exit code
exit_code="${*: -1}"
if [[ "${exit_code}" =~ ^[0-9]+$ ]]; then
exit "${exit_code}"
fi
exit 1
}
read_build_args() {
read_list_property 'BUILD_ARGS'
for arg in ${result[@]+"${result[@]}"}; do
build_args+=("--build-arg=${arg}")
done
}
# read a plugin property of type [array, string] into a Bash array. Buildkite
# exposes a string value at BUILDKITE_PLUGIN_{NAME}_{KEY}, and array values at
# BUILDKITE_PLUGIN_{NAME}_{KEY}_{IDX}.
read_list_property() {
local base_name="BUILDKITE_PLUGIN_DOCKER_ECR_CACHE_${1}"
result=()
if [[ -n ${!base_name:-} ]]; then
result+=("${!base_name}")
fi
while IFS='=' read -r item_name _; do
if [[ ${item_name} =~ ^(${base_name}_[0-9]+) ]]; then
result+=("${!item_name}")
fi
done < <(env | sort)
}
get_default_image_name() {
echo "build-cache/${BUILDKITE_ORGANIZATION_SLUG}/${BUILDKITE_PIPELINE_SLUG}"
}
compute_tag() {
local docker_file="$1"
local sums
echoerr '--- Computing tag'
echoerr 'DOCKERFILE'
echoerr "+ ${docker_file}:${target:-"<target>"}"
sums="$(sha1sum "${docker_file}")"
sums+="$(echo "${target}" | sha1sum)"
echoerr 'ARCHITECTURE'
echoerr "+ $(uname -m)"
sums+="$(uname -m | sha1sum)"
echoerr 'BUILD_ARGS'
read_list_property 'BUILD_ARGS'
for arg in ${result[@]+"${result[@]}"}; do
echoerr "+ ${arg}"
# include underlying environment variable after echo
if [[ ${arg} != *=* ]]; then
arg+="=${!arg:-}"
fi
sums+="$(echo "${arg}" | sha1sum)"
done
# expand ** in cache-on properties
shopt -s globstar
echoerr 'CACHE_ON'
read_list_property 'CACHE_ON'
for glob in ${result[@]+"${result[@]}"}; do
echoerr "${glob}"
for file in ${glob}; do
echoerr "+ ${file}"
sums+="$(sha1sum "${file}")"
done
done
echo "${sums}" | sha1sum | cut -c-7
}