22f6f54288741d2ed4723e9260234b469fb24591
[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/jobs
122 if [[ ! -z "$tags" ]]; then
123     docker build --build-arg COMMIT=${tags/,*/} -t arvados/jobs .
124 else
125     docker build -t arvados/jobs .
126 fi
127
128 ECODE=$?
129
130 if [[ "$ECODE" != "0" ]]; then
131     EXITCODE=$(($EXITCODE + $ECODE))
132 fi
133
134 checkexit $ECODE "docker build"
135 title "docker build complete (`timer`)"
136
137 title "uploading images"
138
139 timer_reset
140
141 if [[ "$ECODE" != "0" ]]; then
142     title "upload arvados images SKIPPED because build failed"
143 else
144     if [[ $upload == true ]]; then
145         ## 20150526 nico -- *sometimes* dockerhub needs re-login
146         ## even though credentials are already in .dockercfg
147         docker login -u arvados
148
149         docker_push arvados/jobs
150         title "upload arvados images finished (`timer`)"
151     else
152         title "upload arvados images SKIPPED because no --upload option set (`timer`)"
153     fi
154 fi
155
156 exit_cleanly