Merge branch 'main' into 22141-picking-dialog-search
[arvados.git] / build / run-build-test-packages-one-target.sh
1 #!/bin/bash
2 # Copyright (C) The Arvados Authors. All rights reserved.
3 #
4 # SPDX-License-Identifier: AGPL-3.0
5
6 read -rd "\000" helpmessage <<EOF
7 $(basename $0): Build, test and (optionally) upload packages for one target
8
9 Syntax:
10         WORKSPACE=/path/to/arvados $(basename $0) --target <target> [options]
11
12 --target <target>
13     Distribution to build packages for
14 --only-build <package>
15     Build only a specific package (or ONLY_BUILD from environment)
16 --arch <arch>
17     Build a specific architecture (or ARCH from environment, defaults to native architecture)
18 --force-build
19     Build even if the package exists upstream or if it has already been
20     built locally
21 --force-test
22     Test even if there is no new untested package
23 --upload
24     If the build and test steps are successful, upload the packages
25     to a remote apt repository (default: false)
26 --debug
27     Output debug information (default: false)
28 --rc
29     Optional Parameter to build Release Candidate
30 --build-version <version>
31     Version to build (default:
32     \$ARVADOS_BUILDING_VERSION-\$ARVADOS_BUILDING_ITERATION or
33     0.1.timestamp.commithash)
34 --skip-docker-build
35     Don't try to build Docker images
36
37 WORKSPACE=path         Path to the Arvados source tree to build packages from
38
39 EOF
40
41 if ! [[ -n "$WORKSPACE" ]]; then
42   echo >&2 "$helpmessage"
43   echo >&2
44   echo >&2 "Error: WORKSPACE environment variable not set"
45   echo >&2
46   exit 1
47 fi
48
49 if ! [[ -d "$WORKSPACE" ]]; then
50   echo >&2 "$helpmessage"
51   echo >&2
52   echo >&2 "Error: $WORKSPACE is not a directory"
53   echo >&2
54   exit 1
55 fi
56
57 PARSEDOPTS=$(getopt --name "$0" --longoptions \
58     help,debug,upload,rc,target:,force-test,only-build:,force-build,arch:,build-version:,skip-docker-build \
59     -- "" "$@")
60 if [ $? -ne 0 ]; then
61     exit 1
62 fi
63
64 UPLOAD=0
65 RC=0
66 DEBUG=
67 TARGET=
68
69 declare -a build_args=()
70
71 eval set -- "$PARSEDOPTS"
72 while [ $# -gt 0 ]; do
73     case "$1" in
74         --help)
75             echo >&2 "$helpmessage"
76             echo >&2
77             exit 1
78             ;;
79         --target)
80             TARGET="$2"; shift
81             ;;
82         --force-test)
83             FORCE_TEST=1
84             ;;
85         --force-build)
86             FORCE_BUILD=1
87             ;;
88         --only-build)
89             ONLY_BUILD="$2"; shift
90             ;;
91         --arch)
92             ARCH="$2"; shift
93             ;;
94         --debug)
95             DEBUG=" --debug"
96             ;;
97         --upload)
98             UPLOAD=1
99             ;;
100         --rc)
101             RC=1
102             ;;
103         --build-version)
104             build_args+=("$1" "$2")
105             shift
106             ;;
107         --skip-docker-build)
108             SKIP_DOCKER_BUILD=1
109             ;;
110         --)
111             if [ $# -gt 1 ]; then
112                 echo >&2 "$0: unrecognized argument '$2'. Try: $0 --help"
113                 exit 1
114             fi
115             ;;
116     esac
117     shift
118 done
119
120 if [[ -z "$TARGET" ]]; then
121     echo "FATAL: --target must be specified" >&2
122     exit 2
123 elif [[ ! -d "$WORKSPACE/build/package-build-dockerfiles/$TARGET" ]]; then
124     echo "FATAL: unknown build target '$TARGET'" >&2
125     exit 2
126 fi
127
128 build_args+=(--target "$TARGET")
129
130 if [[ -n "$ONLY_BUILD" ]]; then
131   build_args+=(--only-build "$ONLY_BUILD")
132 fi
133
134 if [[ -n "$FORCE_BUILD" ]]; then
135   build_args+=(--force-build)
136 fi
137
138 if [[ -n "$FORCE_TEST" ]]; then
139   build_args+=(--force-test)
140 fi
141
142 if [[ "$SKIP_DOCKER_BUILD" = 1 ]]; then
143   build_args+=(--skip-docker-build)
144 fi
145
146 if [[ -n "$ARCH" ]]; then
147   build_args+=(--arch "$ARCH")
148 fi
149
150 exit_cleanly() {
151     trap - INT
152     report_outcomes
153     exit ${#failures}
154 }
155
156 COLUMNS=80
157 . $WORKSPACE/build/run-library.sh
158
159 title "Start build packages"
160 timer_reset
161
162 $WORKSPACE/build/run-build-packages-one-target.sh "${build_args[@]}"$DEBUG
163
164 checkexit $? "build packages"
165 title "End of build packages (`timer`)"
166
167 title "Start test packages"
168 timer_reset
169
170 if [ ${#failures[@]} -eq 0 ]; then
171   $WORKSPACE/build/run-build-packages-one-target.sh "${build_args[@]}" --test-packages$DEBUG
172 else
173   echo "Skipping package upload, there were errors building the packages"
174 fi
175
176 checkexit $? "test packages"
177 title "End of test packages (`timer`)"
178
179 if [[ "$UPLOAD" != 0 ]]; then
180   title "Start upload packages"
181   timer_reset
182
183   if [ ${#failures[@]} -eq 0 ]; then
184     if [[ "$RC" != 0 ]]; then
185       echo "/usr/local/arvados-dev/jenkins/run_upload_packages.py --repo testing -H jenkinsapt@apt.arvados.org -o Port=2222 --workspace $WORKSPACE $TARGET"
186       /usr/local/arvados-dev/jenkins/run_upload_packages.py --repo testing -H jenkinsapt@apt.arvados.org -o Port=2222 --workspace $WORKSPACE $TARGET
187     else
188       echo "/usr/local/arvados-dev/jenkins/run_upload_packages.py --repo dev -H jenkinsapt@apt.arvados.org -o Port=2222 --workspace $WORKSPACE $TARGET"
189       /usr/local/arvados-dev/jenkins/run_upload_packages.py --repo dev -H jenkinsapt@apt.arvados.org -o Port=2222 --workspace $WORKSPACE $TARGET
190     fi
191   else
192     echo "Skipping package upload, there were errors building and/or testing the packages"
193   fi
194   checkexit $? "upload packages"
195   title "End of upload packages (`timer`)"
196 fi
197
198 exit_cleanly