11789: Merge branch 'master' into 11789-arvput-exclude-flag
[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 "  -u, --upload                  Upload the images (docker push)"
13     echo >&2 "  -h, --help                    Display this help and exit"
14     echo >&2
15     echo >&2 "  If no options are given, just builds the images."
16 }
17
18 upload=false
19
20 # NOTE: This requires GNU getopt (part of the util-linux package on Debian-based distros).
21 TEMP=`getopt -o hut: \
22     --long help,upload,tags: \
23     -n "$0" -- "$@"`
24
25 if [ $? != 0 ] ; then echo "Use -h for help"; exit 1 ; fi
26 # Note the quotes around `$TEMP': they are essential!
27 eval set -- "$TEMP"
28
29 while [ $# -ge 1 ]
30 do
31     case $1 in
32         -u | --upload)
33             upload=true
34             shift
35             ;;
36         -t | --tags)
37             case "$2" in
38                 "")
39                   echo "ERROR: --tags needs a parameter";
40                   usage;
41                   exit 1
42                   ;;
43                 *)
44                   tags=$2;
45                   shift 2
46                   ;;
47             esac
48             ;;
49         --)
50             shift
51             break
52             ;;
53         *)
54             usage
55             exit 1
56             ;;
57     esac
58 done
59
60
61 EXITCODE=0
62
63 COLUMNS=80
64
65 title () {
66     printf "\n%*s\n\n" $(((${#title}+$COLUMNS)/2)) "********** $1 **********"
67 }
68
69 docker_push () {
70     if [[ ! -z "$tags" ]]
71     then
72         for tag in $( echo $tags|tr "," " " )
73         do
74              $DOCKER tag $1 $1:$tag
75         done
76     fi
77
78     # Sometimes docker push fails; retry it a few times if necessary.
79     for i in `seq 1 5`; do
80         $DOCKER push $*
81         ECODE=$?
82         if [[ "$ECODE" == "0" ]]; then
83             break
84         fi
85     done
86
87     if [[ "$ECODE" != "0" ]]; then
88         title "!!!!!! docker push $* failed !!!!!!"
89         EXITCODE=$(($EXITCODE + $ECODE))
90     fi
91 }
92
93 timer_reset() {
94     t0=$SECONDS
95 }
96
97 timer() {
98     echo -n "$(($SECONDS - $t0))s"
99 }
100
101 # Sanity check
102 if ! [[ -n "$WORKSPACE" ]]; then
103     echo >&2
104     echo >&2 "Error: WORKSPACE environment variable not set"
105     echo >&2
106     exit 1
107 fi
108
109 echo $WORKSPACE
110
111 # find the docker binary
112 DOCKER=`which docker.io`
113
114 if [[ "$DOCKER" == "" ]]; then
115     DOCKER=`which docker`
116 fi
117
118 if [[ "$DOCKER" == "" ]]; then
119     title "Error: you need to have docker installed. Could not find the docker executable."
120     exit 1
121 fi
122
123 # DOCKER
124 title "Starting docker build"
125
126 timer_reset
127
128 # clean up the docker build environment
129 cd "$WORKSPACE"
130
131 tools/arvbox/bin/arvbox build localdemo
132 ECODE=$?
133
134 if [[ "$ECODE" != "0" ]]; then
135     title "!!!!!! docker BUILD FAILED !!!!!!"
136     EXITCODE=$(($EXITCODE + $ECODE))
137 fi
138
139 tools/arvbox/bin/arvbox build dev
140
141 ECODE=$?
142
143 if [[ "$ECODE" != "0" ]]; then
144     title "!!!!!! docker BUILD FAILED !!!!!!"
145     EXITCODE=$(($EXITCODE + $ECODE))
146 fi
147
148 title "docker build complete (`timer`)"
149
150 title "uploading images"
151
152 timer_reset
153
154 if [[ "$EXITCODE" != "0" ]]; then
155     title "upload arvados images SKIPPED because build failed"
156 else
157     if [[ $upload == true ]]; then
158         ## 20150526 nico -- *sometimes* dockerhub needs re-login
159         ## even though credentials are already in .dockercfg
160         docker login -u arvados
161
162         docker_push arvados/arvbox-dev
163         docker_push arvados/arvbox-demo
164         title "upload arvados images complete (`timer`)"
165     else
166         title "upload arvados images SKIPPED because no --upload option set"
167     fi
168 fi
169
170 exit $EXITCODE