|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Calculate the current build locations |
| 4 | +export SCRIPTDIR=$(dirname "${BASH_SOURCE[0]}") |
| 5 | +export ROOTDIR=${SCRIPTDIR}/.. |
| 6 | + |
| 7 | +# If running on travis and processing a tag, work with curlbuildimages. |
| 8 | +# If running on travis and processing anything else, work with |
| 9 | +# curlbuildimagestemp. |
| 10 | +# If running locally, work with the curlbuildimagestemp namespace. |
| 11 | +if [[ -n "${TRAVIS:-}" ]] |
| 12 | +then |
| 13 | + |
| 14 | + # Check to see if we can log into Docker. Pull requests cannot log into Docker |
| 15 | + if [[ -n "${DOCKER_USER:-}" ]] |
| 16 | + then |
| 17 | + # Log into Docker. |
| 18 | + echo "${DOCKER_PASS}" | docker login -u "${DOCKER_USER}" --password-stdin |
| 19 | + fi |
| 20 | + |
| 21 | + if [[ -n "${TRAVIS_TAG:-}" ]] |
| 22 | + then |
| 23 | + # Processing a tagged build. Get the version from the version file. |
| 24 | + export VERSION=$(cat VERSION) |
| 25 | + |
| 26 | + # Use the main repository. |
| 27 | + export DOCKER_REPO=curlbuildimages |
| 28 | + else |
| 29 | + # Processing an untagged build. The version is the build ID, which is |
| 30 | + # shared between stages. |
| 31 | + export VERSION=${TRAVIS_BUILD_ID} |
| 32 | + |
| 33 | + # Use the temporary repository. |
| 34 | + export DOCKER_REPO=curlbuildimagestemp |
| 35 | + fi |
| 36 | +else |
| 37 | + # Get the version from the version file. |
| 38 | + export VERSION=$(cat VERSION) |
| 39 | + |
| 40 | + # Use the temporary repository. |
| 41 | + export DOCKER_REPO=curlbuildimagestemp |
| 42 | +fi |
| 43 | + |
| 44 | + |
| 45 | +# Simple function which uses docker to build images. |
| 46 | +build_image() |
| 47 | +{ |
| 48 | + TAG=$1 |
| 49 | + BASE_IMAGE=$2 |
| 50 | + DOCKER_SCRIPT=$3 |
| 51 | + |
| 52 | + echo "Building ${TAG}:${VERSION} from ${BASE_IMAGE}" |
| 53 | + |
| 54 | + docker build --build-arg BASE_IMAGE=${BASE_IMAGE} \ |
| 55 | + --build-arg DOCKER_SCRIPT=${DOCKER_SCRIPT} \ |
| 56 | + -t ${DOCKER_REPO}/${TAG}:${VERSION} \ |
| 57 | + . |
| 58 | +} |
| 59 | + |
| 60 | +# Simple wrapper function which adds the REPOSITORY and VERSION to the given |
| 61 | +# BASE_IMAGE, then builds. |
| 62 | +build_image_versioned() |
| 63 | +{ |
| 64 | + TAG=$1 |
| 65 | + BASE_IMAGE=$2 |
| 66 | + DOCKER_SCRIPT=$3 |
| 67 | + |
| 68 | + build_image ${TAG} ${DOCKER_REPO}/${BASE_IMAGE}:${VERSION} ${DOCKER_SCRIPT} |
| 69 | +} |
| 70 | + |
| 71 | + |
| 72 | +# Simple function which adds the REPOSITORY and VERSION to the given TAG, |
| 73 | +# then pushes to Docker. |
| 74 | +push_image_versioned() |
| 75 | +{ |
| 76 | + if [[ -n "${DOCKER_USER:-}" ]] |
| 77 | + then |
| 78 | + # The Docker user environment variable exists, so pushing should work. |
| 79 | + TAG=$1 |
| 80 | + docker push ${DOCKER_REPO}/${TAG}:${VERSION} |
| 81 | + else |
| 82 | + echo "Would have pushed to ${DOCKER_REPO}/${TAG}:${VERSION} but " |
| 83 | + echo "DOCKER_USER is not set." |
| 84 | + fi |
| 85 | +} |
| 86 | + |
| 87 | + |
| 88 | +# Function to remove an image tag from the Docker hub |
| 89 | +remove_image_versioned() |
| 90 | +{ |
| 91 | + python3 ${ROOTDIR}/docker_hub_scripts/remove_tag.py \ |
| 92 | + --username ${DOCKER_USER} \ |
| 93 | + --password ${DOCKER_PASS} \ |
| 94 | + --repository ${1} \ |
| 95 | + --tag ${VERSION} |
| 96 | +} |
0 commit comments