Fix inverted test for pypi/gem upload logic. Make upload more verbose.
[arvados.git] / build / run-build-docker-jobs-image.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 EXITCODE=0
58
59 exit_cleanly() {
60     trap - INT
61     report_outcomes
62     exit $EXITCODE
63 }
64
65 COLUMNS=80
66 . $WORKSPACE/build/run-library.sh
67
68 docker_push () {
69     if [[ ! -z "$tags" ]]
70     then
71         for tag in $( echo $tags|tr "," " " )
72         do
73              $DOCKER tag $1 $1:$tag
74         done
75     fi
76
77     # Sometimes docker push fails; retry it a few times if necessary.
78     for i in `seq 1 5`; do
79         $DOCKER push $*
80         ECODE=$?
81         if [[ "$ECODE" == "0" ]]; then
82             break
83         fi
84     done
85
86     if [[ "$ECODE" != "0" ]]; then
87         EXITCODE=$(($EXITCODE + $ECODE))
88     fi
89     checkexit $ECODE "docker push $*"
90 }
91
92 # Sanity check
93 if ! [[ -n "$WORKSPACE" ]]; then
94     echo >&2
95     echo >&2 "Error: WORKSPACE environment variable not set"
96     echo >&2
97     exit 1
98 fi
99
100 echo $WORKSPACE
101
102 # find the docker binary
103 DOCKER=`which docker.io`
104
105 if [[ "$DOCKER" == "" ]]; then
106     DOCKER=`which docker`
107 fi
108
109 if [[ "$DOCKER" == "" ]]; then
110     title "Error: you need to have docker installed. Could not find the docker executable."
111     exit 1
112 fi
113
114 # DOCKER
115 title "Starting docker build"
116
117 timer_reset
118
119 # clean up the docker build environment
120 cd "$WORKSPACE"
121 cd docker
122 rm -f jobs-image
123 rm -f config.yml
124
125 # Get test config.yml file
126 cp $HOME/docker/config.yml .
127
128 if [[ ! -z "$tags" ]]; then
129   COMMIT=${tags/,*/} ./build.sh jobs-image
130 else
131   ./build.sh jobs-image
132 fi
133
134 ECODE=$?
135
136 if [[ "$ECODE" != "0" ]]; then
137     EXITCODE=$(($EXITCODE + $ECODE))
138 fi
139
140 checkexit $ECODE "docker build"
141 title "docker build complete (`timer`)"
142
143 title "uploading images"
144
145 timer_reset
146
147 if [[ "$ECODE" != "0" ]]; then
148     title "upload arvados images SKIPPED because build failed"
149 else
150     if [[ $upload == true ]]; then 
151         ## 20150526 nico -- *sometimes* dockerhub needs re-login 
152         ## even though credentials are already in .dockercfg
153         docker login -u arvados
154
155         docker_push arvados/jobs
156         title "upload arvados images finished (`timer`)"
157     else
158         title "upload arvados images SKIPPED because no --upload option set (`timer`)"
159     fi
160 fi
161
162 exit_cleanly