/
build-images
executable file
·89 lines (77 loc) · 2.06 KB
/
build-images
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
#!/bin/bash
unset CDPATH
SCRIPT_NAME=$( basename "$0" )
SCRIPT_HOME=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
CURRENT_DIR=$PWD
# defaults
source $SCRIPT_HOME/.env
BUILD_ARGS="--no-cache"
PUSH='false'
evalrc() {
if [ "$1" -ne "0" ]; then
echo "$2"
cd $CURRENT_DIR
exit $1
fi
}
usage() {
cat <<HELP
Usage: $SCRIPT_NAME [options] [IMAGE]
* if IMAGE is omitted, all images will be built
OPTIONS:
-h Prints this message.
-p Push images to a repository or registry
-d <repo> Specify the destination repo to receive the images; implies -p;
defaults to: "$REGISTRY"
-c Use cached layers when building containers; defaults to false
-v Enable verbose/debug output
HELP
}
while getopts ":pd:cv" opt; do
case $opt in
h ) usage; exit 0;;
p ) PUSH='true';;
d ) PUSH='true'
REGISTRY="${OPTARG}";;
c ) BUILD_ARGS="$(echo $BUILD_ARGS | sed -e s/--no-cache//g)";;
v ) set -x;;
\?)
echo "Invalid option: -$OPTARG" >&2
usage
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
usage
exit 1
;;
esac
done
shift $(($OPTIND - 1))
# Determine image name
if [ "$1" != "" ]; then
IMAGE=$1
fi
get_image_names() {
python -c 'import yaml,sys;y=yaml.safe_load(sys.stdin); print " ".join(y["services"].keys())' < $SCRIPT_HOME/docker-compose-build.yml
}
# tags a docker image with a version
tag_images() {
TAG_IMAGES=${IMAGE:-$(get_image_names)}
CP_VERSION="$( git describe | cut -d- -f 2 )"
for IMG in ${TAG_IMAGES}; do
docker tag $REGISTRY/$IMG:latest $REGISTRY/$IMG:$CP_VERSION
done
}
echo "Building images..."
cd $SCRIPT_HOME
docker-compose -f docker-compose-build.yml build $BUILD_ARGS $IMAGE && tag_images
evalrc $? "Build not successful."
if [ $PUSH = true ]; then
echo "Pushing images..."
# pushes the version tag
for IMG in ${TAG_IMAGES}; do
docker push $REGISTRY/$IMG
evalrc $? "Push not successful."
done
fi