diff --git a/.travis.yml b/.travis.yml index d9694b4..513e391 100644 --- a/.travis.yml +++ b/.travis.yml @@ -26,6 +26,7 @@ jobs: if: branch = master AND NOT type IN (pull_request) install: skip script: travis/release.sh + post_script: travis/delete-dockerhub-images.sh import: - docs/.travis.yml diff --git a/travis/acceptanceTest.sh b/travis/acceptanceTest.sh index a0f02b5..0d19da6 100755 --- a/travis/acceptanceTest.sh +++ b/travis/acceptanceTest.sh @@ -3,12 +3,16 @@ # fail fast settings from https://dougrichardson.org/2018/08/03/fail-fast-bash-scripting.html set -euov pipefail +# obtain current repository name +REPO_LOCAL_PATH=`git rev-parse --show-toplevel` +REPO_NAME=`basename $REPO_LOCAL_PATH` + # Check presence of environment variables TRAVIS_BUILD_NUMBER="${TRAVIS_BUILD_NUMBER:-0}" buildTag=travis_$TRAVIS_BUILD_NUMBER # We use a temporary build number for tagging, since this is a transient artefact -docker run --rm -d -p 8080:7000 --name template-svc eoepca/template-service:${buildTag} # Runs container from EOEPCA repository +docker run --rm -d -p 8080:7000 --name ${REPO_NAME} eoepca/${REPO_NAME}:${buildTag} # Runs container from EOEPCA repository sleep 15 # wait until the container is running diff --git a/travis/containerCreation.sh b/travis/containerCreation.sh index 67b5b98..9278c28 100755 --- a/travis/containerCreation.sh +++ b/travis/containerCreation.sh @@ -3,18 +3,21 @@ # fail fast settings from https://dougrichardson.org/2018/08/03/fail-fast-bash-scripting.html set -euov pipefail +# obtain current repository name +REPO_LOCAL_PATH=`git rev-parse --show-toplevel` +REPO_NAME=`basename $REPO_LOCAL_PATH` + ./gradlew shadowJar # Check presence of environment variables TRAVIS_BUILD_NUMBER="${TRAVIS_BUILD_NUMBER:-0}" - # Create a Docker image and tag it as 'travis_' buildTag=travis_$TRAVIS_BUILD_NUMBER # We use a temporary build number for tagging, since this is a transient artefact -docker build -t eoepca/template-service . -docker tag eoepca/template-service eoepca/template-service:$buildTag # Tags container in EOEPCA repository with buildTag +docker build -t eoepca/${REPO_NAME} . +docker tag eoepca/${REPO_NAME} eoepca/${REPO_NAME}:$buildTag # Tags container in EOEPCA repository with buildTag echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin -docker push eoepca/template-service:$buildTag # defaults to docker hub EOEPCA repository +docker push eoepca/${REPO_NAME}:$buildTag # defaults to docker hub EOEPCA repository diff --git a/travis/delete-dockerhub-images.sh b/travis/delete-dockerhub-images.sh new file mode 100755 index 0000000..2350b44 --- /dev/null +++ b/travis/delete-dockerhub-images.sh @@ -0,0 +1,71 @@ +#!/bin/bash +# Based on kizbitz/dockerhub-v2-api-organization.sh at https://gist.github.com/kizbitz/175be06d0fbbb39bc9bfa6c0cb0d4721 + +# Example for the Docker Hub V2 API +# Returns all images and tags associated with a Docker Hub organization account. +# Requires 'jq': https://stedolan.github.io/jq/ + +# set username, password, and organization +ORG="eoepca" +if [ -z "$DOCKER_USERNAME" ]; then + echo "Please enter your Dockerhub account name for organization $ORG: " + read -r USERNAME + export DOCKER_USERNAME=${USERNAME} +fi +if [ -z "$DOCKER_PASSWORD" ]; then + echo "Please enter your Dockerhub password for accout $DOCKER_USERNAME, part of $ORG organization:" + read -r PASSWORD + export DOCKER_PASSWORD=${PASSWORD} +fi + +ALL=false +while [ "$1" != "" ]; do + case $1 in + -a | --all ) ALL=true + ;; + -h | --help ) #usage + exit + ;; + * ) #usage + exit 1 + esac + shift +done + +# ------- + +set -e + +# get token +echo "Retrieving token ..." +TOKEN=$(curl -s -H "Content-Type: application/json" -X POST -d '{"username": "'${DOCKER_USERNAME}'", "password": "'${DOCKER_PASSWORD}'"}' https://hub.docker.com/v2/users/login/ | jq -r .token) + +# get list of repositories +echo "Retrieving repository list ..." +REPO_LIST=$(curl -s -H "Authorization: JWT ${TOKEN}" https://hub.docker.com/v2/repositories/${ORG}/?page_size=200 | jq -r '.results|.[]|.name') + +# obtain current repository name +REPO_LOCAL_PATH=`git rev-parse --show-toplevel` +REPO_NAME=`basename $REPO_LOCAL_PATH` + +# delete images and/or tags +echo "Deleting images and tags for organization: ${ORG}" + +echo $REPO_LIST +for i in ${REPO_LIST} +do + if [ "$i" = "$REPO_NAME" ] || [ "$ALL" = "true" ]; then + echo "\nEntering repository $i" + + # Delete by tags starting with "travis_" + IMAGE_TAGS=$(curl -s -H "Authorization: JWT ${TOKEN}" https://hub.docker.com/v2/repositories/${ORG}/${i}/tags/?page_size=300 | jq -r '.results|.[]| select (.name | startswith("travis_")) | .name') + for j in ${IMAGE_TAGS} + do + echo -n " - ${j} ... " + curl -X DELETE -s -H "Authorization: JWT ${TOKEN}" https://hub.docker.com/v2/repositories/${ORG}/${i}/tags/${j}/ + echo "DELETED" + done + fi +done + +echo "\nFinished" \ No newline at end of file diff --git a/travis/release.sh b/travis/release.sh index 7fd2203..b163ca9 100755 --- a/travis/release.sh +++ b/travis/release.sh @@ -6,16 +6,20 @@ set -euov pipefail # Check presence of environment variables TRAVIS_BUILD_NUMBER="${TRAVIS_BUILD_NUMBER:-0}" +# obtain current repository name +REPO_LOCAL_PATH=`git rev-parse --show-toplevel` +REPO_NAME=`basename $REPO_LOCAL_PATH` + # Create a Docker image and tag it as 'travis_' buildTag=travis_$TRAVIS_BUILD_NUMBER # We use a temporary build number for tagging, since this is a transient artefact echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin -docker pull eoepca/template-service:${buildTag} # have to pull locally in order to tag as a release +docker pull eoepca/${REPO_NAME}:${buildTag} # have to pull locally in order to tag as a release # Tag and push as a Release following the SemVer approach, e.g. 0.1.1-Alpha -docker tag eoepca/template-service:${buildTag} eoepca/template-service:release_${TRAVIS_TAG} # This recovers the GitHub release/tag number -docker push eoepca/template-service:release_${TRAVIS_TAG} +docker tag eoepca/${REPO_NAME}:${buildTag} eoepca/${REPO_NAME}:release_${TRAVIS_TAG} # This recovers the GitHub release/tag number +docker push eoepca/${REPO_NAME}:release_${TRAVIS_TAG} # Tag and push as `latest` -docker tag eoepca/template-service:${buildTag} eoepca/template-service:latest -docker push eoepca/template-service:latest +docker tag eoepca/${REPO_NAME}:${buildTag} eoepca/${REPO_NAME}:latest +docker push eoepca/${REPO_NAME}:latest