-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-base-image.sh
executable file
·152 lines (114 loc) · 3.41 KB
/
build-base-image.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
#!/bin/bash
REGISTRY=registry.docker.grnet.gr
usage_string="usage:
$ ./$(basename "$0") --tag TAG [-d DOCKERFILE] [--push] [--prod] [--no-push]
Build base image and optionally push to docker registry (${REGISTRY})
Name of image results from the Dockerfile after replacing the \"Dockerfile\"
prefix with \"ebsi\" and \".\"'s with \"-\", and then tagging it. If to be
pushed, it is further tagged thrice with respect to the registry: with the
provided tag, with the provided tag plus current timestamp, and as \"latest\".
For example, building from \"Dockerfile.base.dev\" with \"--tag test\"
produces the following tagged image:
ebsi-base-dev:test
If in push mode, the following tagged images are further created:
${REGISTRY}/ebsi-base-dev:test
${REGISTRY}/ebsi-base-dev:test-20211228164642
${REGISTRY}/ebsi-base-dev:latest
Arguments (required)
-t, --tag Docker tag (same for all images)
Options:
--dockerfile FILE Dockerfile to build from. Default: Dockerfile.base.dev
--push Push images to registry after build
--no-push Annules --push
-h, --help Show help message and exit
Examples
./$(basename "$0") --tag test
./$(basename "$0") --tag test --push
./$(basename "$0") --tag debug --no-push
./$(basename "$0") --tag ci --push --prod
"
usage() { echo -n "$usage_string" 1>&2; }
tag_for_registry() {
image=$1
tag=$2
timestamp=$3
echo "Tagging image ${image}:${tag} for registry"
docker image tag ${image}:${tag} ${REGISTRY}/${image}:${tag}
docker image tag ${image}:${tag} ${REGISTRY}/${image}:${tag}-${timestamp}
docker image tag ${image}:${tag} ${REGISTRY}/${image}:latest
}
build_image() {
dockerfile=$1
tag=$2
timestamp=$3
image=ebsi-$(echo $dockerfile | awk -F'.' '{st = index($0, ".");
print substr($0, st + 1)}' | tr '.' '-')
echo "Building image ${image}:${tag} from ${dockerfile}"
export DOCKER_DEFAULT_PLATFORM=linux/amd64
docker image build \
-t ${image}:${tag} \
-f ${dockerfile} \
.
if [ ${DO_PUSH} == true ] && [ ${NO_PUSH} == false ]; then
tag_for_registry $image $tag $timestamp
fi
}
push_image() {
image=$1
tag=$2
timestamp=$3
echo "Pushing image ${image} to registry"
docker push ${REGISTRY}/${image}:${tag}
docker push ${REGISTRY}/${image}:${tag}-${timestamp}
docker push ${REGISTRY}/${image}:latest
}
set -e
DOCKERFILE=Dockerfile.base.dev
DO_PUSH=false
NO_PUSH=false
dockerfiles=${DOCKERFILES}
while [[ $# -gt 0 ]]
do
arg="$1"
case $arg in
-t|--tag)
TAG=$2
shift
shift
;;
--dockerfile)
DOCKERFILE=$2
shift
shift
;;
--push)
DO_PUSH=true
shift
;;
--no-push)
NO_PUSH=true
shift
;;
-h|--help)
usage
exit 0
;;
*)
echo "[-] Invalid argument: $arg"
usage
exit 1
;;
esac
done
if [[ -z ${TAG} ]]; then
echo "[-] No tag provided"
usage
exit 1
fi
timestamp=${TIMESTAMP:-`date '+%Y%m%d%H%M%S'`} # '+%s' for unix time
build_image $DOCKERFILE $TAG $timestamp
if [ ${DO_PUSH} == true ] && [ ${NO_PUSH} == false ]; then
docker login ${REGISTRY}
push_image $image $TAG $timestamp
docker logout ${REGISTRY}
fi