1#!/usr/bin/env bash 2set -e 3 4function error 5{ 6 echo >&2 "ERROR: ${1} Aborting." && false 7} 8 9function warning 10{ 11 echo >&2 "WARNING: ${1}" 12} 13 14[[ $# == 3 ]] || error "Expected exactly 3 parameters: '${0} <IMAGE_NAME> <IMAGE_VARIANT> <DOCKER_REPOSITORY>'." 15 16IMAGE_NAME="${1}" 17IMAGE_VARIANT="${2}" 18DOCKER_REPOSITORY="${3}" 19DOCKERFILE="scripts/docker/${IMAGE_NAME}/Dockerfile.${IMAGE_VARIANT}" 20 21echo "-- check_dockerfile_was_changed" 22 23# exit, if the dockerfile was not changed. 24if git diff --quiet origin/develop HEAD -- "${DOCKERFILE}"; then 25 echo "${DOCKERFILE} was not changed. Nothing to do." 26 exit 0 27fi 28 29echo "-- check_version" 30 31PREV_VERSION=$(git diff origin/develop HEAD -- "${DOCKERFILE}" | grep -e '^\s*-LABEL\s\+version=".*"\s*$' | awk -F'"' '{ print $2 }') 32NEXT_VERSION=$(git diff origin/develop HEAD -- "${DOCKERFILE}" | grep -e '^\s*+LABEL\s\+version=".*"\s*$' | awk -F'"' '{ print $2 }') 33 34[[ $NEXT_VERSION != "" ]] || error "No version label defined in Dockerfile. You may need to add 'LABEL version' in '${DOCKERFILE}'." 35 36[[ $PREV_VERSION != "" ]] || { 37 warning "no previous version found. Will set \$PREV_VERSION = 0." 38 PREV_VERSION=0 39} 40 41if [[ $((PREV_VERSION + 1)) != $((NEXT_VERSION)) ]]; then 42 error "Version label in Dockerfile was not incremented. You may need to change 'LABEL version' in '${DOCKERFILE}'." 43fi 44 45echo "-- build_docker" 46 47# This is a workaround: we run `docker build` twice to prevent the `layer does not exist` problem. 48# See https://github.com/moby/moby/issues/37965. 49docker build "scripts/docker/${IMAGE_NAME}" --file "scripts/docker/${IMAGE_NAME}/Dockerfile.${IMAGE_VARIANT}" --tag "${IMAGE_NAME}" || 50 docker build "scripts/docker/${IMAGE_NAME}" --file "scripts/docker/${IMAGE_NAME}/Dockerfile.${IMAGE_VARIANT}" --tag "${IMAGE_NAME}" 51 52echo "-- test_docker @ '${PWD}'" 53 54docker run --rm --volume "${PWD}:/root/project" "${IMAGE_NAME}" "/root/project/scripts/ci/${IMAGE_NAME}_test_${IMAGE_VARIANT}.sh" 55 56echo "-- push_docker" 57 58VERSION=$(docker inspect --format='{{.Config.Labels.version}}' "${IMAGE_NAME}") 59DOCKER_IMAGE_ID="${DOCKER_REPOSITORY}:${IMAGE_VARIANT}" 60 61docker tag "${IMAGE_NAME}" "${DOCKER_IMAGE_ID}-${VERSION}" 62docker push "${DOCKER_IMAGE_ID}-${VERSION}" 63 64REPO_DIGEST=$(docker inspect --format='{{.RepoDigests}}' "${DOCKER_IMAGE_ID}-${VERSION}") 65 66echo "DOCKER_IMAGE=${DOCKER_IMAGE_ID}-${VERSION}" >> "$GITHUB_ENV" 67echo "DOCKER_REPO_DIGEST=${REPO_DIGEST}" >> "$GITHUB_ENV" 68