-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathbuild_test.sh
executable file
·86 lines (75 loc) · 2.26 KB
/
build_test.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
#!/usr/bin/env bash
set -Eeu
repo_root() { git rev-parse --show-toplevel; }
readonly SH_DIR="$(repo_root)/sh"
source "${SH_DIR}/echo_versioner_env_vars.sh"
export $(echo_versioner_env_vars)
# - - - - - - - - - - - - - - - - - - - - - - - -
remove_old_image_layers()
{
echo; echo Removing old image layers
local -r dil=$(docker image ls --format "{{.Repository}}:{{.Tag}}")
remove_all_but_latest "${dil}" "${CYBER_DOJO_NGINX_IMAGE}"
}
# - - - - - - - - - - - - - - - - - - - - - - - -
remove_all_but_latest()
{
local -r docker_image_ls="${1}"
local -r name="${2}"
for image in `echo "${docker_image_ls}" | grep "${name}:"`
do
if [ "${image}" != "${name}:latest" ]; then
if [ "${image}" != "${name}:<none>" ]; then
docker image rm "${image}"
fi
fi
done
docker system prune --force
}
# - - - - - - - - - - - - - - - - - - - - - - - -
build_tagged_image()
{
echo; echo Building tagged image
docker build \
--no-cache \
--build-arg COMMIT_SHA=${CYBER_DOJO_NGINX_SHA} \
--tag ${CYBER_DOJO_NGINX_IMAGE}:${CYBER_DOJO_NGINX_TAG} \
"$(repo_root)"
}
# - - - - - - - - - - - - - - - - - - - - - - - -
tag_image_to_latest()
{
echo; echo Tagging image to :latest
docker tag "${CYBER_DOJO_NGINX_IMAGE}:${CYBER_DOJO_NGINX_TAG}" "${CYBER_DOJO_NGINX_IMAGE}:latest"
}
# - - - - - - - - - - - - - - - - - - - - - - - -
check_embedded_SHA_env_var()
{
echo; echo Checking SHA env-var embedded inside image matches git commit sha
local -r expected="$(git_commit_sha)"
local -r actual="$(sha_inside_image)"
if [ "${expected}" != "${actual}" ]; then
echo "ERROR: unexpected env-var inside image ${CYBER_DOJO_NGINX_IMAGE}:${CYBER_DOJO_NGINX_TAG}"
echo "expected: 'SHA=${expected}'"
echo " actual: 'SHA=${actual}'"
exit 42
fi
}
# - - - - - - - - - - - - - - - - - - - - - - - -
show_SHA_env_var()
{
echo
echo "CYBER_DOJO_NGINX_SHA=${CYBER_DOJO_NGINX_SHA}"
echo "CYBER_DOJO_NGINX_TAG=${CYBER_DOJO_NGINX_TAG}"
}
# - - - - - - - - - - - - - - - - - - - - - - - -
sha_inside_image()
{
docker run --rm "${CYBER_DOJO_NGINX_IMAGE}:${CYBER_DOJO_NGINX_TAG}" sh -c 'echo ${SHA}'
}
# - - - - - - - - - - - - - - - - - - - - - - - -
remove_old_image_layers
build_tagged_image
tag_image_to_latest
check_embedded_SHA_env_var
show_SHA_env_var