-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild_and_push
executable file
·137 lines (124 loc) · 3.1 KB
/
build_and_push
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
#!/bin/bash
set -e
declare -a build_tags
while getopts hlucbkpnat: opt; do
case $opt in
a)
build_all=y
;;
p)
push_tags=y
;;
n)
push_tags=n
;;
l)
list_tags=y
;;
t)
build_tags+=("$OPTARG")
;;
c)
no_cache="--no-cache"
;;
u)
no_cache=""
;;
b)
pull="--pull"
;;
k)
pull=""
;;
h)
echo "usage: $0 <options> "
echo
echo "Options:"
echo -e "\t -h:\t\t this help message"
echo -e "\t -l:\t\t list known tags and exit"
echo -e "\t -u|-c:\t\t use cache for build / clear cache before building."
echo -e "\t -b|-k:\t\t attempt to pull newer image / do not pull image if it exists"
echo -e "\t -p|-n:\t\t push / don't push tags to dockerhub."
echo -e "\t -a:\t\t build all tags / build a specific tag"
echo -e "\t -t <name>:\t build this tag name"
echo
echo "If you don't provide -a, list as many -t <tag_name> as you want"
echo "Tag names are ignored if -a is passed"
echo "All options (except -l and -h) are asked interactively if not provided"
echo
exit 0
;;
*)
echo Unknown flag
exit 1
;;
esac
done
shopt -s extglob
declare -a tags
for df in Dockerfile-*; do
tags+=("${df#@(Dockerfile-)}")
done
if [ "$list_tags" == 'y' ]; then
echo -e "Known tags to build:\n\t${tags[*]}"
exit 0
fi
if [ "${build_all}" == "" ] && [ "${#build_tags[@]}" == 0 ]; then
read -p "Build all tags? [y/n]: " -n 1 build_all; echo; echo
fi
if [ "$build_all" != 'y' ]; then
if [ ${#build_tags[@]} -eq 0 ]; then
for t in "${tags[@]}"; do
read -p "Build $t? [y/n]: " -n 1 build_this_tag; echo
if [ "$build_this_tag" == "y" ]; then
build_tags+=("$t")
fi
done
else
for bt in "${build_tags[@]}"; do
if ! [[ "${tags[*]}" == *"$bt"* ]]; then
echo "Error: tag $bt is unknown"
exit 1
fi
done
fi
else
build_tags=("${tags[@]}")
fi
echo
echo -e "Tags to build:\n\t${build_tags[*]}"
echo
if [ -z ${no_cache+x} ]; then
read -rp "Use build cache? [y/n]: " -n 1 use_cache; echo;
if [ "$use_cache" != "y" ]; then
no_cache="--no-cache"
fi
fi
if [ -z ${pull+x} ]; then
read -rp "Attempt base image pull? [y/n]: " -n 1 pull_base; echo;
if [ "$pull_base" == "y" ]; then
pull="--pull"
fi
fi
if [ -z ${push_tags+x} ]; then
read -rp "Push tags to dockerhub? [y/n]: " -n 1 push_tags
fi
# Make sure we use buildkit
export DOCKER_BUILDKIT=1
# Build images
for tag in "${build_tags[@]}"; do
docker build ${pull} ${no_cache} -f "Dockerfile-${tag}" -t "thoteam/molecule_apache_openjdk8:${tag}" .
done
if [ "$push_tags" == 'y' ]; then
# Make sure we are logged in
# !!! DOCKERHUB_USER must be set in the environment
# !!! DOCKERHUB_VAULT_ID must be set in the environment
# !!! vault-keyring-client must be installed
# !!! a password for DOCKERHUB_VAULT_ID must be set in keyring
vault-keyring-client --vault-id ${DOCKERHUB_VAULT_ID} | docker login -u "${DOCKERHUB_USER}" --password-stdin
for tag in "${build_tags[@]}"; do
docker push "thoteam/molecule_apache_openjdk8:${tag}"
done
# Logout from github
docker logout
fi