2023-10-04 07:55:38 -04:00
name : Setup bazel and Nix
description : Setup Bazel and Nix for CI builds and tests
2023-03-20 11:05:08 -04:00
inputs :
useCache :
2023-11-03 06:59:19 -04:00
description : "Cache Bazel artifacts. Use 'true' to enable with rw, 'readonly' to download, 'rbe' to enable with remote execution, 'log' to disable cache but upload logs, and 'false' to disable."
2023-03-20 11:05:08 -04:00
default : "false"
required : true
buildBuddyApiKey :
description : "BuildBuddy API key for caching Bazel artifacts"
required : false
2023-11-03 06:59:19 -04:00
rbePlatform :
description : "RBE platform to use. If empty, RBE will not be used."
required : false
2023-11-16 08:41:34 -05:00
nixTools :
description : "Nix tools to install as list of strings separated by newlines. If empty, no tools will be installed."
default : ""
required : false
2023-03-20 11:05:08 -04:00
runs :
using : "composite"
steps :
- name : Check inputs
2023-10-04 07:55:38 -04:00
id : check_inputs
2023-03-20 11:05:08 -04:00
shell : bash
run : |
echo "::group::Check inputs"
2023-11-03 06:59:19 -04:00
if [[ "${{ inputs.useCache }}" != "true" && "${{ inputs.useCache }}" != "readonly" && "${{ inputs.useCache }}" != "rbe" && "${{ inputs.useCache }}" != "logs" && "${{ inputs.useCache }}" != "false" ]]; then
2023-03-20 11:05:08 -04:00
echo "Invalid value for 'useCache' input: '${{ inputs.useCache }}'. Must be 'true', 'readonly', or 'false'."
exit 1
fi
2023-06-23 06:12:32 -04:00
if [[ "${{ inputs.useCache }}" == "true" || "${{ inputs.useCache }}" == "readonly" || "${{ inputs.useCache }}" == "logs" ]] && [[ -z "${{ inputs.buildBuddyApiKey }}" ]]; then
2023-03-20 11:05:08 -04:00
echo "BuildBuddy API key is required when cache is enabled."
exit 1
fi
2023-11-03 06:59:19 -04:00
if [[ "${{ inputs.useCache }}" == "rbe" && -z "${{ inputs.rbePlatform }}" ]]; then
echo "RBE platform is required when cache is enabled."
exit 1
fi
if [[ -n "${{inputs.rbePlatform}}" ]]; then
case "${{ inputs.rbePlatform }}" in
ubuntu-22.04)
echo "rbeConfig=build_barn_rbe_ubuntu_22_04" | tee -a "$GITHUB_OUTPUT"
;;
*)
echo "Invalid value for 'rbePlatform' input: '${{ inputs.rbePlatform }}'. Must be 'ubuntu-22.04'."
exit 1
;;
esac
fi
2023-10-04 07:55:38 -04:00
if command -v nix; then
echo "nixPreinstalled=true" | tee -a "$GITHUB_OUTPUT"
else
echo "nixPreinstalled=false" | tee -a "$GITHUB_OUTPUT"
fi
if command -v bazel; then
echo "bazelPreinstalled=true" | tee -a "$GITHUB_OUTPUT"
else
echo "bazelPreinstalled=false" | tee -a "$GITHUB_OUTPUT"
fi
if [[ -f /etc/NIXOS ]]; then
echo "nixOS=true" | tee -a "$GITHUB_OUTPUT"
else
echo "nixOS=false" | tee -a "$GITHUB_OUTPUT"
fi
if [[ "$RUNNER_OS" == "Linux" ]]; then
echo "os=linux" | tee -a "$GITHUB_OUTPUT"
elif [[ "$RUNNER_OS" == "Windows" ]]; then
echo "os=windows" | tee -a "$GITHUB_OUTPUT"
elif [[ "$RUNNER_OS" == "macOS" ]]; then
echo "os=darwin" | tee -a "$GITHUB_OUTPUT"
else
echo "$RUNNER_OS not supported"
exit 1
fi
if [[ "$RUNNER_ARCH" == "X64" ]]; then
echo "arch=amd64" | tee -a "$GITHUB_OUTPUT"
elif [[ "$RUNNER_ARCH" == "ARM64" ]]; then
echo "arch=arm64" | tee -a "$GITHUB_OUTPUT"
else
echo "$RUNNER_ARCH not supported"
exit 1
fi
echo "::endgroup::"
2023-12-21 12:12:07 -05:00
- name : Install current Bash on macOS
shell : bash
if : runner.os == 'macOS'
run : brew install bash
2023-11-03 06:59:19 -04:00
- name : Prepare to install tools
shell : bash
run : |
echo "::group::Prepare to install nix and bazel"
requiredTools=( "curl" "xz" "unzip" "git" )
declare -A packageNamesUbuntu=( ["curl"]="curl" ["xz"]="xz-utils" ["unzip"]="unzip" ["git"]="git" )
missingTools=()
for tool in "${requiredTools[@]}"; do
if ! command -v "$tool"; then
echo "$tool not found, installing..."
missingTools+=("$tool")
else
echo "$tool found $(command -v "$tool")"
fi
done
missingPackagesUbuntu=()
for tool in "${missingTools[@]}"; do
echo "Ubuntu name for $tool is ${packageNamesUbuntu[$tool]}"
missingPackagesUbuntu+=("${packageNamesUbuntu[$tool]}")
done
if [[ "${#missingTools[@]}" -gt 0 ]]; then
echo "Installing missing tools ${missingTools[*]}..."
if [[ "$RUNNER_OS" == "Linux" ]]; then
sudo apt-get update || true
sudo apt-get install -y ${missingPackagesUbuntu[*]} || true
fi
fi
echo "::endgroup::"
2023-10-04 07:55:38 -04:00
- name : Install nix
if : steps.check_inputs.outputs.nixPreinstalled == 'false'
2023-12-21 02:43:44 -05:00
uses : cachix/install-nix-action@7ac1ec25491415c381d9b62f0657c7a028df52a7 # v24
2023-10-04 07:55:38 -04:00
2023-10-12 10:11:02 -04:00
- name : Set $USER if not set
shell : bash
run : |
echo "::group::Set \$USER if not set"
if [[ -z "$USER" ]]; then
echo "USER=$(id -un)" | tee -a "$GITHUB_ENV"
fi
echo "::endgroup::"
2023-10-04 07:55:38 -04:00
- name : Install Bazelisk
if : steps.check_inputs.outputs.bazelPreinstalled == 'false' && steps.check_inputs.outputs.nixOS == 'false'
shell : bash
env :
OS : ${{ steps.check_inputs.outputs.os }}
ARCH : ${{ steps.check_inputs.outputs.arch }}
run : |
echo "::group::Install Bazelisk"
2023-11-03 06:59:19 -04:00
sudo mkdir -p /usr/local/bin
sudo chown -R "$USER" /usr/local/bin
2023-10-04 07:55:38 -04:00
curl -fsSLo /usr/local/bin/bazel "https://github.com/bazelbuild/bazelisk/releases/download/v1.18.0/bazelisk-${OS}-${ARCH}"
chmod +x /usr/local/bin/bazel
2023-03-20 11:05:08 -04:00
echo "::endgroup::"
2023-03-21 05:06:32 -04:00
2023-10-09 05:00:22 -04:00
- name : Free up space (Ubuntu)
shell : bash
if : startsWith(runner.name, 'GitHub Actions') && runner.os == 'Linux'
run : |
echo "::group::Free up space (Ubuntu)"
echo "Available storage (before):"
df -h
2023-10-12 08:07:59 -04:00
sudo apt-get update || true
sudo apt-get remove -y '^dotnet-.*' || true
sudo apt-get remove -y '^llvm-.*' || true
sudo apt-get remove -y 'php.*' || true
sudo apt-get remove -y '^mongodb-.*' || true
sudo apt-get remove -y '^mysql-.*' || true
2023-10-09 05:00:22 -04:00
sudo rm -rf /usr/share/dotnet
sudo rm -rf /usr/local/lib/android
sudo rm -rf /opt/ghc
sudo rm -rf /opt/hostedtoolcache/CodeQL
sudo docker image prune --all --force
2023-10-12 08:07:59 -04:00
sudo apt-get autoremove -y || true
sudo apt-get clean || true
2023-10-09 05:00:22 -04:00
echo "Available storage (after):"
df -h
echo "::endgroup::"
2023-05-19 09:55:41 -04:00
- name : Configure Bazel (general)
shell : bash
env :
WORKSPACE : ${{ github.workspace }}
run : |
echo "::group::Configure Bazel"
cat <<EOF >> "${WORKSPACE}/.bazeloverwriterc"
2023-08-18 09:12:21 -04:00
import %workspace%/bazel/bazelrc/ci.bazelrc
2023-05-19 09:55:41 -04:00
EOF
echo "::endgroup::"
2023-03-21 05:06:32 -04:00
2023-05-19 09:55:41 -04:00
- name : Configure Bazel (rw)
2023-03-20 11:05:08 -04:00
if : inputs.useCache == 'true' || inputs.useCache == 'readonly'
2023-10-04 07:55:38 -04:00
shell : bash
2023-03-20 11:05:08 -04:00
env :
BUILDBUDDY_ORG_API_KEY : ${{ inputs.buildBuddyApiKey }}
2023-05-19 09:55:41 -04:00
WORKSPACE : ${{ github.workspace }}
2023-03-20 11:05:08 -04:00
run : |
echo "::group::Configure Bazel"
2023-05-19 09:55:41 -04:00
cat <<EOF >> "${WORKSPACE}/.bazeloverwriterc"
2023-08-15 04:34:42 -04:00
common --bes_results_url=https://app.buildbuddy.io/invocation/
common --bes_backend=grpcs://remote.buildbuddy.io
common --remote_cache=grpcs://remote.buildbuddy.io
common --remote_header=x-buildbuddy-api-key=${BUILDBUDDY_ORG_API_KEY}
2023-08-18 09:12:21 -04:00
cquery --bes_results_url=
cquery --bes_backend=
cquery --remote_cache=
query --bes_results_url=
query --bes_backend=
query --remote_cache=
2023-03-20 11:05:08 -04:00
EOF
echo "::endgroup::"
2023-03-21 05:06:32 -04:00
2023-03-20 11:05:08 -04:00
- name : Configure Bazel (readonly)
2023-06-23 06:12:32 -04:00
if : inputs.useCache == 'readonly'
2023-10-04 07:55:38 -04:00
shell : bash
2023-06-09 10:59:19 -04:00
env :
WORKSPACE : ${{ github.workspace }}
2023-03-20 11:05:08 -04:00
run : |
echo "::group::Configure Bazel (readonly)"
2023-08-15 04:34:42 -04:00
echo "common --remote_upload_local_results=false" >> "${WORKSPACE}/.bazeloverwriterc"
2023-03-20 11:05:08 -04:00
echo "::endgroup::"
2023-03-21 05:06:32 -04:00
2023-06-23 06:12:32 -04:00
- name : Configure Bazel (logs)
if : inputs.useCache == 'logs'
2023-10-04 07:55:38 -04:00
shell : bash
2023-06-23 06:12:32 -04:00
env :
2023-11-03 06:59:19 -04:00
BUILDBUDDY_ORG_API_KEY : ${{ inputs.buildBuddyApiKey }}
WORKSPACE : ${{ github.workspace }}
run : |
echo "::group::Configure Bazel"
cat <<EOF >> "${WORKSPACE}/.bazeloverwriterc"
common --bes_results_url=https://app.buildbuddy.io/invocation/
common --bes_backend=grpcs://remote.buildbuddy.io
common --remote_header=x-buildbuddy-api-key=${BUILDBUDDY_ORG_API_KEY}
cquery --bes_results_url=
cquery --bes_backend=
query --bes_results_url=
query --bes_backend=
EOF
echo "::endgroup::"
- name : Configure Bazel (rbe)
if : inputs.useCache == 'rbe'
shell : bash
env :
RBE_CONFIG : ${{ steps.check_inputs.outputs.rbeConfig }}
WORKSPACE : ${{ github.workspace }}
run : |
echo "::group::Configure Bazel"
cat <<EOF >> "${WORKSPACE}/.bazeloverwriterc"
common --config=${RBE_CONFIG}
common --repository_cache=/repository_cache
common --repo_env=GOPROXY=http://goproxy:3000
EOF
echo "::endgroup::"
- name : Configure Bazel (rbe logs)
if : inputs.useCache == 'rbe' && inputs.buildBuddyApiKey != ''
shell : bash
env :
2023-06-23 06:12:32 -04:00
BUILDBUDDY_ORG_API_KEY : ${{ inputs.buildBuddyApiKey }}
WORKSPACE : ${{ github.workspace }}
run : |
echo "::group::Configure Bazel"
cat <<EOF >> "${WORKSPACE}/.bazeloverwriterc"
2023-08-15 04:34:42 -04:00
common --bes_results_url=https://app.buildbuddy.io/invocation/
common --bes_backend=grpcs://remote.buildbuddy.io
common --remote_header=x-buildbuddy-api-key=${BUILDBUDDY_ORG_API_KEY}
2023-08-18 09:12:21 -04:00
cquery --bes_results_url=
cquery --bes_backend=
query --bes_results_url=
query --bes_backend=
2023-06-23 06:12:32 -04:00
EOF
echo "::endgroup::"
2023-06-09 10:59:19 -04:00
- name : Disable disk cache on GitHub Actions runners
2023-10-04 07:55:38 -04:00
if : startsWith(runner.name , 'GitHub Actions')
2023-06-09 10:59:19 -04:00
shell : bash
env :
WORKSPACE : ${{ github.workspace }}
run : |
echo "::group::Configure Bazel (disk cache)"
2023-08-15 04:34:42 -04:00
echo "common --disk_cache=" >> "${WORKSPACE}/.bazeloverwriterc"
echo "common --repository_cache=" >> "${WORKSPACE}/.bazeloverwriterc"
2023-06-09 10:59:19 -04:00
echo "::endgroup::"
2023-11-16 08:41:34 -05:00
- name : Install nix tools
if : inputs.nixTools != ''
shell : bash
env :
tools : ${{ inputs.nixTools }}
repository : ${{ github.repository }}
gitSha : ${{ github.sha }}
run : |
echo "::group::Install nix tools"
toolsNixList=$(printf ' "%s"' ${tools[@]})
toolsNixList="[ ${toolsNixList} ]"
expressionFile=$(mktemp)
cat << "EOF" > "${expressionFile}"
{ tools, repository, rev } :
let
repoFlake = builtins.getFlake ("github:" + repository + "/" + rev);
nixpkgs = repoFlake.inputs.nixpkgsUnstable;
pkgs = import nixpkgs { system = builtins.currentSystem; };
toolPkgs = map (p : pkgs.${p}) tools;
in
{
tools = pkgs.symlinkJoin { name = "tools"; paths = [ toolPkgs ]; };
pathVar = pkgs.lib.makeBinPath toolPkgs;
}
EOF
# ensure the store paths are created
nix-build \
--no -out-link \
--arg tools "${toolsNixList}" \
--argstr repository "${repository}" \
--argstr rev "${gitSha}" \
--attr tools \
"${expressionFile}"
# evaluate the path expression
# EXTRA_PATH=/nix/store/...:/nix/store/...:/nix/store/...
EXTRA_PATH=$(nix eval --raw --file "${expressionFile}" \
--arg tools "${toolsNixList}" \
--argstr repository "${repository}" \
--argstr rev "${gitSha}" \
pathVar)
echo "EXTRA_PATH=${EXTRA_PATH}"
echo "${EXTRA_PATH}" >> "${GITHUB_PATH}"
echo "::endgroup::"