-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjenkins.sh
executable file
·335 lines (281 loc) · 8.75 KB
/
jenkins.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#!/bin/bash
#set -x
set -o nounset
set -o errexit
set -o pipefail
# without errtrace functions don't inherit the ERR trap
set -o errtrace
# default libs version
export DOCKER_LIBS_REPO="${DOCKER_LIBS_REPO:-https://github.com/deephealthproject/docker-libs.git}"
export DOCKER_LIBS_BRANCH=${DOCKER_LIBS_BRANCH:-develop}
export DOCKER_LIBS_VERSION=""
# set Docker repository
export DOCKER_REPOSITORY_OWNER="${DOCKER_REPOSITORY_OWNER:-dhealth}"
# env requirements
DOCKER_USER=${DOCKER_USER:-}
DOCKER_PASSWORD=${DOCKER_PASSWORD:-}
GIT_URL=${GIT_URL:-}
# set script version
VERSION=0.3.0
function abspath() {
local path="${*}"
if [[ -d "${path}" ]]; then
echo "$( cd "${path}" >/dev/null && pwd )"
else
echo "$( cd "$( dirname "${path}" )" >/dev/null && pwd )/$(basename "${path}")"
fi
}
function log() {
echo -e "${@}" >&2
}
function debug_log() {
if [[ -n "${DEBUG:-}" ]]; then
echo -e "DEBUG: ${@}" >&2
fi
}
function error_log() {
echo -e "ERROR: ${@}" >&2
}
function error_trap() {
error_log "Error at line ${BASH_LINENO[1]} running the following command:\n\n\t${BASH_COMMAND}\n\n"
error_log "Stack trace:"
for (( i=1; i < ${#BASH_SOURCE[@]}; ++i)); do
error_log "$(printf "%$((4*$i))s %s:%s\n" " " "${BASH_SOURCE[$i]}" "${BASH_LINENO[$i]}")"
done
exit 2
}
trap error_trap ERR
function usage_error() {
if [[ $# > 0 ]]; then
echo -e "ERROR: ${@}" >&2
fi
help
exit 2
}
function print_version() {
echo ${VERSION}
}
function help() {
local script_name=$(basename "$0")
echo -e "\nUsage of '${script_name}'
${script_name} [options]
${script_name} -h prints this help message
${script_name} -v prints the '${script_name}' version
OPTIONS:
--target [CPU|GPU|CUDNN] Set the build target (CPU, GPU or CUDNN; default= GPU)
--clean-sources Remove library sources
--clean-images Remove local Docker images
--disable-cache Disable Docker cache
--disable-build Disable build of Docker images
--disable-pull Disable pull of existing Docker images
--disable-push Disable push of Docker images
--disable-tests Disable tests of Docker images
--disable-docker-login Disable login on Docker registry
--debug Enable debug logs
ENVIRONMENT requirements:
* DOCKER_USER
* DOCKER_PASSWORD
ENVIRONMENT defaults:
* DOCKER_LIBS_REPO => https://github.com/deephealthproject/docker-libs.git
* DOCKER_LIBS_BRANCH => develop
* DOCKER_REPOSITORY_OWNER => dhealth
" >&2
}
# sw version
LIBS_TAG=$(git tag -l --points-at HEAD | tail -n 1)
LIBS_REVISION=$(git rev-parse --short HEAD | sed -E 's/-//; s/ .*//')
LIBS_BRANCH=$(git rev-parse --abbrev-ref HEAD | sed -E 's+(remotes/|origin/)++g; s+/+-+g; s/ .*//')
LIBS_VERSION=$(if [[ -n "${LIBS_TAG}" ]]; then echo ${LIBS_TAG}; else echo ${LIBS_BRANCH}-${LIBS_REVISION}; fi)
# various settings
CLEAN_IMAGES=0
CLEAN_SOURCES=0
DISABLE_CACHE=0
DISABLE_PULL=0
DISABLE_BUILD=0
DISABLE_PUSH=0
DISABLE_TESTS=0
DISABLE_DOCKER_LOGIN=0
BUILD_TARGET=GPU
# get docker-libs repository
function clone_docker_libs() {
if [[ ! -d "docker-libs" ]]; then
log "Cloning docker-libs (branch=${DOCKER_LIBS_BRANCH}, rev=${DOCKER_LIBS_VERSION})..."
if [[ -n "${DOCKER_LIBS_BRANCH}" ]]; then
branch="-b ${DOCKER_LIBS_BRANCH}"
fi
git clone ${branch} ${DOCKER_LIBS_REPO}
fi
if [[ -n "${DOCKER_LIBS_VERSION}" ]]; then
log "Cloning docker-libs (rev.${DOCKER_LIBS_VERSION})..."
cd docker-libs && git fetch && git reset --hard ${DOCKER_LIBS_VERSION}
fi
log "DONE"
}
# Docker login
function docker_login() {
echo ${DOCKER_PASSWORD} | docker login --username ${DOCKER_USER} --password-stdin
export DOCKER_LOGIN_DONE="true"
}
function run() {
local REPOSITORY=""
local LIB_NAME=""
# set repository
if [[ -n "${GIT_URL}" ]]; then \
REPOSITORY=$(echo "${GIT_URL}" | sed -E 's+(.*)/([^/]*)\.git+\2+') ; \
else
REPOSITORY=$(basename $(git rev-parse --show-toplevel)) ;
fi
# set library name
LIB_NAME=$(echo "${REPOSITORY}" | tr a-z A-Z | sed 's+DOCKER-LIBS+LIBS+; s+-+_+')
# set git tag & branch & image prefix
# and define whether to push lates tag
GIT_BRANCH=${GIT_BRANCH:-$(git rev-parse --abbrev-ref HEAD | sed -E 's+(remotes/|origin/|tags/)++g; s+/+-+g; s/ .*//')}
BRANCH_NAME=$(echo "${GIT_BRANCH}" | sed 's+origin/++g; s+refs/tags/++g')
NORMALIZED_BRANCH_NAME=$(echo "${BRANCH_NAME}" | sed 's+/+-+g; s+[[:space:]]++g')
TAG=$(git tag -l --points-at HEAD | tail -n 1 | sed 's+[[:space:]]++g')
REVISION="$(git rev-parse --short HEAD --short | sed 's+[[:space:]]++g')"
if [ -n "${TAG}" ]; then
DOCKER_IMAGE_PREFIX="${TAG}"
# update latest if a tag exists and the branch is master
if [ "${BRANCH_NAME}" == "master" ]; then
export DOCKER_IMAGE_LATEST=true
fi
else
DOCKER_IMAGE_PREFIX="${REVISION}"
fi
# define Docker image tags
export BUILD_NUMBER=${BUILD_NUMBER:-$(date '+%Y%m%d%H%M%S')}
#export DOCKER_IMAGE_TAG_EXTRA="${DOCKER_IMAGE_PREFIX}_build${BUILD_NUMBER}"
#export DOCKER_IMAGE_TAG_EXTRA="${DOCKER_IMAGE_TAG_EXTRA} ${NORMALIZED_BRANCH_NAME}_build${BUILD_NUMBER}"
# set branch and revision
export ${LIB_NAME}_BRANCH=${BRANCH_NAME}
export ${LIB_NAME}_REVISION=${REVISION}
# log environment
printenv
# detect repository
lib_suffix=""
if [[ "${LIB_NAME}" != "LIBS" ]]; then
lib_suffix="_${REPOSITORY}"
export CONFIG_FILE=""
clone_docker_libs
cd docker-libs
fi
# cleanup sources
if [[ "${CLEAN_SOURCES}" == "true" ]]; then
make clean_sources
fi
# cleanup images
if [[ "${CLEAN_IMAGES}" == "true" ]]; then
make clean_images
fi
# build images
if [[ ${DISABLE_BUILD} == 0 ]]; then
log "Docker images before..."
docker images
make build${lib_suffix} ;
log "Docker images after..."
docker images
fi
# make tests
if [[ ${DISABLE_TESTS} == 0 ]]; then
if [[ -n ${lib_suffix} ]]; then
make test${lib_suffix} ;
make test${lib_suffix}_toolkit ;
else
make test ;
fi
fi
# push images
if [[ ${DISABLE_PUSH} == 0 ]]; then
# login to DockerHub
if [[ ${DISABLE_DOCKER_LOGIN} == 0 ]]; then
if [[ -z ${DOCKER_USER} || -z ${DOCKER_PASSWORD} ]]; then
usage_error "You need to set DOCKER_USER and DOCKER_PASSWORD on your environment"
fi
docker_login
export DOCKER_LOGIN_DONE="true"
fi
if [[ -n ${lib_suffix} ]]; then
make push${lib_suffix} ;
make push${lib_suffix}_toolkit ;
else
make push ;
fi
fi
# print images
if [[ ${DISABLE_BUILD} == 0 ]]; then
log "\n\nList of Docker images..."
log "*************************************************************************************************************"
make images_list
# print libraries
log "\n\nList of Libraries..."
log "*************************************************************************************************************"
make libraries_list
fi
}
# parse arguments
POSITIONAL=()
while [[ $# -gt 0 ]]
do
opt="${1}"
value="${2:-}"
case $opt in
-h) help; exit 0 ;;
-v) print_version; exit 0 ;;
--debug)
DEBUG=true
shift ;;
--clean-sources)
CLEAN_SOURCES=true
shift ;;
--clean-images)
CLEAN_IMAGES=true
shift ;;
--disable-cache)
DISABLE_CACHE=1
shift ;;
--disable-build)
DISABLE_BUILD=1
shift ;;
--disable-pull)
DISABLE_PULL=1
shift ;;
--disable-push)
DISABLE_PUSH=1
shift ;;
--disable-tests)
DISABLE_TESTS=1
shift ;;
--disable-docker-login)
DISABLE_DOCKER_LOGIN=1
shift ;;
--target)
if [[ "CPU GPU CUDNN" =~ (^|[[:space:]])${2}($|[[:space:]]) ]]; then
BUILD_TARGET=$2 ;
else
usage_error "The target '$2' is not a valid target"
fi
shift 2 ;;
*) # unknown option
POSITIONAL+=("$opt") # save it in an array for later
shift ;;
esac
done
set -- "${POSITIONAL[@]}" # restore positional parameters
# get the absolute path of CONFIG_DIR
CONFIG_DIR=$(abspath $(pwd))
debug_log "docker-libs branch: ${DOCKER_LIBS_BRANCH}"
debug_log "docker-libs revision: ${DOCKER_LIBS_VERSION}"
debug_log "disable cache: ${DISABLE_CACHE}"
debug_log "disable pull: ${DISABLE_PULL}"
debug_log "build target: ${BUILD_TARGET}"
#
export DOCKER_LIBS_BRANCH
export DOCKER_LIBS_VERSION
export CLEAN_IMAGES
export CLEAN_SOURCES
export DISABLE_CACHE
export DISABLE_PULL
export BUILD_TARGET
# exec
run