forked from spiffxp/kraken-services
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sh
executable file
·92 lines (76 loc) · 1.79 KB
/
build.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
#!/bin/bash
set -e
set -u
# env-injectable defaults
DOCKER_REPO=${DOCKER_REPO:-"quay.io/samsung_cnct"}
DOCKER_TAG=${DOCKER_TAG:-"latest"}
DOCKER_PUSH=${DOCKER_PUSH:-"false"}
IGNORE_PATHS=${IGNORE_PATHS:-"helm-docker charts"}
# utilities
doit=""
print_usage_and_die() {
echo "usage: $0 [--repo (default: quay.io/samsung_cnct)] [--tag (default: latest)] [--ignore (default: 'helm-docker charts')] [--push] dir"
exit 1
}
# parse args
while [[ $# > 1 ]]; do
key="$1"
shift # past key
case $key in
-r|--repo)
DOCKER_REPO="$1"
shift # past value
;;
-t|--tag)
DOCKER_TAG="$1"
shift # past value
;;
-p|--push)
DOCKER_PUSH="true"
;;
-n|--dryrun)
doit="echo"
;;
-i|--ignore)
IGNORE_PATHS="$1"
;;
-h|*)
print_usage_and_die
;;
esac
done
if [[ $# < 1 ]] || [[ "$1" =~ ^- ]] || ! [[ -d "$1" ]]; then
print_usage_and_die
fi
dockerfiles_dir=$1
# build ignore dirs string
ignore_paths=""
for folder in ${IGNORE_PATHS}; do
ignore_paths="${ignore_paths}-not -path \"*/${folder}/*\" "
done
# build command string
command_string="find $dockerfiles_dir -type f -name Dockerfile $ignore_paths| sed -e 's|^./||'"
# locate dockerfiles
dockerfiles=$(eval $command_string)
# pre-pull FROM images
for image in $(cat $dockerfiles | grep "^FROM" | sed -e 's/^FROM //' | grep -v '^scratch$' | sort | uniq); do
${doit} docker pull $image
done
# build images
for x in $dockerfiles; do
dir=$(dirname $x)
cd $dir
repo=${DOCKER_REPO}
name=$(basename $dir)
tag=${DOCKER_TAG}
image="${repo}/${name}:${tag}"
echo
echo "Building ${image}..."
${doit} docker build -t ${image} .
if [ "${DOCKER_PUSH}" == "true" ]; then
echo
echo "Pushing ${image}..."
${doit} docker push ${image}
fi
cd -
done