blob: d8fcf817603a59b236e2b4a5ca987db7948c2865 [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 Schabelda5cfaa2020-02-24 10:07:10 +010025# The Go pkg cache is safe to use concurrently.
26GOPKG_VOLUME=gopkg-cache
27
Leopold Schabel822341a2020-02-03 21:51:47 +010028# We do our own image caching since the podman build step cache does
29# not work across different repository checkouts and is also easily
30# invalidated by multiple in-flight revisions with different Dockerfiles.
31if ! podman image inspect "$TAG" >/dev/null; then
32 echo "Could not find $TAG, building..."
33 podman build -t ${TAG} build
34fi
Leopold Schabel052af2d2019-11-06 02:21:53 +000035
36# Keep this in sync with create_container.sh:
37
38function cleanup {
39 rc=$?
40 ! podman pod rm $POD --force
Leopold Schabel052af2d2019-11-06 02:21:53 +000041 exit $rc
42}
43
44trap cleanup EXIT
45
Leopold Schabel16267052019-11-06 14:43:21 +000046! podman volume create --opt o=nodev,exec ${CACHE_VOLUME}
Leopold Schabelda5cfaa2020-02-24 10:07:10 +010047! podman volume create --opt o=nodev ${GOPKG_VOLUME}
Leopold Schabel052af2d2019-11-06 02:21:53 +000048
49podman pod create --name ${POD}
50
Leopold Schabelab0cc822020-02-03 21:11:16 +010051podman run \
52 --rm \
53 -v $(pwd):/work \
54 -v ${CACHE_VOLUME}:/user/.cache/bazel/_bazel_root \
Leopold Schabelda5cfaa2020-02-24 10:07:10 +010055 -v ${GOPKG_VOLUME}:/user/go/pkg \
Leopold Schabelab0cc822020-02-03 21:11:16 +010056 --privileged \
57 ${TAG} \
58 scripts/gazelle.sh
59
60if [[ ! -z "$(git status --porcelain)" ]]; then
61 echo "Unclean working directory after running scripts/gazelle.sh:"
62 git diff HEAD
63 exit 1
64fi
65
Leopold Schabel052af2d2019-11-06 02:21:53 +000066podman run -d \
67 --pod ${POD} \
68 --ulimit nofile=262144:262144 \
69 --name=${POD}-cockroach \
70 cockroachdb/cockroach:v19.1.5 start --insecure
71
72podman run \
73 -v $(pwd):/work \
Leopold Schabel5b87d7b2019-11-06 14:56:30 +000074 -v ${CACHE_VOLUME}:/user/.cache/bazel/_bazel_root \
Leopold Schabelda5cfaa2020-02-24 10:07:10 +010075 -v ${GOPKG_VOLUME}:/user/go/pkg \
Leopold Schabel052af2d2019-11-06 02:21:53 +000076 --device /dev/kvm \
77 --privileged \
78 --pod ${POD} \
79 --name=${POD}-bazel \
80 ${TAG} \
Leopold Schabel383d4bb2019-11-14 22:53:58 +010081 bazel test //...
Leopold Schabel052af2d2019-11-06 02:21:53 +000082
Leopold Schabel052af2d2019-11-06 02:21:53 +000083function conduit() {
84 # Get Phabricator host from Git origin
85 local pattern='ssh://(.+?):([0-9]+)'
86 [[ "$(git remote get-url origin)" =~ $pattern ]];
87 local host=${BASH_REMATCH[1]}
88 local port=${BASH_REMATCH[2]}
89
90 ssh "$host" -p "$port" conduit $@
91}
92
93# Report build results if we made it here successfully
94conduit harbormaster.sendmessage <<EOF
95{"params": "{\"buildTargetPHID\": \"${BUILD_PHID}\", \"type\": \"pass\"}"}
96EOF