github.com/MetalBlockchain/metalgo@v1.11.9/scripts/build_image.sh (about)

     1  #!/usr/bin/env bash
     2  
     3  set -euo pipefail
     4  
     5  # e.g.,
     6  # ./scripts/build_image.sh                                           # Build local single-arch image
     7  # DOCKER_IMAGE=mymetalgo ./scripts/build_image.sh                # Build local single arch image with a custom image name
     8  # DOCKER_IMAGE=MetalBlockchain/metalgo ./scripts/build_image.sh      # Build and push multi-arch image to docker hub
     9  # DOCKER_IMAGE=localhost:5001/metalgo ./scripts/build_image.sh   # Build and push multi-arch image to private registry
    10  # DOCKER_IMAGE=localhost:5001/mymetalgo ./scripts/build_image.sh # Build and push multi-arch image to private registry with a custom image name
    11  
    12  # Multi-arch builds require Docker Buildx and QEMU. buildx should be enabled by
    13  # default in the verson of docker included with Ubuntu 22.04, and qemu can be
    14  # installed as follows:
    15  #
    16  #  sudo apt-get install qemu qemu-user-static
    17  #
    18  # After installing qemu, it will also be necessary to start a new builder that can
    19  # support multiplatform builds:
    20  #
    21  #  docker buildx create --use
    22  #
    23  # Reference: https://docs.docker.com/buildx/working-with-buildx/
    24  
    25  # Directory above this script
    26  METAL_PATH=$( cd "$( dirname "${BASH_SOURCE[0]}" )"; cd .. && pwd )
    27  
    28  # Load the constants
    29  source "$METAL_PATH"/scripts/constants.sh
    30  
    31  if [[ $image_tag == *"-race" ]]; then
    32    echo "Branch name must not end in '-race'"
    33    exit 1
    34  fi
    35  
    36  # The published name should be 'avaplatform/avalanchego', but to avoid unintentional
    37  # pushes it is defaulted to 'avalanchego' (without a repo or registry name) which can
    38  # only be used to create local images.
    39  DOCKER_IMAGE=${DOCKER_IMAGE:-"metalgo"}
    40  
    41  # buildx (BuildKit) improves the speed and UI of builds over the legacy builder and
    42  # simplifies creation of multi-arch images.
    43  #
    44  # Reference: https://docs.docker.com/build/buildkit/
    45  DOCKER_CMD="docker buildx build"
    46  
    47  # The dockerfile doesn't specify the golang version to minimize the
    48  # changes required to bump the version. Instead, the golang version is
    49  # provided as an argument.
    50  GO_VERSION="$(go list -m -f '{{.GoVersion}}')"
    51  DOCKER_CMD="${DOCKER_CMD} --build-arg GO_VERSION=${GO_VERSION}"
    52  
    53  if [[ "${DOCKER_IMAGE}" == *"/"* ]]; then
    54    # Build a multi-arch image since the image name includes a slash which indicates
    55    # the use of a registry e.g.
    56    #
    57    #  - dockerhub: [repo]/[image name]:[tag]
    58    #  - private registry: [private registry hostname]/[image name]:[tag]
    59    #
    60    # A registry is required to build a multi-arch image since a multi-arch image is
    61    # not really an image at all. A multi-arch image (also called a manifest) is
    62    # basically a list of arch-specific images available from the same registry that
    63    # hosts the manifest. Manifests are not supported for local images.
    64    #
    65    # Reference: https://docs.docker.com/build/building/multi-platform/
    66    PLATFORMS="${PLATFORMS:-linux/amd64,linux/arm64}"
    67    DOCKER_CMD="${DOCKER_CMD} --push --platform=${PLATFORMS}"
    68  
    69    # A populated DOCKER_USERNAME env var triggers login
    70    if [[ -n "${DOCKER_USERNAME:-}" ]]; then
    71      echo "$DOCKER_PASS" | docker login --username "$DOCKER_USERNAME" --password-stdin
    72    fi
    73  else
    74    # Build a single-arch image since the image name does not include a slash which
    75    # indicates that a registry is not available.
    76    #
    77    # Building a single-arch image with buildx and having the resulting image show up
    78    # in the local store of docker images (ala 'docker build') requires explicitly
    79    # loading it from the buildx store with '--load'.
    80    DOCKER_CMD="${DOCKER_CMD} --load"
    81  fi
    82  
    83  echo "Building Docker Image with tags: $DOCKER_IMAGE:$commit_hash , $DOCKER_IMAGE:$image_tag"
    84  ${DOCKER_CMD} -t "$DOCKER_IMAGE:$commit_hash" -t "$DOCKER_IMAGE:$image_tag" \
    85                "$METAL_PATH" -f "$METAL_PATH/Dockerfile"
    86  
    87  echo "Building Docker Image with tags: $DOCKER_IMAGE:$commit_hash-race , $DOCKER_IMAGE:$image_tag-race"
    88  ${DOCKER_CMD} --build-arg="RACE_FLAG=-r" -t "$DOCKER_IMAGE:$commit_hash-race" -t "$DOCKER_IMAGE:$image_tag-race" \
    89                "$METAL_PATH" -f "$METAL_PATH/Dockerfile"
    90  
    91  # Only tag the latest image for the master branch when images are pushed to a registry
    92  if [[ "${DOCKER_IMAGE}" == *"/"* && $image_tag == "master" ]]; then
    93    echo "Tagging current metalgo images as $DOCKER_IMAGE:latest"
    94    docker buildx imagetools create -t "$DOCKER_IMAGE:latest" "$DOCKER_IMAGE:$commit_hash"
    95  fi