xref: /OpenGrok/dev/docker.sh (revision 89259090331334df7b2cff4cf42d8b1f213f7983)
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 -x
14675e1740SVladimir Kotalset -e
15675e1740SVladimir Kotal
16*89259090SVladimir KotalIMAGE="opengrok/docker"
17*89259090SVladimir 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 \
38*89259090SVladimir Kotal    -t $IMAGE:$VERSION \
39*89259090SVladimir Kotal    -t $IMAGE:$VERSION_SHORT \
40*89259090SVladimir 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#
46*89259090SVladimir 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
51*89259090SVladimir 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
57*89259090SVladimir Kotal	echo "Not pushing Docker image for non main repository"
583e788c1bSVladimir Kotal	exit 0
593e788c1bSVladimir Kotalfi
603e788c1bSVladimir Kotal
61*89259090SVladimir 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
79*89259090SVladimir 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
84*89259090SVladimir Kotal		echo "Pushing Docker image for tag $tag"
85*89259090SVladimir Kotal		docker push $IMAGE:$tag
86573d2ad6SVladimir Kotal	done
87675e1740SVladimir Kotalfi
88*89259090SVladimir Kotal
89*89259090SVladimir Kotal# Update README file in Docker hub.
90*89259090SVladimir Kotalpush_readme() {
91*89259090SVladimir Kotal	declare -r image="${1}"
92*89259090SVladimir Kotal	declare -r token="${2}"
93*89259090SVladimir Kotal	declare -r input_file="${3}"
94*89259090SVladimir Kotal
95*89259090SVladimir Kotal	if [[ ! -r $input_file ]]; then
96*89259090SVladimir Kotal		echo "file $input_file is not readable"
97*89259090SVladimir Kotal		exit 1
98*89259090SVladimir Kotal	fi
99*89259090SVladimir Kotal
100*89259090SVladimir Kotal	local code=$(jq -n --arg msg "$(<${input_file})" \
101*89259090SVladimir Kotal	    '{"registry":"registry-1.docker.io","full_description": $msg }' | \
102*89259090SVladimir Kotal	        curl -s -o /dev/null  -L -w "%{http_code}" \
103*89259090SVladimir Kotal	           https://cloud.docker.com/v2/repositories/"${image}"/ \
104*89259090SVladimir Kotal	           -d @- -X PATCH \
105*89259090SVladimir Kotal	           -H "Content-Type: application/json" \
106*89259090SVladimir Kotal	           -H "Authorization: JWT ${token}")
107*89259090SVladimir Kotal
108*89259090SVladimir Kotal	if [[ "${code}" = "200" ]]; then
109*89259090SVladimir Kotal		echo "Successfully pushed README to Docker Hub"
110*89259090SVladimir Kotal	else
111*89259090SVladimir Kotal		printf "Unable to push README to Docker Hub, response code: %s\n" "${code}"
112*89259090SVladimir Kotal		exit 1
113*89259090SVladimir Kotal	fi
114*89259090SVladimir Kotal}
115*89259090SVladimir Kotal
116*89259090SVladimir KotalTOKEN=$(curl -s -H "Content-Type: application/json" -X POST \
117*89259090SVladimir Kotal    -d '{"username": "'${DOCKER_USERNAME}'", "password": "'${DOCKER_PASSWORD}'"}' \
118*89259090SVladimir Kotal    https://hub.docker.com/v2/users/login/ | jq -r .token)
119*89259090SVladimir Kotalif [[ -z $TOKEN ]]; then
120*89259090SVladimir Kotal	echo "Cannot get auth token to publish the README file"
121*89259090SVladimir Kotal	exit 1
122*89259090SVladimir Kotalfi
123*89259090SVladimir Kotal
124*89259090SVladimir Kotalpush_readme "${IMAGE}" "${TOKEN}" "docker/README.md"
125