blob: de521b02d69a47779d34ef352a79c7cb71ee5f33 [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
Serge Bazanskifebf0b02021-01-07 16:34:28 +010012TAG=monogon-version-${DOCKERFILE_HASH}
13POD=monogon-build-${BUILD_ID}
Leopold Schabel052af2d2019-11-06 02:21:53 +000014
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} \
Leopold Schabel3b544a92020-06-08 19:24:42 +020058 bazel run //:fietsje
59
60podman run \
61 --rm \
62 -v $(pwd):/work \
63 -v ${CACHE_VOLUME}:/user/.cache/bazel/_bazel_root \
64 -v ${GOPKG_VOLUME}:/user/go/pkg \
65 --privileged \
66 ${TAG} \
Leopold Schabelab0cc822020-02-03 21:11:16 +010067 scripts/gazelle.sh
68
69if [[ ! -z "$(git status --porcelain)" ]]; then
Leopold Schabel3b544a92020-06-08 19:24:42 +020070 echo "Unclean working directory after running scripts/gazelle.sh and fietsje:"
Leopold Schabelab0cc822020-02-03 21:11:16 +010071 git diff HEAD
72 exit 1
73fi
74
Leopold Schabel052af2d2019-11-06 02:21:53 +000075podman run \
76 -v $(pwd):/work \
Leopold Schabel5b87d7b2019-11-06 14:56:30 +000077 -v ${CACHE_VOLUME}:/user/.cache/bazel/_bazel_root \
Leopold Schabelda5cfaa2020-02-24 10:07:10 +010078 -v ${GOPKG_VOLUME}:/user/go/pkg \
Leopold Schabel052af2d2019-11-06 02:21:53 +000079 --device /dev/kvm \
80 --privileged \
81 --pod ${POD} \
82 --name=${POD}-bazel \
83 ${TAG} \
Lorenz Brun5e4fc2d2020-09-22 18:35:15 +020084 bazel test //...
Leopold Schabel052af2d2019-11-06 02:21:53 +000085
Leopold Schabel052af2d2019-11-06 02:21:53 +000086function conduit() {
87 # Get Phabricator host from Git origin
88 local pattern='ssh://(.+?):([0-9]+)'
89 [[ "$(git remote get-url origin)" =~ $pattern ]];
90 local host=${BASH_REMATCH[1]}
91 local port=${BASH_REMATCH[2]}
92
93 ssh "$host" -p "$port" conduit $@
94}
95
96# Report build results if we made it here successfully
97conduit harbormaster.sendmessage <<EOF
98{"params": "{\"buildTargetPHID\": \"${BUILD_PHID}\", \"type\": \"pass\"}"}
99EOF