refs #12850 Merge branch '12850-docker-tag-version'
[arvados.git] / build / run-build-docker-jobs-image.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                    version tag for docker"
12     echo >&2 "  -u, --upload                  Upload the images (docker push)"
13     echo >&2 "  --no-cache                    Don't use build cache"
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 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,no-cache,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         --no-cache)
37             NOCACHE=--no-cache
38             shift
39             ;;
40         -t | --tags)
41             case "$2" in
42                 "")
43                   echo "ERROR: --tags needs a parameter";
44                   usage;
45                   exit 1
46                   ;;
47                 *)
48                   version_tag="$2";
49                   shift 2
50                   ;;
51             esac
52             ;;
53         --)
54             shift
55             break
56             ;;
57         *)
58             usage
59             exit 1
60             ;;
61     esac
62 done
63
64 EXITCODE=0
65
66 exit_cleanly() {
67     trap - INT
68     report_outcomes
69     exit $EXITCODE
70 }
71
72 COLUMNS=80
73 . $WORKSPACE/build/run-library.sh
74
75 docker_push () {
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         EXITCODE=$(($EXITCODE + $ECODE))
87     fi
88     checkexit $ECODE "docker push $*"
89 }
90
91 # Sanity check
92 if ! [[ -n "$WORKSPACE" ]]; then
93     echo >&2
94     echo >&2 "Error: WORKSPACE environment variable not set"
95     echo >&2
96     exit 1
97 fi
98
99 echo $WORKSPACE
100
101 # find the docker binary
102 DOCKER=`which docker.io`
103
104 if [[ "$DOCKER" == "" ]]; then
105     DOCKER=`which docker`
106 fi
107
108 if [[ "$DOCKER" == "" ]]; then
109     title "Error: you need to have docker installed. Could not find the docker executable."
110     exit 1
111 fi
112
113 # DOCKER
114 title "Starting docker build"
115
116 timer_reset
117
118 # clean up the docker build environment
119 cd "$WORKSPACE"
120
121 python_sdk_ts=$(cd sdk/python && timestamp_from_git)
122 cwl_runner_ts=$(cd sdk/cwl && timestamp_from_git)
123
124 python_sdk_version=$(cd sdk/python && nohash_version_from_git 0.1)
125 cwl_runner_version=$(cd sdk/cwl && nohash_version_from_git 1.0)
126
127 if [[ $python_sdk_ts -gt $cwl_runner_ts ]]; then
128     cwl_runner_version=$(cd sdk/python && nohash_version_from_git 1.0)
129 fi
130
131 echo cwl_runner_version $cwl_runner_version python_sdk_version $python_sdk_version
132
133 cd docker/jobs
134 docker build $NOCACHE \
135        --build-arg python_sdk_version=${python_sdk_version}-2 \
136        --build-arg cwl_runner_version=${cwl_runner_version}-3 \
137        -t arvados/jobs:$cwl_runner_version .
138
139 ECODE=$?
140
141 if [[ "$ECODE" != "0" ]]; then
142     EXITCODE=$(($EXITCODE + $ECODE))
143 fi
144
145 checkexit $ECODE "docker build"
146 title "docker build complete (`timer`)"
147
148 if [[ "$ECODE" != "0" ]]; then
149   exit_cleanly
150 fi
151
152 timer_reset
153
154 if docker --version |grep " 1\.[0-9]\." ; then
155     # Docker version prior 1.10 require -f flag
156     # -f flag removed in Docker 1.12
157     FORCE=-f
158 fi
159 if ! [[ -z "$version_tag" ]]; then
160     docker tag $FORCE arvados/jobs:$cwl_runner_version arvados/jobs:"$version_tag"
161 else
162     docker tag $FORCE arvados/jobs:$cwl_runner_version arvados/jobs:latest
163 fi
164
165 ECODE=$?
166
167 if [[ "$ECODE" != "0" ]]; then
168     EXITCODE=$(($EXITCODE + $ECODE))
169 fi
170
171 checkexit $ECODE "docker tag"
172 title "docker tag complete (`timer`)"
173
174 title "uploading images"
175
176 timer_reset
177
178 if [[ "$ECODE" != "0" ]]; then
179     title "upload arvados images SKIPPED because build or tag failed"
180 else
181     if [[ $upload == true ]]; then
182         ## 20150526 nico -- *sometimes* dockerhub needs re-login
183         ## even though credentials are already in .dockercfg
184         docker login -u arvados
185         if ! [[ -z "$version_tag" ]]; then
186             docker_push arvados/jobs:"$version_tag"
187         else
188            docker_push arvados/jobs:$cwl_runner_version
189            docker_push arvados/jobs:latest
190         fi
191         title "upload arvados images finished (`timer`)"
192     else
193         title "upload arvados images SKIPPED because no --upload option set (`timer`)"
194     fi
195 fi
196
197 exit_cleanly