Skip to content

Instantly share code, notes, and snippets.

@albertocavalcante
Last active March 5, 2025 10:40
Show Gist options
  • Save albertocavalcante/9a58a99e51c7a5c1b465fba594b06013 to your computer and use it in GitHub Desktop.
Save albertocavalcante/9a58a99e51c7a5c1b465fba594b06013 to your computer and use it in GitHub Desktop.
Bazel Dev Container Feature
#!/bin/bash
set -e
# Versions can be overridden with environment variables
BAZELISK_VERSION="${BAZELISK_VERSION:-v1.10.1}"
BUILDIFIER_VERSION="${BUILDIFIER_VERSION:-v6.0.1}"
LOCAL_BIN="/usr/local/bin"
if [ "$(id -u)" -ne 0 ]; then
echo -e 'Script must be run as root. Use sudo, su, or add "USER root" to your Dockerfile before running this script.'
exit 1
fi
# Bring in distribution info
. /etc/os-release
# Determine package manager and installation command
if type apt-get > /dev/null 2>&1; then
install_packages() {
apt-get update -y
apt-get install -y --no-install-recommends "$@"
rm -rf /var/lib/apt/lists/*
}
elif type microdnf > /dev/null 2>&1; then
install_packages() {
microdnf install -y "$@"
microdnf clean all
}
elif type dnf > /dev/null 2>&1; then
install_packages() {
dnf install -y "$@"
dnf clean all
}
elif type yum > /dev/null 2>&1; then
install_packages() {
yum install -y "$@"
yum clean all
}
else
echo "Unsupported package manager"
exit 1
fi
# Install dependencies
echo "Installing dependencies..."
install_packages curl ca-certificates
# Determine architecture
architecture="$(uname -m)"
case $architecture in
x86_64) ARCHITECTURE="amd64";;
aarch64 | armv8*) ARCHITECTURE="arm64";;
*) echo "(!) Architecture $architecture unsupported"; exit 1 ;;
esac
# Determine platform (linux is the most common, but support macOS just in case)
PLATFORM="linux"
if [ "$(uname -s)" = "Darwin" ]; then
PLATFORM="darwin"
fi
# Function to download from GitHub with support for "latest"
github_download() {
repo=$1
version=$2
artifact=$3
output=$4
if [ "${version}" = "latest" ]; then
echo "Determining latest release for ${repo}..."
version=$(curl -sSL "https://api.github.com/repos/${repo}/releases/latest" | grep -Po '"tag_name": "\K.*?(?=")')
if [ -z "${version}" ]; then
echo "Failed to determine latest version for ${repo}"
exit 1
fi
echo "Latest version is ${version}"
fi
# Remove 'v' prefix from version if it exists for URL construction
download_version="${version#v}"
URL="https://github.com/${repo}/releases/download/${version}/${artifact}"
echo "Downloading ${URL} to ${output}"
curl -fsSL "${URL}" -o "${output}"
if [ $? -ne 0 ]; then
echo "Download failed for ${URL}"
exit 1
fi
}
# Download and install bazelisk
echo "Installing bazelisk ${BAZELISK_VERSION}..."
github_download "bazelbuild/bazelisk" "${BAZELISK_VERSION}" "bazelisk-${PLATFORM}-${ARCHITECTURE}" "${LOCAL_BIN}/bazelisk"
chmod +x "${LOCAL_BIN}/bazelisk"
ln -sf "${LOCAL_BIN}/bazelisk" "${LOCAL_BIN}/bazel"
# Download and install buildifier
echo "Installing buildifier ${BUILDIFIER_VERSION}..."
github_download "bazelbuild/buildtools" "${BUILDIFIER_VERSION}" "buildifier-${PLATFORM}-${ARCHITECTURE}" "${LOCAL_BIN}/buildifier"
chmod +x "${LOCAL_BIN}/buildifier"
echo "Bazel installation completed successfully"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment