Skip to content

Commit

Permalink
[docker] Enable image builds from a git worktree
Browse files Browse the repository at this point in the history
This change enables docker image builds from [git
worktrees](https://git-scm.com/docs/git-worktree). This required
discoverying the git commit used to configure the avalanchego build
script in advance of image build and providing it as an argument to
image build.

Previously, it was impossible to build docker images from a git
worktree:

 - the avalanchego build script needed to discover the git commit to
 configure the binary with for traceability

 - the build script assumed a .git directory to discover the git
 commit

 - the dockerfile copied the working tree into the image prior to
 running the avalanchego build script

 - if the working tree was for a git worktree, the resulting copy
 wouldn't have a .git directory to discovery the git commit from

  - instead of a regular .git directory, a non-primary worktree has a
  .git file referencing a path in the primary worktree's .git
  directory.
  • Loading branch information
maru-ava committed Jan 19, 2025
1 parent 0e994f6 commit f32daa7
Show file tree
Hide file tree
Showing 12 changed files with 53 additions and 21 deletions.
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ RUN [ -d ./build ] && rm -rf ./build/* || true
# enabling cross-compilation.
ARG RACE_FLAG=""
ARG BUILD_SCRIPT=build.sh
ARG AVALANCHEGO_COMMIT=""
RUN . ./build_env.sh && \
echo "{CC=$CC, TARGETPLATFORM=$TARGETPLATFORM, BUILDPLATFORM=$BUILDPLATFORM}" && \
export GOARCH=$(echo ${TARGETPLATFORM} | cut -d / -f2) && \
export AVALANCHEGO_COMMIT="${AVALANCHEGO_COMMIT}" && \
./scripts/${BUILD_SCRIPT} ${RACE_FLAG}

# Create this directory in the builder to avoid requiring anything to be executed in the
Expand Down
6 changes: 4 additions & 2 deletions scripts/build_antithesis_images.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ fi
# Directory above this script
AVALANCHE_PATH=$( cd "$( dirname "${BASH_SOURCE[0]}" )"; cd .. && pwd )

source "${AVALANCHE_PATH}"/scripts/constants.sh
source "${AVALANCHE_PATH}"/scripts/git_commit.sh

# Import common functions used to build images for antithesis test setups
source "${AVALANCHE_PATH}"/scripts/lib_build_antithesis_images.sh

Expand All @@ -28,7 +31,6 @@ IMAGE_PREFIX="${IMAGE_PREFIX:-}"
IMAGE_TAG="${IMAGE_TAG:-}"
if [[ -z "${IMAGE_TAG}" ]]; then
# Default to tagging with the commit hash
source "${AVALANCHE_PATH}"/scripts/constants.sh
IMAGE_TAG="${commit_hash}"
fi

Expand Down Expand Up @@ -56,7 +58,7 @@ function build_antithesis_images_for_avalanchego {
fi
build_antithesis_images "${GO_VERSION}" "${image_prefix}" "antithesis-${test_setup}" "${IMAGE_TAG}" "${IMAGE_TAG}" \
"${AVALANCHE_PATH}/tests/antithesis/${test_setup}/Dockerfile" "${uninstrumented_node_dockerfile}" \
"${AVALANCHE_PATH}" "${node_only}"
"${AVALANCHE_PATH}" "${node_only}" "${git_commit}"
}

if [[ "${TEST_SETUP}" == "avalanchego" ]]; then
Expand Down
1 change: 1 addition & 0 deletions scripts/build_avalanche.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ done
AVALANCHE_PATH=$( cd "$( dirname "${BASH_SOURCE[0]}" )"; cd .. && pwd )
# Load the constants
source "$AVALANCHE_PATH"/scripts/constants.sh
source "$AVALANCHE_PATH"/scripts/git_commit.sh

build_args="$race"
echo "Building AvalancheGo..."
Expand Down
1 change: 1 addition & 0 deletions scripts/build_bootstrap_monitor.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ set -euo pipefail
AVALANCHE_PATH=$( cd "$( dirname "${BASH_SOURCE[0]}" )"; cd .. && pwd )
# Load the constants
source "$AVALANCHE_PATH"/scripts/constants.sh
source "$AVALANCHE_PATH"/scripts/git_commit.sh

echo "Building bootstrap-monitor..."
go build -ldflags\
Expand Down
9 changes: 9 additions & 0 deletions scripts/build_image.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ FORCE_TAG_LATEST="${FORCE_TAG_LATEST:-}"

# Load the constants
source "$AVALANCHE_PATH"/scripts/constants.sh
source "$AVALANCHE_PATH"/scripts/git_commit.sh

image_tag="$("${AVALANCHE_PATH}"/scripts/get_image_tag.sh)"

if [[ -z "${SKIP_BUILD_RACE}" && $image_tag == *"-r" ]]; then
echo "Branch name must not end in '-r'"
Expand Down Expand Up @@ -69,6 +72,12 @@ DOCKER_CMD="docker buildx build ${*}"
GO_VERSION="$(go list -m -f '{{.GoVersion}}')"
DOCKER_CMD="${DOCKER_CMD} --build-arg GO_VERSION=${GO_VERSION}"

# Provide the git commit as a build argument to avoid requiring this
# to be discovered within the image. This enables image builds from
# git worktrees since a non-primary worktree won't have a .git
# directory to copy into the image.
DOCKER_CMD="${DOCKER_CMD} --build-arg AVALANCHEGO_COMMIT=${git_commit}"

if [[ "${DOCKER_IMAGE}" == *"/"* ]]; then
# Default to pushing when the image name includes a slash which indicates the
# use of a registry e.g.
Expand Down
1 change: 1 addition & 0 deletions scripts/build_tmpnetctl.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ set -euo pipefail
AVALANCHE_PATH=$( cd "$( dirname "${BASH_SOURCE[0]}" )"; cd .. && pwd )
# Load the constants
source "$AVALANCHE_PATH"/scripts/constants.sh
source "$AVALANCHE_PATH"/scripts/git_commit.sh

echo "Building tmpnetctl..."
go build -ldflags\
Expand Down
18 changes: 0 additions & 18 deletions scripts/constants.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,6 @@ AVALANCHE_PATH=$( cd "$( dirname "${BASH_SOURCE[0]}" )"; cd .. && pwd ) # Direct
# Where AvalancheGo binary goes
avalanchego_path="$AVALANCHE_PATH/build/avalanchego"

# Image tag based on current branch (shared between image build and its test script)
# TODO: fix "fatal: No names found, cannot describe anything" in github CI
image_tag=$(git symbolic-ref -q --short HEAD || git describe --tags --exact-match || true)
if [[ -z $image_tag ]]; then
# Supply a default tag when one is not discovered
image_tag=ci_dummy
elif [[ "$image_tag" == */* ]]; then
# Slashes are not legal for docker image tags - replace with dashes
image_tag=$(echo "$image_tag" | tr '/' '-')
fi

