xref: /OpenGrok/dev/docker.sh (revision e8ba77c1e89062c4e6eed987dab703ffd6141a4c)
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#
10675e1740SVladimir Kotal# These are set via https://travis-ci.com/OpenGrok/docker/settings
11675e1740SVladimir Kotal#
12675e1740SVladimir Kotal
13675e1740SVladimir Kotalset -e
14675e1740SVladimir Kotal
15*e8ba77c1SVladimir 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
36c0e56161SVladimir Kotal# Build the image.
37573d2ad6SVladimir Kotaldocker build \
3889259090SVladimir Kotal    -t $IMAGE:$VERSION \
3989259090SVladimir Kotal    -t $IMAGE:$VERSION_SHORT \
4089259090SVladimir Kotal    -t $IMAGE:latest .
41573d2ad6SVladimir Kotal
42c0e56161SVladimir Kotal#
43c0e56161SVladimir Kotal# Run the image in container. This is not strictly needed however
44c0e56161SVladimir Kotal# serves as additional test in automatic builds.
45c0e56161SVladimir Kotal#
4689259090SVladimir Kotaldocker run -d $IMAGE
47675e1740SVladimir Kotaldocker ps -a
48675e1740SVladimir Kotal
493e788c1bSVladimir Kotal# Travis can only work on master since it needs encrypted variables.
503e788c1bSVladimir Kotalif [ "${TRAVIS_PULL_REQUEST}" != "false" ]; then
5189259090SVladimir Kotal	echo "Not pushing Docker image for pull requests"
523e788c1bSVladimir Kotal	exit 0
533e788c1bSVladimir Kotalfi
543e788c1bSVladimir Kotal
553e788c1bSVladimir Kotal# The push only works on the main repository.
563e788c1bSVladimir Kotalif [[ "${TRAVIS_REPO_SLUG}" != "oracle/opengrok" ]]; then
5789259090SVladimir Kotal	echo "Not pushing Docker image for non main repository"
583e788c1bSVladimir Kotal	exit 0
593e788c1bSVladimir Kotalfi
603e788c1bSVladimir Kotal
6189259090SVladimir Kotal# Allow Docker push for release builds only.
623e788c1bSVladimir Kotalif [[ -z $TRAVIS_TAG ]]; then
633e788c1bSVladimir Kotal	echo "TRAVIS_TAG is empty"
643e788c1bSVladimir Kotal	exit 0
653e788c1bSVladimir Kotalfi
663e788c1bSVladimir Kotal
673e788c1bSVladimir Kotalif [[ -z $DOCKER_USERNAME ]]; then
683e788c1bSVladimir Kotal	echo "DOCKER_USERNAME is empty"
693e788c1bSVladimir Kotal	exit 1
703e788c1bSVladimir Kotalfi
713e788c1bSVladimir Kotal
723e788c1bSVladimir Kotalif [[ -z $DOCKER_PASSWORD ]]; then
733e788c1bSVladimir Kotal	echo "DOCKER_PASSWORD is empty"
743e788c1bSVladimir Kotal	exit 1
753e788c1bSVladimir Kotalfi
763e788c1bSVladimir Kotal
77675e1740SVladimir Kotal# Publish the image to Docker hub.
78675e1740SVladimir Kotalif [ -n "$DOCKER_PASSWORD" -a -n "$DOCKER_USERNAME" -a -n "$VERSION" ]; then
7989259090SVladimir Kotal	echo "Logging into Docker Hub"
80675e1740SVladimir Kotal	echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin
81c0e56161SVladimir Kotal
82c0e56161SVladimir Kotal	# All the tags need to be pushed individually:
83573d2ad6SVladimir Kotal	for tag in $VERSION $VERSION_SHORT latest; do
8489259090SVladimir Kotal		echo "Pushing Docker image for tag $tag"
8589259090SVladimir Kotal		docker push $IMAGE:$tag
86573d2ad6SVladimir Kotal	done
87675e1740SVladimir Kotalfi
8889259090SVladimir Kotal
8989259090SVladimir Kotal# Update README file in Docker hub.
9089259090SVladimir Kotalpush_readme() {
9189259090SVladimir Kotal	declare -r image="${1}"
9289259090SVladimir Kotal	declare -r token="${2}"
9389259090SVladimir Kotal	declare -r input_file="${3}"
9489259090SVladimir Kotal
9589259090SVladimir Kotal	if [[ ! -r $input_file ]]; then
9689259090SVladimir Kotal		echo "file $input_file is not readable"
9789259090SVladimir Kotal		exit 1
9889259090SVladimir Kotal	fi
9989259090SVladimir Kotal
100*e8ba77c1SVladimir Kotal	local code=$(curl -s -o /dev/null -L -w "%{http_code}" \
101*e8ba77c1SVladimir Kotal	           -X PATCH --data-urlencode \
102*e8ba77c1SVladimir Kotal		   full_description@${input_file} \
103*e8ba77c1SVladimir Kotal	           -H "Authorization: JWT ${token}" \
104*e8ba77c1SVladimir Kotal	           ${API_URL}/repositories/"${image}"/)
10589259090SVladimir Kotal
10689259090SVladimir Kotal	if [[ "${code}" = "200" ]]; then
10789259090SVladimir Kotal		echo "Successfully pushed README to Docker Hub"
10889259090SVladimir Kotal	else
10989259090SVladimir Kotal		printf "Unable to push README to Docker Hub, response code: %s\n" "${code}"
11089259090SVladimir Kotal		exit 1
11189259090SVladimir Kotal	fi
11289259090SVladimir Kotal}
11389259090SVladimir Kotal
11489259090SVladimir KotalTOKEN=$(curl -s -H "Content-Type: application/json" -X POST \
11589259090SVladimir Kotal    -d '{"username": "'${DOCKER_USERNAME}'", "password": "'${DOCKER_PASSWORD}'"}' \
116*e8ba77c1SVladimir Kotal    ${API_URL}/users/login/ | jq -r .token)
11789259090SVladimir Kotalif [[ -z $TOKEN ]]; then
11889259090SVladimir Kotal	echo "Cannot get auth token to publish the README file"
11989259090SVladimir Kotal	exit 1
12089259090SVladimir Kotalfi
12189259090SVladimir Kotal
12289259090SVladimir Kotalpush_readme "${IMAGE}" "${TOKEN}" "docker/README.md"
123