-
Notifications
You must be signed in to change notification settings - Fork 81
/
build_danm.sh
executable file
·161 lines (140 loc) · 4.38 KB
/
build_danm.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
153
154
155
156
157
158
159
160
161
#!/bin/bash -e
#ERR pseudo-signal is only supported by bash.
#
# Build script to create DANM container images.
#
# This script supports the following environment variables:
#
# - `TAG_PREFIX`. A string that will be prepended to all
# built image tags. This can, for example, be a registry
# name. Note that the string is prepended without any additional
# separators, so if it is eg. a registry name, it MUST end with
# a "/".
#
# - `EXTRA_BUILD_ARGS`. Any additional arguments that you want to
# provide to the container build.
#
# - `USE_CACHE`. If defined, use cache during image build process.
# For compativility with earlier versions of the build process,
# the default is to NOT use the cache.
#
# - `KEEP_BUILDER`. If defined, keep the builder image and tag
# it. This can be useful if the image is to be re-used for
# unit testing, or if a developer wants to run an instance
# off the builder image as a work environment.
#
# - `IMAGE_PUSH`. If defined, push images to registry right
# after building.
#
# error handling with trap taken from https://unix.stackexchange.com/questions/79648/how-to-trigger-error-using-trap-command/157327
unset killer_sig
for sig in SIGHUP SIGINT SIGQUIT SIGTERM; do
trap '
killer_sig="$sig"
exit $((128 + $(kill -l "$sig")))' "$sig"
done
trap '
ret=$?
[ "$ret" -eq 0 ] || echo >&2 "Terminating with error!"
if [ -n "$killer_sig" ]; then
trap - "$killer_sig" # reset traps
kill -s "$killer_sig" "$$"
else
exit "$ret"
fi' EXIT
trap '
ret=$?
error_handler $ret $LINENO $BASH_COMMAND' ERR
error_handler()
{
echo "$(basename $0) error on line : $2 command was: $3"
exit $1
}
#
# Don't use cache unless we're told otherwise.
#
if [ -z "${USE_CACHE}" ]
then
FIRST_BUILD_EXTRA_BUILD_ARGS="--no-cache"
fi
#
# Identify if we need to run docker or buildah
#
if [[ ( "$TRAVIS_PIPELINE" = "buildah" ) || ( "$TRAVIS_PIPELINE" = "" && -x "$(command -v buildah)" ) ]]
then
BUILD_COMMAND="buildah bud"
TAG_COMMAND="buildah tag"
PUSH_COMMAND="buildah push"
elif [[ ( "$TRAVIS_PIPELINE" = "docker" ) || ( "$TRAVIS_PIPELINE" = "" && -x "$(command -v docker)" ) ]]
then
BUILD_COMMAND="docker image build"
TAG_COMMAND="docker image tag"
PUSH_COMMAND="docker image push"
else
echo 'The build process requires docker or buildah/podman installed. Please install any of these and make sure these are executable'
exit 1
fi
#
# Construct a unique version number from the git commit
# hash that is being build. If the workspace isn't
# clean (ie. if "git status" would say anything other than
# "working tree clean"), add a _dirty suffix to that
# version number.
#
LATEST_TAG=$(git describe --tags)
COMMIT_HASH=$(git rev-parse --short=8 HEAD)
if [ -n "$(git status --porcelain)" ]
then
COMMIT_HASH="${COMMIT_HASH}_dirty"
fi
#
# Determine which build stages we want to tag as an image.
#
build_targets=(netwatcher svcwatcher webhook danm-cni-plugins)
if [ -n "${KEEP_BUILDER}" ]
then
build_targets+=(builder)
fi
#
# Build the various images. Each image is represented
# by one target in the multi-stage Dockerfile.
#
for plugin in ${build_targets[@]}
do
echo Building: ${plugin}, version ${COMMIT_HASH}
${BUILD_COMMAND} \
${EXTRA_BUILD_ARGS} \
${FIRST_BUILD_EXTRA_BUILD_ARGS} \
--build-arg LATEST_TAG=${LATEST_TAG} \
--build-arg COMMIT_HASH=${COMMIT_HASH} \
--tag ${TAG_PREFIX}${plugin}:${COMMIT_HASH} \
--target ${plugin} \
--file scm/build/Dockerfile \
.
# Tag image as "latest", too
${TAG_COMMAND} ${TAG_PREFIX}${plugin}:${COMMIT_HASH} ${TAG_PREFIX}${plugin}:latest
# Push to registry if configured to do so
if [ -n "${IMAGE_PUSH}" ]
then
${PUSH_COMMAND} ${TAG_PREFIX}${plugin}:${COMMIT_HASH}
${PUSH_COMMAND} ${TAG_PREFIX}${plugin}:latest
fi
# Make sure we use the cache on the 2nd and subsequent iterations.
unset FIRST_BUILD_EXTRA_BUILD_ARGS
done
#
# Build the installer job image. This is a separate Dockerfile as it has no direct
# overlap with the main binaries.
#
echo Building installer, version ${COMMIT_HASH}
${BUILD_COMMAND} \
${EXTRA_BUILD_ARGS} \
--tag ${TAG_PREFIX}danm-installer:${COMMIT_HASH} \
--file scm/build/Dockerfile.install \
.
${TAG_COMMAND} ${TAG_PREFIX}danm-installer:${COMMIT_HASH} ${TAG_PREFIX}danm-installer:latest
if [ -n "${IMAGE_PUSH}" ]
then
${PUSH_COMMAND} ${TAG_PREFIX}danm-installer:${COMMIT_HASH}
${PUSH_COMMAND} ${TAG_PREFIX}danm-installer:latest
fi