Jessica Wagantall | d9c235c | 2018-03-21 13:03:59 -0700 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # Ensure we fail the job if any steps fail |
| 3 | # Do not set -u as DOCKER_ARGS may be unbound |
| 4 | set -e -o pipefail |
| 5 | |
| 6 | FULL_DATE=`date +'%Y%m%dT%H%M%S'` |
Jessica Wagantall | fd057a0 | 2018-04-03 15:33:18 -0700 | [diff] [blame] | 7 | IMAGE_VERSION=`xmlstarlet sel -N "x=http://maven.apache.org/POM/4.0.0" -t -v "/x:project/x:version" pom.xml | cut -c1-5` |
Jessica Wagantall | d9c235c | 2018-03-21 13:03:59 -0700 | [diff] [blame] | 8 | |
Denes Nemeth | bb2815b | 2018-05-15 22:12:54 +0200 | [diff] [blame] | 9 | case "$BUILD_MODE" in |
| 10 | "STAGING") |
Denes Nemeth | 801d2a6 | 2018-05-12 09:40:20 +0200 | [diff] [blame] | 11 | DOCKER_TAG="$IMAGE_VERSION"-STAGING-"$FULL_DATE"Z |
| 12 | DOCKER_LATEST_TAG="$IMAGE_VERSION"-STAGING-latest |
| 13 | echo "Using tags $DOCKER_TAG and $DOCKER_LATEST_TAG" |
Jessica Wagantall | d9c235c | 2018-03-21 13:03:59 -0700 | [diff] [blame] | 14 | ;; |
Denes Nemeth | bb2815b | 2018-05-15 22:12:54 +0200 | [diff] [blame] | 15 | "SNAPSHOT") |
Denes Nemeth | 801d2a6 | 2018-05-12 09:40:20 +0200 | [diff] [blame] | 16 | DOCKER_TAG="$IMAGE_VERSION"-SNAPSHOT-"$FULL_DATE"Z |
| 17 | DOCKER_LATEST_TAG="$IMAGE_VERSION"-SNAPSHOT-latest |
| 18 | echo "Using tags $DOCKER_TAG and $DOCKER_LATEST_TAG" |
Jessica Wagantall | d9c235c | 2018-03-21 13:03:59 -0700 | [diff] [blame] | 19 | ;; |
| 20 | esac |
| 21 | |
| 22 | # Switch to the directory where the Dockerfile is |
| 23 | cd "$DOCKER_ROOT" |
| 24 | |
| 25 | # DOCKERREGISTRY is purposely not using an '_' so as to not conflict with the |
| 26 | # Jenkins global env var of the DOCKER_REGISTRY which the docker-login step uses |
| 27 | IMAGE_NAME="$DOCKERREGISTRY/$DOCKER_NAME:$DOCKER_TAG" |
Denes Nemeth | 801d2a6 | 2018-05-12 09:40:20 +0200 | [diff] [blame] | 28 | IMAGE_NAME_LATEST="$DOCKERREGISTRY/$DOCKER_NAME:$DOCKER_LATEST_TAG" |
Jessica Wagantall | d9c235c | 2018-03-21 13:03:59 -0700 | [diff] [blame] | 29 | |
| 30 | # Build the docker image |
| 31 | |
| 32 | # Allow word splitting |
| 33 | # shellcheck disable=SC2086 |
Denes Nemeth | 801d2a6 | 2018-05-12 09:40:20 +0200 | [diff] [blame] | 34 | docker build $DOCKER_ARGS . -t $IMAGE_NAME -t $IMAGE_NAME_LATEST | tee "$WORKSPACE/docker_build_log.txt" |
Jessica Wagantall | d9c235c | 2018-03-21 13:03:59 -0700 | [diff] [blame] | 35 | |
| 36 | # Write DOCKER_IMAGE information to a file so it can be injected into the |
| 37 | # environment for following steps |
| 38 | echo "DOCKER_IMAGE=$IMAGE_NAME" >> "$WORKSPACE/env_inject.txt" |
Denes Nemeth | 801d2a6 | 2018-05-12 09:40:20 +0200 | [diff] [blame] | 39 | echo "DOCKER_IMAGE_LATEST=$IMAGE_NAME_LATEST" >> "$WORKSPACE/env_inject.txt" |
| 40 | |
Jessica Wagantall | d9c235c | 2018-03-21 13:03:59 -0700 | [diff] [blame] | 41 | |