6035: add support for --upload as an option
[arvados-dev.git] / jenkins / run-docker-tests.sh
1 #!/bin/bash
2
3 function usage {
4     echo >&2
5     echo >&2 "usage: $0 [options]"
6     echo >&2
7     echo >&2 "$0 options:"
8     echo >&2 "  -t, --tags [csv_tags]         comma separated tags"
9     echo >&2 "  -u, --upload                  Upload the images (docker push)"
10     echo >&2 "  -h, --help                    Display this help and exit"
11     echo >&2
12     echo >&2 "  If no options are given, just builds the images."
13 }
14
15 upload=false
16
17 # NOTE: This requires GNU getopt (part of the util-linux package on Debian-based distros).
18 TEMP=`getopt -o hut: \
19     --long help,upload,tags: \
20     -n "$0" -- "$@"`
21
22 if [ $? != 0 ] ; then echo "Use -h for help"; exit 1 ; fi
23 # Note the quotes around `$TEMP': they are essential!
24 eval set -- "$TEMP"
25
26 while [ $# -ge 1 ]
27 do
28     case $1 in
29         -u | --upload)
30             upload=true
31             shift
32             ;;
33         -t | --tags)
34             case "$2" in
35                 "")
36                   echo "ERROR: --tags needs a parameter";
37                   usage;
38                   exit 1
39                   ;;
40                 *)
41                   tags=$2;
42                   shift 2
43                   ;;
44             esac
45             ;;
46         --)
47             shift
48             break
49             ;;
50         *)
51             usage
52             exit 1
53             ;;
54     esac
55 done
56
57
58 EXITCODE=0
59
60 COLUMNS=80
61
62 title () {
63     printf "\n%*s\n\n" $(((${#title}+$COLUMNS)/2)) "********** $1 **********"
64 }
65
66 docker_push () {
67     echo $tags
68     if [[ ! -z "$tags" ]]
69     then
70         for tag in $( echo $tags|tr "," " " )
71         do
72              $DOCKER tag $1 $1:$tag
73         done
74     fi
75
76     # Sometimes docker push fails; retry it a few times if necessary.
77     for i in `seq 1 5`; do
78         $DOCKER push $*
79         ECODE=$?
80         if [[ "$ECODE" == "0" ]]; then
81             break
82         fi
83     done
84
85     if [[ "$ECODE" != "0" ]]; then
86         title "!!!!!! docker push $* failed !!!!!!"
87         EXITCODE=$(($EXITCODE + $ECODE))
88     fi
89 }
90
91 timer_reset() {
92     t0=$SECONDS
93 }
94
95 timer() {
96     echo -n "$(($SECONDS - $t0))s"
97 }
98
99 # Sanity check
100 if ! [[ -n "$WORKSPACE" ]]; then
101     echo >&2
102     echo >&2 "Error: WORKSPACE environment variable not set"
103     echo >&2
104     exit 1
105 fi
106
107 echo $WORKSPACE
108
109 # find the docker binary
110 DOCKER=`which docker.io`
111
112 if [[ "$DOCKER" == "" ]]; then
113     DOCKER=`which docker`
114 fi
115
116 if [[ "$DOCKER" == "" ]]; then
117     title "Error: you need to have docker installed. Could not find the docker executable."
118     exit 1
119 fi
120
121 # DOCKER
122 title "Starting docker build"
123
124 timer_reset
125
126 # clean up the docker build environment
127 cd "$WORKSPACE"
128 cd docker
129 ./build.sh realclean
130
131 rm -f config.yml
132
133 # Get test config.yml file
134 cp $HOME/docker/config.yml .
135
136 ./build.sh
137
138 ECODE=$?
139
140 if [[ "$ECODE" != "0" ]]; then
141     title "!!!!!! docker BUILD FAILED !!!!!!"
142     EXITCODE=$(($EXITCODE + $ECODE))
143 fi
144
145 title "docker build complete (`timer`)"
146
147 title "uploading images"
148
149 timer_reset
150
151 if [[ "$ECODE" != "0" ]]; then
152     title "upload arvados images SKIPPED because build failed"
153 else
154     if [[ $upload == true ]]; then 
155         docker_push arvados/api
156         docker_push arvados/compute
157         docker_push arvados/doc
158         docker_push arvados/workbench
159         docker_push arvados/keep
160         docker_push arvados/keepproxy
161         docker_push arvados/shell
162         docker_push arvados/sso
163         title "upload arvados images complete (`timer`)"
164     else
165         title "upload arvados images SKIPPED because no --upload option set"
166     fi
167 fi
168
169 title "Starting docker java-bwa-samtools build"
170
171 timer_reset
172
173 ./build.sh java-bwa-samtools-image
174
175 ECODE=$?
176
177 if [[ "$ECODE" != "0" ]]; then
178     title "!!!!!! docker java-bwa-samtools BUILD FAILED !!!!!!"
179     EXITCODE=$(($EXITCODE + $ECODE))
180 fi
181
182 title "docker build java-bwa-samtools complete (`timer`)"
183
184 timer_reset
185
186 if [[ "$ECODE" != "0" ]]; then
187     title "upload arvados/jobs image SKIPPED because build failed"
188 else
189     if [[ $upload == true ]]; then 
190         title "upload arvados/jobs image"
191         docker_push arvados/jobs
192         title "upload arvados/jobs image complete (`timer`)"
193     else
194         title "upload arvados images SKIPPED because no --upload option set"
195     fi
196 fi
197
198 exit $EXITCODE