Skip to content

Commit

Permalink
fixup: Ensure bootstrap tester image build is multiplatform
Browse files Browse the repository at this point in the history
This supports building an image on a mac that will work in an amd64
eks cluster.
  • Loading branch information
marun committed Jul 25, 2024
1 parent e5b9a78 commit f79f576
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 9 deletions.
19 changes: 12 additions & 7 deletions scripts/build_bootstrap_tester_image.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,20 @@ fi
# Build the avalanchego image
DOCKER_CMD="docker buildx build"

# TODO(marun) Figure out how best to support this in CI and locally
PLATFORMS="${PLATFORMS:-}"

# Build the avalanchego image local-only
PLATFORMS="${PLATFORMS}" IMAGE_PREFIX= ./scripts/build_image.sh
AVALANCHEGO_NODE_IMAGE="avalanchego:${IMAGE_TAG}"

# Specifying an image prefix will ensure the image is pushed after build
IMAGE_PREFIX="${IMAGE_PREFIX:-}"
if [[ -n "${IMAGE_PREFIX}" ]]; then
IMAGE_NAME="${IMAGE_PREFIX}/${IMAGE_NAME}"
if [[ -n "${PLATFORMS}" ]]; then
DOCKER_CMD="${DOCKER_CMD} --platform=${PLATFORMS}"
fi
DOCKER_CMD="${DOCKER_CMD} --push"

# Tag the image as latest for the master branch
Expand All @@ -33,19 +43,14 @@ if [[ -n "${IMAGE_PREFIX}" ]]; then
if [[ -n "${DOCKER_USERNAME:-}" ]]; then
echo "$DOCKER_PASS" | docker login --username "$DOCKER_USERNAME" --password-stdin
fi

# The avalanchego image will have already have been built
AVALANCHEGO_NODE_IMAGE="${IMAGE_PREFIX}/avalanchego:${IMAGE_TAG}"
else
# Build the avalanchego image locally
./scripts/build_image.sh
AVALANCHEGO_NODE_IMAGE="avalanchego:${IMAGE_TAG}"
fi

# The dockerfiles don't specify the golang version to minimize the changes required to bump
# the version. Instead, the golang version is provided as an argument.
GO_VERSION="$(go list -m -f '{{.GoVersion}}')"

PLATFORMS="${PLATFORMS:-linux/amd64,linux/arm64}"

# Build the image for the bootstrap tester
${DOCKER_CMD} -t "${IMAGE_NAME}:${IMAGE_TAG}" \
--build-arg GO_VERSION="${GO_VERSION}" --build-arg AVALANCHEGO_NODE_IMAGE="${AVALANCHEGO_NODE_IMAGE}" \
Expand Down
5 changes: 5 additions & 0 deletions scripts/build_image.sh
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ if [[ "${DOCKER_IMAGE}" == *"/"* ]]; then
echo "$DOCKER_PASS" | docker login --username "$DOCKER_USERNAME" --password-stdin
fi
else
PLATFORMS="${PLATFORMS:-}"
if [[ -n "${PLATFORMS}" ]]; then
DOCKER_CMD="${DOCKER_CMD} --platform=${PLATFORMS}"
fi

# Build a single-arch image since the image name does not include a slash which
# indicates that a registry is not available.
#
Expand Down
27 changes: 25 additions & 2 deletions tests/bootstrap/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,30 @@ ARG GO_VERSION
# AVALANCHEGO_NODE_IMAGE needs to identify an existing node image and should include the tag
ARG AVALANCHEGO_NODE_IMAGE

FROM golang:$GO_VERSION-bullseye AS builder
# ============= Compilation Stage ================
# Always use the native platform to ensure fast builds
FROM --platform=$BUILDPLATFORM golang:$GO_VERSION-bullseye AS builder

WORKDIR /builder_workdir

ARG TARGETPLATFORM
ARG BUILDPLATFORM

# Configure a cross-compiler if the target platform differs from the build platform.
#
# build_env.sh is used to capture the environmental changes required by the build step since RUN
# environment state is not otherwise persistent.
# TODO(marun) Extract a common multi-platform builder from this and the avalanchego Dockerfile.
RUN if [ "$TARGETPLATFORM" = "linux/arm64" ] && [ "$BUILDPLATFORM" != "linux/arm64" ]; then \
apt-get update && apt-get install -y gcc-aarch64-linux-gnu && \
echo "export CC=aarch64-linux-gnu-gcc" > ./build_env.sh \
; elif [ "$TARGETPLATFORM" = "linux/amd64" ] && [ "$BUILDPLATFORM" != "linux/amd64" ]; then \
apt-get update && apt-get install -y gcc-x86-64-linux-gnu && \
echo "export CC=x86_64-linux-gnu-gcc" > ./build_env.sh \
; else \
echo "export CC=gcc" > ./build_env.sh \
; fi

# Copy and download avalanche dependencies using go mod
COPY go.mod .
COPY go.sum .
Expand All @@ -21,7 +41,10 @@ COPY . .
RUN [ -d ./build ] && rm -rf ./build/* || true

# Build tester binary
RUN ./scripts/build_bootstrap_tester.sh
RUN . ./build_env.sh && \
echo "{CC=$CC, TARGETPLATFORM=$TARGETPLATFORM, BUILDPLATFORM=$BUILDPLATFORM}" && \
export GOARCH=$(echo ${TARGETPLATFORM} | cut -d / -f2) && \
./scripts/build_bootstrap_tester.sh

# ============= Cleanup Stage ================
FROM $AVALANCHEGO_NODE_IMAGE AS execution
Expand Down

0 comments on commit f79f576

Please sign in to comment.