Merge branch 'master' into 7167-keep-rsync-test-setup
[arvados.git] / jenkins / run-build-packages-one-target.sh
1 #!/bin/bash
2
3 read -rd "\000" helpmessage <<EOF
4 $(basename $0): Orchestrate run-build-packages.sh for one target
5
6 Syntax:
7         WORKSPACE=/path/to/arvados $(basename $0) [options]
8
9 --target <target>
10     Distribution to build packages for (default: debian7)
11 --command
12     Build command to execute (default: use built-in Docker image command)
13 --debug
14     Output debug information (default: false)
15
16 WORKSPACE=path         Path to the Arvados source tree to build packages from
17
18 EOF
19
20 if ! [[ -n "$WORKSPACE" ]]; then
21   echo >&2 "$helpmessage"
22   echo >&2
23   echo >&2 "Error: WORKSPACE environment variable not set"
24   echo >&2
25   exit 1
26 fi
27
28 if ! [[ -d "$WORKSPACE" ]]; then
29   echo >&2 "$helpmessage"
30   echo >&2
31   echo >&2 "Error: $WORKSPACE is not a directory"
32   echo >&2
33   exit 1
34 fi
35
36 PARSEDOPTS=$(getopt --name "$0" --longoptions \
37     help,debug,target:,command: \
38     -- "" "$@")
39 if [ $? -ne 0 ]; then
40     exit 1
41 fi
42
43 TARGET=debian7
44 COMMAND=
45 DEBUG=
46
47 eval set -- "$PARSEDOPTS"
48 while [ $# -gt 0 ]; do
49     case "$1" in
50         --help)
51             echo >&2 "$helpmessage"
52             echo >&2
53             exit 1
54             ;;
55         --target)
56             TARGET="$2"; shift
57             ;;
58         --debug)
59             DEBUG=" --debug"
60             ;;
61         --command)
62             COMMAND="$2"; shift
63             ;;
64         --)
65             if [ $# -gt 1 ]; then
66                 echo >&2 "$0: unrecognized argument '$2'. Try: $0 --help"
67                 exit 1
68             fi
69             ;;
70     esac
71     shift
72 done
73
74 set -e
75
76 if [[ "$COMMAND" != "" ]]; then
77   COMMAND="/usr/local/rvm/bin/rvm-exec default bash /jenkins/$COMMAND --target $TARGET$DEBUG"
78 fi
79
80 FINAL_EXITCODE=0
81 JENKINS_DIR=$(dirname "$(readlink -e "$0")")
82
83 run_docker() {
84     local tag=$1; shift
85     if docker run -v "$JENKINS_DIR:/jenkins" -v "$WORKSPACE:/arvados" \
86           --env ARVADOS_DEBUG=1 "arvados/build:$tag" $COMMAND; then
87         # Success - nothing more to do.
88         true
89     else
90         FINAL_EXITCODE=$?
91         echo "ERROR: $tag build failed with exit status $FINAL_EXITCODE." >&2
92     fi
93 }
94
95 # In case it's needed, build the container. This costs just a few
96 # seconds when the container already exist, so it's not a big deal to
97 # do it on each run.
98 cd "$JENKINS_DIR/dockerfiles"
99 echo $TARGET
100 cd $TARGET
101 time docker build -t arvados/build:$TARGET .
102 cd ..
103
104 run_docker $TARGET
105
106 exit $FINAL_EXITCODE