xref: /OpenGrok/dev/docker.sh (revision ec9556c1bc1b8f9d49a394990488520457746c87)
1675e1740SVladimir Kotal#!/bin/bash
2675e1740SVladimir Kotal
3675e1740SVladimir Kotal#
43e788c1bSVladimir Kotal# Build and optionally push new image to Docker hub.
5675e1740SVladimir Kotal#
6*ec9556c1SVladimir Kotal# When pushing, this script uses the following secure variables:
7675e1740SVladimir Kotal#  - DOCKER_USERNAME
8675e1740SVladimir Kotal#  - DOCKER_PASSWORD
9675e1740SVladimir Kotal#
1012897179SVladimir 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
189e106a9aSVladimir Kotalif [[ -n $OPENGROK_REF && $OPENGROK_REF == refs/tags/* ]]; then
199e106a9aSVladimir Kotal	OPENGROK_TAG=${OPENGROK_REF#"refs/tags/"}
209e106a9aSVladimir Kotalfi
219e106a9aSVladimir Kotal
229e106a9aSVladimir Kotalif [[ -n $OPENGROK_TAG ]]; then
239e106a9aSVladimir Kotal	VERSION="$OPENGROK_TAG"
24573d2ad6SVladimir Kotal	VERSION_SHORT=$( echo $VERSION | cut -d. -f1,2 )
25573d2ad6SVladimir Kotal
26573d2ad6SVladimir Kotal	if [[ -z $VERSION ]]; then
27573d2ad6SVladimir Kotal		echo "empty VERSION"
28573d2ad6SVladimir Kotal		exit 1
29573d2ad6SVladimir Kotal	fi
30573d2ad6SVladimir Kotal
31573d2ad6SVladimir Kotal	if [[ -z $VERSION_SHORT ]]; then
32573d2ad6SVladimir Kotal		echo "empty VERSION_SHORT"
33573d2ad6SVladimir Kotal		exit 1
34573d2ad6SVladimir Kotal	fi
35f9bac693SVladimir Kotal
36014d6520SVladimir Kotal	echo "Version: $VERSION"
37014d6520SVladimir Kotal	echo "Short version: $VERSION_SHORT"
38014d6520SVladimir Kotal
39628efbb5SVladimir Kotal	TAGS="$VERSION $VERSION_SHORT latest"
40628efbb5SVladimir Kotal
41628efbb5SVladimir Kotal	echo "Building docker image for release ($TAGS)"
42573d2ad6SVladimir Kotal	docker build \
4389259090SVladimir Kotal	    -t $IMAGE:$VERSION \
4489259090SVladimir Kotal	    -t $IMAGE:$VERSION_SHORT \
4589259090SVladimir Kotal	    -t $IMAGE:latest .
46628efbb5SVladimir Kotalelse
47628efbb5SVladimir Kotal	TAGS="master"
48628efbb5SVladimir Kotal
49628efbb5SVladimir Kotal	echo "Building docker image for master"
50628efbb5SVladimir Kotal	docker build -t $IMAGE:master .
51628efbb5SVladimir Kotalfi
52573d2ad6SVladimir Kotal
53c0e56161SVladimir Kotal#
54628efbb5SVladimir Kotal# Run the image in a container. This is not strictly needed however
55c0e56161SVladimir Kotal# serves as additional test in automatic builds.
56c0e56161SVladimir Kotal#
57014d6520SVladimir Kotalecho "Running the image in container"
5889259090SVladimir Kotaldocker run -d $IMAGE
59675e1740SVladimir Kotaldocker ps -a
60675e1740SVladimir Kotal
615cda9bedSVladimir Kotal# This can only work on home repository since it needs encrypted variables.
625cda9bedSVladimir Kotalif [[ -n "$OPENGROK_PULL_REQUEST" ]]; then
6389259090SVladimir Kotal	echo "Not pushing Docker image for pull requests"
643e788c1bSVladimir Kotal	exit 0
653e788c1bSVladimir Kotalfi
663e788c1bSVladimir Kotal
673e788c1bSVladimir Kotal# The push only works on the main repository.
685cda9bedSVladimir Kotalif [[ "$OPENGROK_REPO_SLUG" != "oracle/opengrok" ]]; then
6989259090SVladimir Kotal	echo "Not pushing Docker image for non main repository"
703e788c1bSVladimir Kotal	exit 0
713e788c1bSVladimir Kotalfi
723e788c1bSVladimir Kotal
733e788c1bSVladimir Kotalif [[ -z $DOCKER_USERNAME ]]; then
743e788c1bSVladimir Kotal	echo "DOCKER_USERNAME is empty"
753e788c1bSVladimir Kotal	exit 1
763e788c1bSVladimir Kotalfi
773e788c1bSVladimir Kotal
783e788c1bSVladimir Kotalif [[ -z $DOCKER_PASSWORD ]]; then
793e788c1bSVladimir Kotal	echo "DOCKER_PASSWORD is empty"
803e788c1bSVladimir Kotal	exit 1
813e788c1bSVladimir Kotalfi
823e788c1bSVladimir Kotal
83675e1740SVladimir Kotal# Publish the image to Docker hub.
84628efbb5SVladimir Kotalif [ -n "$DOCKER_PASSWORD" -a -n "$DOCKER_USERNAME" -a -n "$TAGS" ]; then
8589259090SVladimir Kotal	echo "Logging into Docker Hub"
86675e1740SVladimir Kotal	echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin
87c0e56161SVladimir Kotal
88c0e56161SVladimir Kotal	# All the tags need to be pushed individually:
89628efbb5SVladimir Kotal	for tag in $TAGS; do
9089259090SVladimir Kotal		echo "Pushing Docker image for tag $tag"
9189259090SVladimir Kotal		docker push $IMAGE:$tag
92573d2ad6SVladimir Kotal	done
93675e1740SVladimir Kotalfi
9489259090SVladimir Kotal
9589259090SVladimir Kotal# Update README file in Docker hub.
9689259090SVladimir Kotalpush_readme() {
9789259090SVladimir Kotal	declare -r image="${1}"
9889259090SVladimir Kotal	declare -r token="${2}"
9989259090SVladimir Kotal	declare -r input_file="${3}"
10089259090SVladimir Kotal
10189259090SVladimir Kotal	if [[ ! -r $input_file ]]; then
10289259090SVladimir Kotal		echo "file $input_file is not readable"
10389259090SVladimir Kotal		exit 1
10489259090SVladimir Kotal	fi
10589259090SVladimir Kotal
106e8ba77c1SVladimir Kotal	local code=$(curl -s -o /dev/null -L -w "%{http_code}" \
107e8ba77c1SVladimir Kotal	           -X PATCH --data-urlencode \
108e8ba77c1SVladimir Kotal		   full_description@${input_file} \
109e8ba77c1SVladimir Kotal	           -H "Authorization: JWT ${token}" \
110e8ba77c1SVladimir Kotal	           ${API_URL}/repositories/"${image}"/)
11189259090SVladimir Kotal
11289259090SVladimir Kotal	if [[ "${code}" = "200" ]]; then
11389259090SVladimir Kotal		echo "Successfully pushed README to Docker Hub"
11489259090SVladimir Kotal	else
11589259090SVladimir Kotal		printf "Unable to push README to Docker Hub, response code: %s\n" "${code}"
11689259090SVladimir Kotal		exit 1
11789259090SVladimir Kotal	fi
11889259090SVladimir Kotal}
11989259090SVladimir Kotal
120628efbb5SVladimir Kotal# Update README and badge only for release builds.
121628efbb5SVladimir Kotalif [[ -n $OPENGROK_TAG ]]; then
12289259090SVladimir Kotal	TOKEN=$(curl -s -H "Content-Type: application/json" -X POST \
12389259090SVladimir Kotal	    -d '{"username": "'${DOCKER_USERNAME}'", "password": "'${DOCKER_PASSWORD}'"}' \
124e8ba77c1SVladimir Kotal	    ${API_URL}/users/login/ | jq -r .token)
12589259090SVladimir Kotal	if [[ -z $TOKEN ]]; then
12689259090SVladimir Kotal		echo "Cannot get auth token to publish the README file"
12789259090SVladimir Kotal		exit 1
12889259090SVladimir Kotal	fi
12989259090SVladimir Kotal
13089259090SVladimir Kotal	push_readme "${IMAGE}" "${TOKEN}" "docker/README.md"
1312387ff1aSVladimir Kotal
1322387ff1aSVladimir Kotal	# update Microbadger
13350676341SVladimir Kotal	curl -s -X POST https://hooks.microbadger.com/images/opengrok/docker/pSastb42Ikfn2dF5llR54sSPqbQ=
134628efbb5SVladimir Kotalfi
135