blob: 302bcfd056172916a3b5615c76430cc37e6f5030 [file] [log] [blame]
Leopold Schabel052af2d2019-11-06 02:21:53 +00001#!/bin/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
Leopold Schabel822341a2020-02-03 21:51:47 +010012TAG=nexantic-version-${DOCKERFILE_HASH}
Leopold Schabel052af2d2019-11-06 02:21:53 +000013POD=nexantic-build-${BUILD_ID}
14
Leopold Schabel16267052019-11-06 14:43:21 +000015# We keep one Bazel build cache per working copy to avoid concurrency issues
16# (we cannot run multiple Bazel servers on a given _bazel_root)
17function getWorkingCopyID {
18 local pattern='/var/drydock/workingcopy-([0-9]+)/'
19 [[ "$(pwd)" =~ $pattern ]]
20 echo ${BASH_REMATCH[1]}
21}
22
23CACHE_VOLUME=bazel-cache-$(getWorkingCopyID)
24
Leopold Schabel822341a2020-02-03 21:51:47 +010025# We do our own image caching since the podman build step cache does
26# not work across different repository checkouts and is also easily
27# invalidated by multiple in-flight revisions with different Dockerfiles.
28if ! podman image inspect "$TAG" >/dev/null; then
29 echo "Could not find $TAG, building..."
30 podman build -t ${TAG} build
31fi
Leopold Schabel052af2d2019-11-06 02:21:53 +000032
33# Keep this in sync with create_container.sh:
34
35function cleanup {
36 rc=$?
37 ! podman pod rm $POD --force
Leopold Schabel052af2d2019-11-06 02:21:53 +000038 exit $rc
39}
40
41trap cleanup EXIT
42
Leopold Schabel16267052019-11-06 14:43:21 +000043! podman volume create --opt o=nodev,exec ${CACHE_VOLUME}
Leopold Schabel052af2d2019-11-06 02:21:53 +000044
45podman pod create --name ${POD}
46
Leopold Schabelab0cc822020-02-03 21:11:16 +010047podman run \
48 --rm \
49 -v $(pwd):/work \
50 -v ${CACHE_VOLUME}:/user/.cache/bazel/_bazel_root \
51 --privileged \
52 ${TAG} \
53 scripts/gazelle.sh
54
55if [[ ! -z "$(git status --porcelain)" ]]; then
56 echo "Unclean working directory after running scripts/gazelle.sh:"
57 git diff HEAD
58 exit 1
59fi
60
Leopold Schabel052af2d2019-11-06 02:21:53 +000061podman run -d \
62 --pod ${POD} \
63 --ulimit nofile=262144:262144 \
64 --name=${POD}-cockroach \
65 cockroachdb/cockroach:v19.1.5 start --insecure
66
67podman run \
68 -v $(pwd):/work \
Leopold Schabel5b87d7b2019-11-06 14:56:30 +000069 -v ${CACHE_VOLUME}:/user/.cache/bazel/_bazel_root \
Leopold Schabel052af2d2019-11-06 02:21:53 +000070 --device /dev/kvm \
71 --privileged \
72 --pod ${POD} \
73 --name=${POD}-bazel \
74 ${TAG} \
Leopold Schabel383d4bb2019-11-14 22:53:58 +010075 bazel test //...
Leopold Schabel052af2d2019-11-06 02:21:53 +000076
Leopold Schabel052af2d2019-11-06 02:21:53 +000077function conduit() {
78 # Get Phabricator host from Git origin
79 local pattern='ssh://(.+?):([0-9]+)'
80 [[ "$(git remote get-url origin)" =~ $pattern ]];
81 local host=${BASH_REMATCH[1]}
82 local port=${BASH_REMATCH[2]}
83
84 ssh "$host" -p "$port" conduit $@
85}
86
87# Report build results if we made it here successfully
88conduit harbormaster.sendmessage <<EOF
89{"params": "{\"buildTargetPHID\": \"${BUILD_PHID}\", \"type\": \"pass\"}"}
90EOF