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