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