# Current commit (shared between image build and its test script)
# WARNING: this will use the most recent commit even if there are un-committed changes present
full_commit_hash="$(git --git-dir="$AVALANCHE_PATH/.git" rev-parse HEAD)"
commit_hash="${full_commit_hash::8}"

git_commit=${AVALANCHEGO_COMMIT:-$( git rev-list -1 HEAD )}

# Static compilation
static_ld_flags=''
if [ "${STATIC_COMPILATION:-}" = 1 ]
Expand Down
15 changes: 15 additions & 0 deletions scripts/get_image_tag.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env bash

set -euo pipefail

# Emits an image tag derived from the current branch or tag

IMAGE_TAG="$( git symbolic-ref -q --short HEAD || git describe --tags --exact-match || true )"
if [[ -z "${IMAGE_TAG}" ]]; then
# Supply a default tag when one is not discovered
IMAGE_TAG=ci_dummy
elif [[ "${IMAGE_TAG}" == */* ]]; then
# Slashes are not legal for docker image tags - replace with dashes
IMAGE_TAG="$( echo "${IMAGE_TAG}" | tr '/' '-' )"
fi
echo "${IMAGE_TAG}"
12 changes: 12 additions & 0 deletions scripts/git_commit.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bash

# Ignore warnings about variables appearing unused since this file is not the consumer of the variables it defines.
# shellcheck disable=SC2034

set -euo pipefail

AVALANCHE_PATH=$( cd "$( dirname "${BASH_SOURCE[0]}" )"; cd .. && pwd ) # Directory above this script

# WARNING: this will use the most recent commit even if there are un-committed changes present
git_commit="${AVALANCHEGO_COMMIT:-$(git --git-dir="${AVALANCHE_PATH}/.git" rev-parse HEAD)}"
commit_hash="${git_commit::8}"
4 changes: 4 additions & 0 deletions scripts/lib_build_antithesis_images.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ function build_antithesis_images {
local uninstrumented_node_dockerfile=$7
local target_path=$8
local node_only=${9:-}
local avalanchego_commit=${10:-}

# Define image names
if [[ -n "${image_prefix}" ]]; then
Expand All @@ -65,6 +66,9 @@ function build_antithesis_images {
--build-arg GO_VERSION=${go_version}\
--build-arg BUILDER_IMAGE_TAG=${image_tag}\
--build-arg BUILDER_WORKDIR=${builder_workdir}"
if [[ -n "${avalanchego_commit:-}" ]]; then
docker_cmd="${docker_cmd} --build-arg AVALANCHEGO_COMMIT=${avalanchego_commit}"
fi

# By default the node image is intended to be local-only.
AVALANCHEGO_NODE_IMAGE="antithesis-avalanchego-node:${node_image_tag}"
Expand Down
2 changes: 1 addition & 1 deletion scripts/tests.build_antithesis_images.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ set -euo pipefail
AVALANCHE_PATH=$( cd "$( dirname "${BASH_SOURCE[0]}" )"; cd .. && pwd )

# Discover the default tag that will be used for the image
source "${AVALANCHE_PATH}"/scripts/constants.sh
source "${AVALANCHE_PATH}"/scripts/git_commit.sh
export IMAGE_TAG="${commit_hash}"

# Build the images for the specified test setup
Expand Down
3 changes: 3 additions & 0 deletions scripts/tests.build_image.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ set -euo pipefail
AVALANCHE_PATH=$( cd "$( dirname "${BASH_SOURCE[0]}" )"; cd .. && pwd )

source "$AVALANCHE_PATH"/scripts/constants.sh
source "$AVALANCHE_PATH"/scripts/git_commit.sh

image_tag="$("${AVALANCHE_PATH}"/scripts/get_image_tag.sh)"

build_and_test() {
local image_name=$1
Expand Down

0 comments on commit f32daa7

Please sign in to comment.