Leopold Schabel | 052af2d | 2019-11-06 02:21:53 +0000 | [diff] [blame] | 1 | #!/bin/bash |
Leopold Schabel | 383d4bb | 2019-11-14 22:53:58 +0100 | [diff] [blame] | 2 | # This helper scripts executes all Bazel tests in our CI environment. |
| 3 | # https://phab.monogon.dev/harbormaster/plan/2/ |
Leopold Schabel | 052af2d | 2019-11-06 02:21:53 +0000 | [diff] [blame] | 4 | set -euo pipefail |
| 5 | |
Leopold Schabel | 822341a | 2020-02-03 21:51:47 +0100 | [diff] [blame] | 6 | DOCKERFILE_HASH=$(sha1sum build/Dockerfile | cut -c -8) |
| 7 | |
Leopold Schabel | 052af2d | 2019-11-06 02:21:53 +0000 | [diff] [blame] | 8 | BUILD_ID=$1; |
| 9 | BUILD_PHID=$2; |
| 10 | shift; shift; |
| 11 | |
Leopold Schabel | 822341a | 2020-02-03 21:51:47 +0100 | [diff] [blame] | 12 | TAG=nexantic-version-${DOCKERFILE_HASH} |
Leopold Schabel | 052af2d | 2019-11-06 02:21:53 +0000 | [diff] [blame] | 13 | POD=nexantic-build-${BUILD_ID} |
| 14 | |
Leopold Schabel | 1626705 | 2019-11-06 14:43:21 +0000 | [diff] [blame] | 15 | # 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) |
| 17 | function getWorkingCopyID { |
| 18 | local pattern='/var/drydock/workingcopy-([0-9]+)/' |
| 19 | [[ "$(pwd)" =~ $pattern ]] |
| 20 | echo ${BASH_REMATCH[1]} |
| 21 | } |
| 22 | |
| 23 | CACHE_VOLUME=bazel-cache-$(getWorkingCopyID) |
| 24 | |
Leopold Schabel | 822341a | 2020-02-03 21:51:47 +0100 | [diff] [blame] | 25 | # 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. |
| 28 | if ! podman image inspect "$TAG" >/dev/null; then |
| 29 | echo "Could not find $TAG, building..." |
| 30 | podman build -t ${TAG} build |
| 31 | fi |
Leopold Schabel | 052af2d | 2019-11-06 02:21:53 +0000 | [diff] [blame] | 32 | |
| 33 | # Keep this in sync with create_container.sh: |
| 34 | |
| 35 | function cleanup { |
| 36 | rc=$? |
| 37 | ! podman pod rm $POD --force |
Leopold Schabel | 052af2d | 2019-11-06 02:21:53 +0000 | [diff] [blame] | 38 | exit $rc |
| 39 | } |
| 40 | |
| 41 | trap cleanup EXIT |
| 42 | |
Leopold Schabel | 1626705 | 2019-11-06 14:43:21 +0000 | [diff] [blame] | 43 | ! podman volume create --opt o=nodev,exec ${CACHE_VOLUME} |
Leopold Schabel | 052af2d | 2019-11-06 02:21:53 +0000 | [diff] [blame] | 44 | |
| 45 | podman pod create --name ${POD} |
| 46 | |
Leopold Schabel | ab0cc82 | 2020-02-03 21:11:16 +0100 | [diff] [blame^] | 47 | podman 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 | |
| 55 | if [[ ! -z "$(git status --porcelain)" ]]; then |
| 56 | echo "Unclean working directory after running scripts/gazelle.sh:" |
| 57 | git diff HEAD |
| 58 | exit 1 |
| 59 | fi |
| 60 | |
Leopold Schabel | 052af2d | 2019-11-06 02:21:53 +0000 | [diff] [blame] | 61 | podman run -d \ |
| 62 | --pod ${POD} \ |
| 63 | --ulimit nofile=262144:262144 \ |
| 64 | --name=${POD}-cockroach \ |
| 65 | cockroachdb/cockroach:v19.1.5 start --insecure |
| 66 | |
| 67 | podman run \ |
| 68 | -v $(pwd):/work \ |
Leopold Schabel | 5b87d7b | 2019-11-06 14:56:30 +0000 | [diff] [blame] | 69 | -v ${CACHE_VOLUME}:/user/.cache/bazel/_bazel_root \ |
Leopold Schabel | 052af2d | 2019-11-06 02:21:53 +0000 | [diff] [blame] | 70 | --device /dev/kvm \ |
| 71 | --privileged \ |
| 72 | --pod ${POD} \ |
| 73 | --name=${POD}-bazel \ |
| 74 | ${TAG} \ |
Leopold Schabel | 383d4bb | 2019-11-14 22:53:58 +0100 | [diff] [blame] | 75 | bazel test //... |
Leopold Schabel | 052af2d | 2019-11-06 02:21:53 +0000 | [diff] [blame] | 76 | |
Leopold Schabel | 052af2d | 2019-11-06 02:21:53 +0000 | [diff] [blame] | 77 | function 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 |
| 88 | conduit harbormaster.sendmessage <<EOF |
| 89 | {"params": "{\"buildTargetPHID\": \"${BUILD_PHID}\", \"type\": \"pass\"}"} |
| 90 | EOF |