forked from kyma-project/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.sh
executable file
·115 lines (97 loc) · 2.52 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env bash
# docker image name.
PACKAGE_NAME=http-db-service
# go binary name.
BINARY_ARTIFACT_EXAMPLE=main
set -e
set -u
set -o pipefail
# Reset in case getopts.
OPTIND=1
VERBOSE=true
HELP=false
APP_VERSION=latest
declare IMAGE_NAME_BROKER
declare DOCKER_SYSTEM_NAME
declare IMAGE_NAME_EXAMPLE=${PACKAGE_NAME}:${APP_VERSION}
RED='\033[0;31m'
GREEN='\033[0;32m'
INVERTED='\033[7m'
NC='\033[0m' # No Color
function usage() {
echo "Build your example/http-db-service docker image."
echo "Usage:"
echo " $0 [-h] [-q] [-t]"
echo
echo "Flags:"
echo " -h help, print usage"
echo " -q quiet, to turn off VERBOSE"
echo " -t tag, tag the image e.g:registry-name/image-name:tag-version, default:http-db-service:latest"
echo "Example:"
echo " ./build.sh"
echo " ./build.sh -t registry:5000/example/http-db-service:0.0.1"
echo " ./build.sh -h"
echo " ./build.sh -q"
}
function debug() {
echo "Config:"
echo "Flags:"
echo "- verbose : ${VERBOSE}"
echo "result:"
echo "- EXAMPLE IMAGE_NAME : ${IMAGE_NAME_EXAMPLE}"
echo "- DOCKER_SYSTEM_NAME : ${DOCKER_SYSTEM_NAME}"
}
function setParameter() {
if [[ $# -ne 1 ]]; then
echo "No parameters are passed to getParameter"
exit 1
else
IMAGE_NAME_EXAMPLE=$1
fi
}
function onExit {
local EXIT_CODE=$?
if [[ ${VERBOSE} == "true" ]] && [[ ${HELP} == "false" ]]; then
echo -e "${GREEN}Debug on exit${NC}"
debug
echo "exit code: ${EXIT_CODE}"
fi
}
trap onExit EXIT
OPTIND=1
while getopts "ht:q" opt
do
case ${opt} in
h)
HELP=true
usage
exit 0
;;
q)
set +e
VERBOSE=false
set -e
;;
t)
set +e
setParameter "$OPTARG"
set -e
;;
\?)
echo "Unknown option: -$OPTARG" >&2;
exit 1
;;
esac
done
shift $((OPTIND-1))
## inspect configured docker env
DOCKER_SYSTEM_NAME=$(docker info -f "{{ .Name }}")
## perform image build
if [[ ${VERBOSE} == "true" ]]; then echo -e "${GREEN}Build Example binary locally${NC}"; fi
if [[ ${VERBOSE} == "true" ]]; then
echo -e "${GREEN}Build docker image ${IMAGE_NAME_EXAMPLE} ${NC}"
docker build -t ${IMAGE_NAME_EXAMPLE} -f Dockerfile .
echo -e "${GREEN}Built Docker image successfully...${NC}"
else
docker build -t ${IMAGE_NAME_EXAMPLE} -f Dockerfile . > /dev/null
fi