1675e1740SVladimir Kotal#!/bin/bash 2675e1740SVladimir Kotal 3675e1740SVladimir Kotal# 43e788c1bSVladimir Kotal# Build and optionally push new image to Docker hub. 5675e1740SVladimir Kotal# 63e788c1bSVladimir Kotal# When pushing, this script uses the following Travis secure variables: 7675e1740SVladimir Kotal# - DOCKER_USERNAME 8675e1740SVladimir Kotal# - DOCKER_PASSWORD 9675e1740SVladimir Kotal# 10*12897179SVladimir Kotal# These are set via https://github.com/oracle/opengrok/settings/secrets 11675e1740SVladimir Kotal# 12675e1740SVladimir Kotal 13675e1740SVladimir Kotalset -e 14675e1740SVladimir Kotal 15e8ba77c1SVladimir KotalAPI_URL="https://hub.docker.com/v2" 1689259090SVladimir KotalIMAGE="opengrok/docker" 1789259090SVladimir Kotal 183e788c1bSVladimir Kotalif [[ -n $TRAVIS_TAG ]]; then 19f9bac693SVladimir Kotal VERSION="$TRAVIS_TAG" 20573d2ad6SVladimir Kotal VERSION_SHORT=$( echo $VERSION | cut -d. -f1,2 ) 213e788c1bSVladimir Kotalelse 223e788c1bSVladimir Kotal VERSION="latest" 233e788c1bSVladimir Kotal VERSION_SHORT="latest" 243e788c1bSVladimir Kotalfi 25573d2ad6SVladimir Kotal 26573d2ad6SVladimir Kotalif [[ -z $VERSION ]]; then 27573d2ad6SVladimir Kotal echo "empty VERSION" 28573d2ad6SVladimir Kotal exit 1 29573d2ad6SVladimir Kotalfi 30573d2ad6SVladimir Kotal 31573d2ad6SVladimir Kotalif [[ -z $VERSION_SHORT ]]; then 32573d2ad6SVladimir Kotal echo "empty VERSION_SHORT" 33573d2ad6SVladimir Kotal exit 1 34573d2ad6SVladimir Kotalfi 35f9bac693SVladimir Kotal 36014d6520SVladimir Kotalecho "Version: $VERSION" 37014d6520SVladimir Kotalecho "Short version: $VERSION_SHORT" 38014d6520SVladimir Kotal 39c0e56161SVladimir Kotal# Build the image. 40014d6520SVladimir Kotalecho "Building docker image" 41573d2ad6SVladimir Kotaldocker build \ 4289259090SVladimir Kotal -t $IMAGE:$VERSION \ 4389259090SVladimir Kotal -t $IMAGE:$VERSION_SHORT \ 4489259090SVladimir Kotal -t $IMAGE:latest . 45573d2ad6SVladimir Kotal 46c0e56161SVladimir Kotal# 47c0e56161SVladimir Kotal# Run the image in container. This is not strictly needed however 48c0e56161SVladimir Kotal# serves as additional test in automatic builds. 49c0e56161SVladimir Kotal# 50014d6520SVladimir Kotalecho "Running the image in container" 5189259090SVladimir Kotaldocker run -d $IMAGE 52675e1740SVladimir Kotaldocker ps -a 53675e1740SVladimir Kotal 543e788c1bSVladimir Kotal# Travis can only work on master since it needs encrypted variables. 553e788c1bSVladimir Kotalif [ "${TRAVIS_PULL_REQUEST}" != "false" ]; then 5689259090SVladimir Kotal echo "Not pushing Docker image for pull requests" 573e788c1bSVladimir Kotal exit 0 583e788c1bSVladimir Kotalfi 593e788c1bSVladimir Kotal 603e788c1bSVladimir Kotal# The push only works on the main repository. 613e788c1bSVladimir Kotalif [[ "${TRAVIS_REPO_SLUG}" != "oracle/opengrok" ]]; then 6289259090SVladimir Kotal echo "Not pushing Docker image for non main repository" 633e788c1bSVladimir Kotal exit 0 643e788c1bSVladimir Kotalfi 653e788c1bSVladimir Kotal 6689259090SVladimir Kotal# Allow Docker push for release builds only. 673e788c1bSVladimir Kotalif [[ -z $TRAVIS_TAG ]]; then 683e788c1bSVladimir Kotal echo "TRAVIS_TAG is empty" 693e788c1bSVladimir Kotal exit 0 703e788c1bSVladimir Kotalfi 713e788c1bSVladimir Kotal 723e788c1bSVladimir Kotalif [[ -z $DOCKER_USERNAME ]]; then 733e788c1bSVladimir Kotal echo "DOCKER_USERNAME is empty" 743e788c1bSVladimir Kotal exit 1 753e788c1bSVladimir Kotalfi 763e788c1bSVladimir Kotal 773e788c1bSVladimir Kotalif [[ -z $DOCKER_PASSWORD ]]; then 783e788c1bSVladimir Kotal echo "DOCKER_PASSWORD is empty" 793e788c1bSVladimir Kotal exit 1 803e788c1bSVladimir Kotalfi 813e788c1bSVladimir Kotal 82675e1740SVladimir Kotal# Publish the image to Docker hub. 83675e1740SVladimir Kotalif [ -n "$DOCKER_PASSWORD" -a -n "$DOCKER_USERNAME" -a -n "$VERSION" ]; then 8489259090SVladimir Kotal echo "Logging into Docker Hub" 85675e1740SVladimir Kotal echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin 86c0e56161SVladimir Kotal 87c0e56161SVladimir Kotal # All the tags need to be pushed individually: 88573d2ad6SVladimir Kotal for tag in $VERSION $VERSION_SHORT latest; do 8989259090SVladimir Kotal echo "Pushing Docker image for tag $tag" 9089259090SVladimir Kotal docker push $IMAGE:$tag 91573d2ad6SVladimir Kotal done 92675e1740SVladimir Kotalfi 9389259090SVladimir Kotal 9489259090SVladimir Kotal# Update README file in Docker hub. 9589259090SVladimir Kotalpush_readme() { 9689259090SVladimir Kotal declare -r image="${1}" 9789259090SVladimir Kotal declare -r token="${2}" 9889259090SVladimir Kotal declare -r input_file="${3}" 9989259090SVladimir Kotal 10089259090SVladimir Kotal if [[ ! -r $input_file ]]; then 10189259090SVladimir Kotal echo "file $input_file is not readable" 10289259090SVladimir Kotal exit 1 10389259090SVladimir Kotal fi 10489259090SVladimir Kotal 105e8ba77c1SVladimir Kotal local code=$(curl -s -o /dev/null -L -w "%{http_code}" \ 106e8ba77c1SVladimir Kotal -X PATCH --data-urlencode \ 107e8ba77c1SVladimir Kotal full_description@${input_file} \ 108e8ba77c1SVladimir Kotal -H "Authorization: JWT ${token}" \ 109e8ba77c1SVladimir Kotal ${API_URL}/repositories/"${image}"/) 11089259090SVladimir Kotal 11189259090SVladimir Kotal if [[ "${code}" = "200" ]]; then 11289259090SVladimir Kotal echo "Successfully pushed README to Docker Hub" 11389259090SVladimir Kotal else 11489259090SVladimir Kotal printf "Unable to push README to Docker Hub, response code: %s\n" "${code}" 11589259090SVladimir Kotal exit 1 11689259090SVladimir Kotal fi 11789259090SVladimir Kotal} 11889259090SVladimir Kotal 11989259090SVladimir KotalTOKEN=$(curl -s -H "Content-Type: application/json" -X POST \ 12089259090SVladimir Kotal -d '{"username": "'${DOCKER_USERNAME}'", "password": "'${DOCKER_PASSWORD}'"}' \ 121e8ba77c1SVladimir Kotal ${API_URL}/users/login/ | jq -r .token) 12289259090SVladimir Kotalif [[ -z $TOKEN ]]; then 12389259090SVladimir Kotal echo "Cannot get auth token to publish the README file" 12489259090SVladimir Kotal exit 1 12589259090SVladimir Kotalfi 12689259090SVladimir Kotal 12789259090SVladimir Kotalpush_readme "${IMAGE}" "${TOKEN}" "docker/README.md" 1282387ff1aSVladimir Kotal 1292387ff1aSVladimir Kotal# update Microbadger 1302387ff1aSVladimir Kotalcurl -X POST https://hooks.microbadger.com/images/opengrok/docker/pSastb42Ikfn2dF5llR54sSPqbQ= 131