diff --git a/bazel/container/README.md b/bazel/container/README.md new file mode 100644 index 000000000..b3c5079ca --- /dev/null +++ b/bazel/container/README.md @@ -0,0 +1,23 @@ +# Bazel build container + +This container enables running Bazel inside a container, with the host cache mounted. + +To use the container, run + +```shell +source container.sh +startBazelServer +``` + +You can then execute Bazel commands like you normally would do, as the sourced `bazel` +function shadows binaries you might have in your path: + +```shell +bazel query //... +``` + +To terminate the container, which is running as daemon in the background, execute + +```shell +stopBazelServer +``` diff --git a/bazel/container/container.sh b/bazel/container/container.sh new file mode 100644 index 000000000..3d818343d --- /dev/null +++ b/bazel/container/container.sh @@ -0,0 +1,71 @@ +#!/usr/bin/env bash + +function setup { + # Ensure that the cache directories exist, so they are not created by docker with root permissions. + mkdir -p "${HOME}/.cache/bazel" + mkdir -p "${HOME}/.cache/shared_bazel_repository_cache" + mkdir -p "${HOME}/.cache/shared_bazel_action_cache" +} + +function startBazelServer { + local containerImage="ghcr.io/edgelesssys/bazel-container:v6.1.0-0" + local containerName="bazeld" + + setup + + local hostWorkspaceDir + hostWorkspaceDir="$(git rev-parse --show-toplevel)" + if [[ $? -ne 0 ]]; then + echo Could not find git repository root. Are you in a git repository? + return 1 + fi + + echo Starting bazel container as daemon... + echo You can stop this command using: + echo docker kill "${containerName}" + + docker run \ + --rm \ + --detach \ + --name "${containerName}" \ + -v "${hostWorkspaceDir}":/workspace \ + -v "${HOME}/.cache/bazel":"/home/builder/.cache/bazel" \ + -v "${HOME}/.cache/shared_bazel_repository_cache":"/home/builder/.cache/shared_bazel_repository_cache" \ + -v "${HOME}/.cache/shared_bazel_action_cache":"/home/builder/.cache/shared_bazel_action_cache" \ + --entrypoint=/bin/sleep \ + "${containerImage}" \ + infinity || return $? +} + +function stopBazelServer { + local containerName="bazeld" + + echo Stopping bazel container... + + docker kill "${containerName}" || return $? +} + +function bazel { + local containerName="bazeld" + + local hostWorkspaceDir + hostWorkspaceDir="$(git rev-parse --show-toplevel)" + if [[ $? -ne 0 ]]; then + echo Could not find git repository root. Are you in a git repository? + return 1 + fi + + local containerWorkDir + containerWorkDir=$(realpath -m "/workspace/$(realpath --relative-base="${hostWorkspaceDir}" .)") + if [[ $? -ne 0 ]]; then + echo Could not determine container work directory. + return 1 + fi + + docker exec \ + -it \ + --workdir "${containerWorkDir}" \ + --env "HOST_CACHE=${HOME}/.cache" \ + "${containerName}" \ + bazel "$@" || return $? +}