|
| 1 | +#!/usr/bin/env bash |
| 2 | +# run-tests-with-custom-go.sh |
| 3 | +# |
| 4 | +# Downloads and builds a custom Go toolchain from cockcroachdb/go (specific |
| 5 | +# branch), caches it in ~/.cache/cockcroachdb-go/<branch>/<sha>, and runs |
| 6 | +# `go test`. |
| 7 | +# |
| 8 | +# Works in GitHub Actions or when run manually. |
| 9 | +# In CI: pass GO_SHA to pin an exact commit. |
| 10 | +# Locally: if GO_SHA is unset, script fetches the latest branch tip. |
| 11 | + |
| 12 | +set -euo pipefail |
| 13 | + |
| 14 | +GO_REPO="https://github.com/cockroachdb/go.git" |
| 15 | +GO_SHA="${GO_SHA:-}" |
| 16 | + |
| 17 | +if [ -z "$GO_SHA" ]; then |
| 18 | + GO_BRANCH="${GO_BRANCH:-cockroach-go1.23.12}" |
| 19 | + echo "==> Resolving latest SHA for branch $GO_BRANCH..." |
| 20 | + GO_SHA=$(git ls-remote "$GO_REPO" "refs/heads/$GO_BRANCH" | cut -f1) |
| 21 | +fi |
| 22 | + |
| 23 | +# Use GITHUB_WORKSPACE if present (GitHub Actions), else current dir |
| 24 | +REPO_ROOT="${GITHUB_WORKSPACE:-$(pwd)}" |
| 25 | + |
| 26 | +# Cache location (works locally and in CI) |
| 27 | +CACHE_BASE="${XDG_CACHE_HOME:-$HOME/.cache}/cockroachdb-go" |
| 28 | +CACHE_DIR="$CACHE_BASE/$GO_SHA" |
| 29 | +SRC_DIR="$CACHE_DIR/src" |
| 30 | + |
| 31 | +echo "==> Commit SHA: $GO_SHA" |
| 32 | +echo "==> Repository root: $REPO_ROOT" |
| 33 | +echo "==> Cache directory: $CACHE_DIR" |
| 34 | + |
| 35 | +if [ ! -x "$SRC_DIR/go/bin/go" ]; then |
| 36 | + echo "==> Building new Go toolchain..." |
| 37 | + mkdir -p "$SRC_DIR" |
| 38 | + rm -rf "$SRC_DIR/go" # in case of partial/incomplete build |
| 39 | + |
| 40 | + git clone "$GO_REPO" "$SRC_DIR/go" |
| 41 | + cd "$SRC_DIR/go" |
| 42 | + git checkout "$GO_SHA" |
| 43 | + |
| 44 | + cd "$SRC_DIR/go/src" |
| 45 | + ./make.bash |
| 46 | +else |
| 47 | + echo "==> Reusing cached Go toolchain" |
| 48 | +fi |
| 49 | + |
| 50 | +# Point environment to new Go |
| 51 | +export GOROOT="$SRC_DIR/go" |
| 52 | +export PATH="$GOROOT/bin:$PATH" |
| 53 | + |
| 54 | +echo "==> Custom Go version:" |
| 55 | +go version |
| 56 | + |
| 57 | +echo "==> Running tests in $REPO_ROOT" |
| 58 | +cd "$REPO_ROOT" |
| 59 | +echo go test -tags cockroach_go "$@" |
| 60 | +go test -tags cockroach_go "$@" |
0 commit comments