|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Copyright (C) 2023, Ava Labs, Inc. All rights reserved. |
| 3 | +# See the file LICENSE for licensing terms. |
| 4 | + |
| 5 | +set -euo pipefail |
| 6 | + |
| 7 | +# If set to non-empty, prompts the building of a multi-arch image when the image |
| 8 | +# name indicates use of a registry. |
| 9 | +# |
| 10 | +# A registry is required to build a multi-arch image since a multi-arch image is |
| 11 | +# not really an image at all. A multi-arch image (also called a manifest) is |
| 12 | +# basically a list of arch-specific images available from the same registry that |
| 13 | +# hosts the manifest. Manifests are not supported for local images. |
| 14 | +# |
| 15 | +# Reference: https://docs.docker.com/build/building/multi-platform/ |
| 16 | +PLATFORMS="${PLATFORMS:-}" |
| 17 | + |
| 18 | +# If set to non-empty, the image will be published to the registry. |
| 19 | +PUBLISH="${PUBLISH:-}" |
| 20 | + |
| 21 | +# The name of the VM to build. Defaults to build morpheusvm in examples/morpheusvm/ |
| 22 | +VM_NAME=${VM_NAME:-"morpheusvm"} |
| 23 | + |
| 24 | +# Directory above this script |
| 25 | +HYPERSDK_PATH=$( |
| 26 | + cd "$(dirname "${BASH_SOURCE[0]}")" |
| 27 | + cd .. && pwd |
| 28 | +) |
| 29 | +VM_PATH=${VM_PATH:-"${HYPERSDK_PATH}/examples/${VM_NAME}"} |
| 30 | + |
| 31 | +# Load the constants |
| 32 | +source "$HYPERSDK_PATH"/scripts/constants.sh |
| 33 | + |
| 34 | +# WARNING: this will use the most recent commit even if there are un-committed changes present |
| 35 | +BUILD_IMAGE_ID=${BUILD_IMAGE_ID:-"${CURRENT_BRANCH}"} |
| 36 | + |
| 37 | +# buildx (BuildKit) improves the speed and UI of builds over the legacy builder and |
| 38 | +# simplifies creation of multi-arch images. |
| 39 | +# |
| 40 | +# Reference: https://docs.docker.com/build/buildkit/ |
| 41 | +DOCKER_CMD="docker buildx build" |
| 42 | + |
| 43 | +if [[ -n "${PUBLISH}" ]]; then |
| 44 | + DOCKER_CMD="${DOCKER_CMD} --push" |
| 45 | + |
| 46 | + echo "Pushing $DOCKERHUB_REPO:$BUILD_IMAGE_ID" |
| 47 | + |
| 48 | + # A populated DOCKER_USERNAME env var triggers login |
| 49 | + if [[ -n "${DOCKER_USERNAME:-}" ]]; then |
| 50 | + echo "$DOCKER_PASS" | docker login --username "$DOCKER_USERNAME" --password-stdin |
| 51 | + fi |
| 52 | +fi |
| 53 | + |
| 54 | +# Build a multi-arch image if requested |
| 55 | +if [[ -n "${PLATFORMS}" ]]; then |
| 56 | + DOCKER_CMD="${DOCKER_CMD} --platform=${PLATFORMS}" |
| 57 | +fi |
| 58 | + |
| 59 | +VM_ID=${VM_ID:-"${DEFAULT_VM_ID}"} |
| 60 | +if [[ "${VM_ID}" != "${DEFAULT_VM_ID}" ]]; then |
| 61 | + DOCKERHUB_TAG="${VM_ID}-${DOCKERHUB_TAG}" |
| 62 | +fi |
| 63 | + |
| 64 | +# Default to the release image. Will need to be overridden when testing against unreleased versions. |
| 65 | +AVALANCHEGO_NODE_IMAGE="${AVALANCHEGO_NODE_IMAGE:-${AVALANCHEGO_IMAGE_NAME}:${AVALANCHE_DOCKER_VERSION}}" |
| 66 | + |
| 67 | +echo "Building Docker Image: $DOCKERHUB_REPO:$BUILD_IMAGE_ID based off AvalancheGo@$AVALANCHE_DOCKER_VERSION" |
| 68 | +${DOCKER_CMD} -t "$DOCKERHUB_REPO:$BUILD_IMAGE_ID" \ |
| 69 | + "$HYPERSDK_PATH" -f "$HYPERSDK_PATH/Dockerfile" \ |
| 70 | + --build-arg AVALANCHEGO_NODE_IMAGE="$AVALANCHEGO_NODE_IMAGE" \ |
| 71 | + --build-arg VM_COMMIT="$VM_COMMIT" \ |
| 72 | + --build-arg CURRENT_BRANCH="$CURRENT_BRANCH" \ |
| 73 | + --build-arg VM_ID="$VM_ID" \ |
| 74 | + --build-arg VM_NAME="$VM_NAME" |
| 75 | + |
| 76 | +if [[ -n "${PUBLISH}" && $CURRENT_BRANCH == "master" ]]; then |
| 77 | + echo "Tagging current image as $DOCKERHUB_REPO:latest" |
| 78 | + docker buildx imagetools create -t "$DOCKERHUB_REPO:latest" "$DOCKERHUB_REPO:$BUILD_IMAGE_ID" |
| 79 | +fi |
0 commit comments