|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -e |
| 3 | + |
| 4 | +if [[ -z $PYTHON_VERSION ]]; then |
| 5 | + echo "Must set a python version to build." |
| 6 | + exit 1 |
| 7 | +fi |
| 8 | + |
| 9 | +# Version- the python version itself (3.7, 3.8, 3.9.7, etc) |
| 10 | +# Build Target - the upstream container to use during the building stage |
| 11 | +# Publish Target - the upstream container to use for the final stage |
| 12 | + |
| 13 | +# Package Version - |
| 14 | +# Package - |
| 15 | + |
| 16 | +# REPO/image:py3.7-0.2.1 |
| 17 | +# REPO/image:py3.7-slim-0.2.1 |
| 18 | +# REPO/image:py3.7-alpine-0.2.1 |
| 19 | + |
| 20 | +if [[ $TARGET_BASE = "full" ]]; then |
| 21 | + TARGET_BASE="" |
| 22 | +fi |
| 23 | + |
| 24 | +# Set image PUBLISH_TARGET version- ie, 3.9-slim. |
| 25 | +if [[ ! -z $TARGET_BASE ]]; then |
| 26 | + PUBLISH_TARGET=$PYTHON_VERSION-$TARGET_BASE |
| 27 | +else |
| 28 | + PUBLISH_TARGET=$PYTHON_VERSION |
| 29 | +fi |
| 30 | + |
| 31 | +# Set build environment |
| 32 | +if [[ $PUBLISH_TARGET =~ "alpine" ]]; then |
| 33 | + # Alpine is the odd one out- everything else is ubuntu. |
| 34 | + # So the build environment has to also be alpine. |
| 35 | + BUILD_TARGET=$PUBLISH_TARGET |
| 36 | +else |
| 37 | + # Every other image should use the full container for builds. |
| 38 | + BUILD_TARGET=$PYTHON_VERSION |
| 39 | +fi |
| 40 | + |
| 41 | + |
| 42 | +# Image Push location |
| 43 | +REGISTRY=${REGISTRY:-"ghcr.io"} |
| 44 | +IMAGE_REPOSITORY=${IMAGE_NAME:-$GITHUB_REPOSITORY} |
| 45 | +IMAGE_LOCATION=$REGISTRY/$IMAGE_REPOSITORY |
| 46 | + |
| 47 | +TAG="$IMAGE_LOCATION:py${PUBLISH_TARGET}-${PACKAGE_VERSION}" |
| 48 | +TAGS = " -t $TAG" |
| 49 | + |
| 50 | +if [[ ! -z $PACKAGE_LATEST_VERSION ]] && [[ $PACKAGE_LATEST_VERSION = $PACKAGE_VERSION ]]; then |
| 51 | + TAGS+="\n -t $IMAGE_LOCATION:py${PUBLISH_TARGET}-LATEST" |
| 52 | +fi |
| 53 | + |
| 54 | + |
| 55 | +echo Building and pushing $TAG |
| 56 | +echo Python Version: $PYTHON_VERSION |
| 57 | +echo Publish Target: $PUBLISH_TARGET |
| 58 | +echo Build Target: $BUILD_TARGET |
| 59 | + |
| 60 | +# Create or Reuse builder builder |
| 61 | +docker buildx use multiarch || docker buildx create --name multiarch --use |
| 62 | + |
| 63 | +# Display docker command for debugging |
| 64 | +echo docker buildx build \ |
| 65 | + --platform $PLATFORM \ |
| 66 | + $TAGS \ |
| 67 | + --build-arg python_version=$PYTHON_VERSION \ |
| 68 | + --build-arg publish_target=$PUBLISH_TARGET \ |
| 69 | + --build-arg build_target=$BUILD_TARGET \ |
| 70 | + --build-arg packages=$PACKAGE \ |
| 71 | + --build-arg package_version=$PACKAGE_VERSION \ |
| 72 | + --build-arg maintainer=$MAINTAINER \ |
| 73 | + -f ${DOCKERFILE_LOCATION:-./dockerfile} \ |
| 74 | + --push \ |
| 75 | + ${DOCKER_CONTEXT:-.} |
| 76 | + |
| 77 | + |
| 78 | +docker buildx build \ |
| 79 | + --platform $PLATFORM \ |
| 80 | + $TAGS \ |
| 81 | + --build-arg python_version=$PYTHON_VERSION \ |
| 82 | + --build-arg publish_target=$PUBLISH_TARGET \ |
| 83 | + --build-arg build_target=$BUILD_TARGET \ |
| 84 | + --build-arg packages=$PACKAGE \ |
| 85 | + --build-arg package_version=$PACKAGE_VERSION \ |
| 86 | + --build-arg maintainer=$MAINTAINER \ |
| 87 | + -f ${DOCKERFILE_LOCATION:-./dockerfile} \ |
| 88 | + --push \ |
| 89 | + ${DOCKER_CONTEXT:-.} |
0 commit comments