blob: a8612f1bd6548b86e469247e0e7697704d7c8237 [file] [log] [blame]
Leopold Schabeldca59d92021-04-01 19:02:53 +02001#!/usr/bin/env bash
Leopold Schabel383d4bb2019-11-14 22:53:58 +01002# This helper scripts executes all Bazel tests in our CI environment.
3# https://phab.monogon.dev/harbormaster/plan/2/
Leopold Schabel052af2d2019-11-06 02:21:53 +00004set -euo pipefail
5
Leopold Schabel822341a2020-02-03 21:51:47 +01006DOCKERFILE_HASH=$(sha1sum build/Dockerfile | cut -c -8)
7
Leopold Schabel052af2d2019-11-06 02:21:53 +00008BUILD_ID=$1;
9BUILD_PHID=$2;
10shift; shift;
11
Serge Bazanskifebf0b02021-01-07 16:34:28 +010012TAG=monogon-version-${DOCKERFILE_HASH}
Serge Bazanskic3ad8462021-01-08 16:45:51 +010013CONTAINER=monogon-build-${BUILD_ID}
Leopold Schabel052af2d2019-11-06 02:21:53 +000014
Serge Bazanskic3ad8462021-01-08 16:45:51 +010015# We keep one set of Bazel build caches per working copy to avoid concurrency
16# issues (we cannot run multiple Bazel servers on a given _bazel_root).
Leopold Schabel16267052019-11-06 14:43:21 +000017function getWorkingCopyID {
18 local pattern='/var/drydock/workingcopy-([0-9]+)/'
19 [[ "$(pwd)" =~ $pattern ]]
20 echo ${BASH_REMATCH[1]}
21}
22
Serge Bazanskic3ad8462021-01-08 16:45:51 +010023# Main Bazel cache, used as Bazel outputRoot/outputBase.
Leopold Schabel16267052019-11-06 14:43:21 +000024CACHE_VOLUME=bazel-cache-$(getWorkingCopyID)
Serge Bazanskic3ad8462021-01-08 16:45:51 +010025# Secondary disk cache for Bazel, used to keep build data between configuration
26# switches (saving from spurious rebuilds when switchint from debug to
27# non-debug builds).
28SECONDARY_CACHE_VOLUME=bazel-secondary-cache-$(getWorkingCopyID)
29SECONDARY_CACHE_LOCATION="/user/.cache/bazel-secondary"
30# TODO(q3k): Neither the main nor secondary caches are garbage collected and
31# they will slowly fill up the disk of the CI builder.
Leopold Schabel16267052019-11-06 14:43:21 +000032
Leopold Schabelda5cfaa2020-02-24 10:07:10 +010033# The Go pkg cache is safe to use concurrently.
34GOPKG_VOLUME=gopkg-cache
35
Serge Bazanskic3ad8462021-01-08 16:45:51 +010036cat > ci.bazelrc <<EOF
37build --disk_cache=${SECONDARY_CACHE_LOCATION}
38EOF
39
Leopold Schabel822341a2020-02-03 21:51:47 +010040# We do our own image caching since the podman build step cache does
41# not work across different repository checkouts and is also easily
42# invalidated by multiple in-flight revisions with different Dockerfiles.
43if ! podman image inspect "$TAG" >/dev/null; then
44 echo "Could not find $TAG, building..."
45 podman build -t ${TAG} build
46fi
Leopold Schabel052af2d2019-11-06 02:21:53 +000047
Leopold Schabel052af2d2019-11-06 02:21:53 +000048function cleanup {
49 rc=$?
Serge Bazanskic3ad8462021-01-08 16:45:51 +010050 ! podman kill $CONTAINER
51 ! podman rm $CONTAINER --force
Leopold Schabel052af2d2019-11-06 02:21:53 +000052 exit $rc
53}
54
55trap cleanup EXIT
56
Serge Bazanskic3ad8462021-01-08 16:45:51 +010057! podman kill $CONTAINER
58! podman rm $CONTAINER --force
59
Leopold Schabel16267052019-11-06 14:43:21 +000060! podman volume create --opt o=nodev,exec ${CACHE_VOLUME}
Serge Bazanskic3ad8462021-01-08 16:45:51 +010061! podman volume create --opt o=nodev ${SECONDARY_CACHE_VOLUME}
Leopold Schabelda5cfaa2020-02-24 10:07:10 +010062! podman volume create --opt o=nodev ${GOPKG_VOLUME}
Leopold Schabel052af2d2019-11-06 02:21:53 +000063
Serge Bazanskic3ad8462021-01-08 16:45:51 +010064function bazel() {
65 podman run \
66 --rm \
67 --name $CONTAINER \
68 -v $(pwd):/work \
69 -v ${CACHE_VOLUME}:/user/.cache/bazel/_bazel_root \
70 -v ${SECONDARY_CACHE_VOLUME}:${SECONDARY_CACHE_LOCATION} \
71 -v ${GOPKG_VOLUME}:/user/go/pkg \
72 --privileged \
73 ${TAG} \
74 bazel "$@"
75}
Leopold Schabel052af2d2019-11-06 02:21:53 +000076
Serge Bazanskic3ad8462021-01-08 16:45:51 +010077bazel run //:fietsje
78bazel run //:gazelle -- update
Leopold Schabelab0cc822020-02-03 21:11:16 +010079
80if [[ ! -z "$(git status --porcelain)" ]]; then
Serge Bazanskic3ad8462021-01-08 16:45:51 +010081 echo "Unclean working directory after running gazelle and fietsje:"
Leopold Schabelab0cc822020-02-03 21:11:16 +010082 git diff HEAD
Serge Bazanskic3ad8462021-01-08 16:45:51 +010083 cat <<EOF
84Please run:
85
86 $ bazel run //:fietsje
87 $ bazel run //:gazelle -- update
88
89in your local branch and add the resulting changes to this diff.
90EOF
Leopold Schabelab0cc822020-02-03 21:11:16 +010091 exit 1
92fi
93
Serge Bazanskic3ad8462021-01-08 16:45:51 +010094bazel test //...
95bazel test //... -c dbg
Leopold Schabel052af2d2019-11-06 02:21:53 +000096
Leopold Schabel052af2d2019-11-06 02:21:53 +000097function conduit() {
98 # Get Phabricator host from Git origin
99 local pattern='ssh://(.+?):([0-9]+)'
100 [[ "$(git remote get-url origin)" =~ $pattern ]];
101 local host=${BASH_REMATCH[1]}
102 local port=${BASH_REMATCH[2]}
103
104 ssh "$host" -p "$port" conduit $@
105}
106
107# Report build results if we made it here successfully
108conduit harbormaster.sendmessage <<EOF
109{"params": "{\"buildTargetPHID\": \"${BUILD_PHID}\", \"type\": \"pass\"}"}
110EOF