xref: /OpenGrok/dev/docker.sh (revision 9e106a9ab1c70fca4f1a41d170bef47e8efe00bd)
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#
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
18*9e106a9aSVladimir Kotalif [[ -n $OPENGROK_REF && $OPENGROK_REF == refs/tags/* ]]; then
19*9e106a9aSVladimir Kotal	OPENGROK_TAG=${OPENGROK_REF#"refs/tags/"}
20*9e106a9aSVladimir Kotalfi
21*9e106a9aSVladimir Kotal
22*9e106a9aSVladimir Kotalif [[ -n $OPENGROK_TAG ]]; then
23*9e106a9aSVladimir Kotal	VERSION="$OPENGROK_TAG"
24573d2ad6SVladimir Kotal	VERSION_SHORT=$( echo $VERSION | cut -d. -f1,2 )
253e788c1bSVladimir Kotalelse
263e788c1bSVladimir Kotal	VERSION="latest"
273e788c1bSVladimir Kotal	VERSION_SHORT="latest"
283e788c1bSVladimir Kotalfi
29573d2ad6SVladimir Kotal
30573d2ad6SVladimir Kotalif [[ -z $VERSION ]]; then
31573d2ad6SVladimir Kotal	echo "empty VERSION"
32573d2ad6SVladimir Kotal	exit 1
33573d2ad6SVladimir Kotalfi
34573d2ad6SVladimir Kotal
35573d2ad6SVladimir Kotalif [[ -z $VERSION_SHORT ]]; then
36573d2ad6SVladimir Kotal	echo "empty VERSION_SHORT"
37573d2ad6SVladimir Kotal	exit 1
38573d2ad6SVladimir Kotalfi
39f9bac693SVladimir Kotal
40014d6520SVladimir Kotalecho "Version: $VERSION"
41014d6520SVladimir Kotalecho "Short version: $VERSION_SHORT"
42014d6520SVladimir Kotal
43c0e56161SVladimir Kotal# Build the image.
44014d6520SVladimir Kotalecho "Building docker image"
45573d2ad6SVladimir Kotaldocker build \
4689259090SVladimir Kotal    -t $IMAGE:$VERSION \
4789259090SVladimir Kotal    -t $IMAGE:$VERSION_SHORT \
4889259090SVladimir Kotal    -t $IMAGE:latest .
49573d2ad6SVladimir Kotal
50c0e56161SVladimir Kotal#
51c0e56161SVladimir Kotal# Run the image in container. This is not strictly needed however
52c0e56161SVladimir Kotal# serves as additional test in automatic builds.
53c0e56161SVladimir Kotal#
54014d6520SVladimir Kotalecho "Running the image in container"
5589259090SVladimir Kotaldocker run -d $IMAGE
56675e1740SVladimir Kotaldocker ps -a
57675e1740SVladimir Kotal
583e788c1bSVladimir Kotal# Travis can only work on master since it needs encrypted variables.
59*9e106a9aSVladimir Kotalif [ "${OPENGROK_PULL_REQUEST}" != "false" ]; then
6089259090SVladimir Kotal	echo "Not pushing Docker image for pull requests"
613e788c1bSVladimir Kotal	exit 0
623e788c1bSVladimir Kotalfi
633e788c1bSVladimir Kotal
643e788c1bSVladimir Kotal# The push only works on the main repository.
65*9e106a9aSVladimir Kotalif [[ "${OPENGROK_REPO_SLUG}" != "oracle/opengrok" ]]; then
6689259090SVladimir Kotal	echo "Not pushing Docker image for non main repository"
673e788c1bSVladimir Kotal	exit 0
683e788c1bSVladimir Kotalfi
693e788c1bSVladimir Kotal
7089259090SVladimir Kotal# Allow Docker push for release builds only.
71*9e106a9aSVladimir Kotalif [[ -z $OPENGROK_TAG ]]; then
72*9e106a9aSVladimir Kotal	echo "OPENGROK_TAG is empty"
733e788c1bSVladimir Kotal	exit 0
743e788c1bSVladimir Kotalfi
753e788c1bSVladimir Kotal
763e788c1bSVladimir Kotalif [[ -z $DOCKER_USERNAME ]]; then
773e788c1bSVladimir Kotal	echo "DOCKER_USERNAME is empty"
783e788c1bSVladimir Kotal	exit 1
793e788c1bSVladimir Kotalfi
803e788c1bSVladimir Kotal
813e788c1bSVladimir Kotalif [[ -z $DOCKER_PASSWORD ]]; then
823e788c1bSVladimir Kotal	echo "DOCKER_PASSWORD is empty"
833e788c1bSVladimir Kotal	exit 1
843e788c1bSVladimir Kotalfi
853e788c1bSVladimir Kotal
86675e1740SVladimir Kotal# Publish the image to Docker hub.
87675e1740SVladimir Kotalif [ -n "$DOCKER_PASSWORD" -a -n "$DOCKER_USERNAME" -a -n "$VERSION" ]; then
8889259090SVladimir Kotal	echo "Logging into Docker Hub"
89675e1740SVladimir Kotal	echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin
90c0e56161SVladimir Kotal
91c0e56161SVladimir Kotal	# All the tags need to be pushed individually:
92573d2ad6SVladimir Kotal	for tag in $VERSION $VERSION_SHORT latest; do
9389259090SVladimir Kotal		echo "Pushing Docker image for tag $tag"
9489259090SVladimir Kotal		docker push $IMAGE:$tag
95573d2ad6SVladimir Kotal	done
96675e1740SVladimir Kotalfi
9789259090SVladimir Kotal
9889259090SVladimir Kotal# Update README file in Docker hub.
9989259090SVladimir Kotalpush_readme() {
10089259090SVladimir Kotal	declare -r image="${1}"
10189259090SVladimir Kotal	declare -r token="${2}"
10289259090SVladimir Kotal	declare -r input_file="${3}"
10389259090SVladimir Kotal
10489259090SVladimir Kotal	if [[ ! -r $input_file ]]; then
10589259090SVladimir Kotal		echo "file $input_file is not readable"
10689259090SVladimir Kotal		exit 1
10789259090SVladimir Kotal	fi
10889259090SVladimir Kotal
109e8ba77c1SVladimir Kotal	local code=$(curl -s -o /dev/null -L -w "%{http_code}" \
110e8ba77c1SVladimir Kotal	           -X PATCH --data-urlencode \
111e8ba77c1SVladimir Kotal		   full_description@${input_file} \
112e8ba77c1SVladimir Kotal	           -H "Authorization: JWT ${token}" \
113e8ba77c1SVladimir Kotal	           ${API_URL}/repositories/"${image}"/)
11489259090SVladimir Kotal
11589259090SVladimir Kotal	if [[ "${code}" = "200" ]]; then
11689259090SVladimir Kotal		echo "Successfully pushed README to Docker Hub"
11789259090SVladimir Kotal	else
11889259090SVladimir Kotal		printf "Unable to push README to Docker Hub, response code: %s\n" "${code}"
11989259090SVladimir Kotal		exit 1
12089259090SVladimir Kotal	fi
12189259090SVladimir Kotal}
12289259090SVladimir Kotal
12389259090SVladimir KotalTOKEN=$(curl -s -H "Content-Type: application/json" -X POST \
12489259090SVladimir Kotal    -d '{"username": "'${DOCKER_USERNAME}'", "password": "'${DOCKER_PASSWORD}'"}' \
125e8ba77c1SVladimir Kotal    ${API_URL}/users/login/ | jq -r .token)
12689259090SVladimir Kotalif [[ -z $TOKEN ]]; then
12789259090SVladimir Kotal	echo "Cannot get auth token to publish the README file"
12889259090SVladimir Kotal	exit 1
12989259090SVladimir Kotalfi
13089259090SVladimir Kotal
13189259090SVladimir Kotalpush_readme "${IMAGE}" "${TOKEN}" "docker/README.md"
1322387ff1aSVladimir Kotal
1332387ff1aSVladimir Kotal# update Microbadger
1342387ff1aSVladimir Kotalcurl -X POST https://hooks.microbadger.com/images/opengrok/docker/pSastb42Ikfn2dF5llR54sSPqbQ=
135