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