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 | |
Serge Bazanski | febf0b0 | 2021-01-07 16:34:28 +0100 | [diff] [blame] | 12 | TAG=monogon-version-${DOCKERFILE_HASH} |
Serge Bazanski | c3ad846 | 2021-01-08 16:45:51 +0100 | [diff] [blame] | 13 | CONTAINER=monogon-build-${BUILD_ID} |
Leopold Schabel | 052af2d | 2019-11-06 02:21:53 +0000 | [diff] [blame] | 14 | |
Serge Bazanski | c3ad846 | 2021-01-08 16:45:51 +0100 | [diff] [blame] | 15 | # 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 Schabel | 1626705 | 2019-11-06 14:43:21 +0000 | [diff] [blame] | 17 | function getWorkingCopyID { |
| 18 | local pattern='/var/drydock/workingcopy-([0-9]+)/' |
| 19 | [[ "$(pwd)" =~ $pattern ]] |
| 20 | echo ${BASH_REMATCH[1]} |
| 21 | } |
| 22 | |
Serge Bazanski | c3ad846 | 2021-01-08 16:45:51 +0100 | [diff] [blame] | 23 | # Main Bazel cache, used as Bazel outputRoot/outputBase. |
Leopold Schabel | 1626705 | 2019-11-06 14:43:21 +0000 | [diff] [blame] | 24 | CACHE_VOLUME=bazel-cache-$(getWorkingCopyID) |
Serge Bazanski | c3ad846 | 2021-01-08 16:45:51 +0100 | [diff] [blame] | 25 | # 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). |
| 28 | SECONDARY_CACHE_VOLUME=bazel-secondary-cache-$(getWorkingCopyID) |
| 29 | SECONDARY_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 Schabel | 1626705 | 2019-11-06 14:43:21 +0000 | [diff] [blame] | 32 | |
Leopold Schabel | da5cfaa | 2020-02-24 10:07:10 +0100 | [diff] [blame] | 33 | # The Go pkg cache is safe to use concurrently. |
| 34 | GOPKG_VOLUME=gopkg-cache |
| 35 | |
Serge Bazanski | c3ad846 | 2021-01-08 16:45:51 +0100 | [diff] [blame] | 36 | cat > ci.bazelrc <<EOF |
| 37 | build --disk_cache=${SECONDARY_CACHE_LOCATION} |
| 38 | EOF |
| 39 | |
Leopold Schabel | 822341a | 2020-02-03 21:51:47 +0100 | [diff] [blame] | 40 | # 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. |
| 43 | if ! podman image inspect "$TAG" >/dev/null; then |
| 44 | echo "Could not find $TAG, building..." |
| 45 | podman build -t ${TAG} build |
| 46 | fi |
Leopold Schabel | 052af2d | 2019-11-06 02:21:53 +0000 | [diff] [blame] | 47 | |
Leopold Schabel | 052af2d | 2019-11-06 02:21:53 +0000 | [diff] [blame] | 48 | function cleanup { |
| 49 | rc=$? |
Serge Bazanski | c3ad846 | 2021-01-08 16:45:51 +0100 | [diff] [blame] | 50 | ! podman kill $CONTAINER |
| 51 | ! podman rm $CONTAINER --force |
Leopold Schabel | 052af2d | 2019-11-06 02:21:53 +0000 | [diff] [blame] | 52 | exit $rc |
| 53 | } |
| 54 | |
| 55 | trap cleanup EXIT |
| 56 | |
Serge Bazanski | c3ad846 | 2021-01-08 16:45:51 +0100 | [diff] [blame] | 57 | ! podman kill $CONTAINER |
| 58 | ! podman rm $CONTAINER --force |
| 59 | |
Leopold Schabel | 1626705 | 2019-11-06 14:43:21 +0000 | [diff] [blame] | 60 | ! podman volume create --opt o=nodev,exec ${CACHE_VOLUME} |
Serge Bazanski | c3ad846 | 2021-01-08 16:45:51 +0100 | [diff] [blame] | 61 | ! podman volume create --opt o=nodev ${SECONDARY_CACHE_VOLUME} |
Leopold Schabel | da5cfaa | 2020-02-24 10:07:10 +0100 | [diff] [blame] | 62 | ! podman volume create --opt o=nodev ${GOPKG_VOLUME} |
Leopold Schabel | 052af2d | 2019-11-06 02:21:53 +0000 | [diff] [blame] | 63 | |
Serge Bazanski | c3ad846 | 2021-01-08 16:45:51 +0100 | [diff] [blame] | 64 | function 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 Schabel | 052af2d | 2019-11-06 02:21:53 +0000 | [diff] [blame] | 76 | |
Serge Bazanski | c3ad846 | 2021-01-08 16:45:51 +0100 | [diff] [blame] | 77 | bazel run //:fietsje |
| 78 | bazel run //:gazelle -- update |
Leopold Schabel | ab0cc82 | 2020-02-03 21:11:16 +0100 | [diff] [blame] | 79 | |
| 80 | if [[ ! -z "$(git status --porcelain)" ]]; then |
Serge Bazanski | c3ad846 | 2021-01-08 16:45:51 +0100 | [diff] [blame] | 81 | echo "Unclean working directory after running gazelle and fietsje:" |
Leopold Schabel | ab0cc82 | 2020-02-03 21:11:16 +0100 | [diff] [blame] | 82 | git diff HEAD |
Serge Bazanski | c3ad846 | 2021-01-08 16:45:51 +0100 | [diff] [blame] | 83 | cat <<EOF |
| 84 | Please run: |
| 85 | |
| 86 | $ bazel run //:fietsje |
| 87 | $ bazel run //:gazelle -- update |
| 88 | |
| 89 | in your local branch and add the resulting changes to this diff. |
| 90 | EOF |
Leopold Schabel | ab0cc82 | 2020-02-03 21:11:16 +0100 | [diff] [blame] | 91 | exit 1 |
| 92 | fi |
| 93 | |
Serge Bazanski | c3ad846 | 2021-01-08 16:45:51 +0100 | [diff] [blame] | 94 | bazel test //... |
| 95 | bazel test //... -c dbg |
Leopold Schabel | 052af2d | 2019-11-06 02:21:53 +0000 | [diff] [blame] | 96 | |
Leopold Schabel | 052af2d | 2019-11-06 02:21:53 +0000 | [diff] [blame] | 97 | function 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 |
| 108 | conduit harbormaster.sendmessage <<EOF |
| 109 | {"params": "{\"buildTargetPHID\": \"${BUILD_PHID}\", \"type\": \"pass\"}"} |
| 110 | EOF |