xref: /OpenGrok/dev/docker.sh (revision 1ca9c8796bed2e501386d296f147250f280bfc7a)
1675e1740SVladimir Kotal#!/bin/bash
2675e1740SVladimir Kotal
3675e1740SVladimir Kotal#
43e788c1bSVladimir Kotal# Build and optionally push new image to Docker hub.
5675e1740SVladimir Kotal#
6ec9556c1SVladimir 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
15*1ca9c879SAdam Hornacekecho "Running linter"
16*1ca9c879SAdam Hornacekdocker run --rm -i hadolint/hadolint:2.6.0 < Dockerfile || exit 1
17*1ca9c879SAdam Hornacek
18e8ba77c1SVladimir KotalAPI_URL="https://hub.docker.com/v2"
1989259090SVladimir KotalIMAGE="opengrok/docker"
2089259090SVladimir Kotal
219e106a9aSVladimir Kotalif [[ -n $OPENGROK_REF && $OPENGROK_REF == refs/tags/* ]]; then
229e106a9aSVladimir Kotal	OPENGROK_TAG=${OPENGROK_REF#"refs/tags/"}
239e106a9aSVladimir Kotalfi
249e106a9aSVladimir Kotal
259e106a9aSVladimir Kotalif [[ -n $OPENGROK_TAG ]]; then
269e106a9aSVladimir Kotal	VERSION="$OPENGROK_TAG"
27573d2ad6SVladimir Kotal	VERSION_SHORT=$( echo $VERSION | cut -d. -f1,2 )
28573d2ad6SVladimir Kotal
29573d2ad6SVladimir Kotal	if [[ -z $VERSION ]]; then
30573d2ad6SVladimir Kotal		echo "empty VERSION"
31573d2ad6SVladimir Kotal		exit 1
32573d2ad6SVladimir Kotal	fi
33573d2ad6SVladimir Kotal
34573d2ad6SVladimir Kotal	if [[ -z $VERSION_SHORT ]]; then
35573d2ad6SVladimir Kotal		echo "empty VERSION_SHORT"
36573d2ad6SVladimir Kotal		exit 1
37573d2ad6SVladimir Kotal	fi
38f9bac693SVladimir Kotal
39014d6520SVladimir Kotal	echo "Version: $VERSION"
40014d6520SVladimir Kotal	echo "Short version: $VERSION_SHORT"
41014d6520SVladimir Kotal
42628efbb5SVladimir Kotal	TAGS="$VERSION $VERSION_SHORT latest"
43628efbb5SVladimir Kotal
44628efbb5SVladimir Kotal	echo "Building docker image for release ($TAGS)"
45573d2ad6SVladimir Kotal	docker build \
4689259090SVladimir Kotal	    -t $IMAGE:$VERSION \
4789259090SVladimir Kotal	    -t $IMAGE:$VERSION_SHORT \
4889259090SVladimir Kotal	    -t $IMAGE:latest .
49628efbb5SVladimir Kotalelse
50628efbb5SVladimir Kotal	TAGS="master"
51628efbb5SVladimir Kotal
52628efbb5SVladimir Kotal	echo "Building docker image for master"
53628efbb5SVladimir Kotal	docker build -t $IMAGE:master .
54628efbb5SVladimir Kotalfi
55573d2ad6SVladimir Kotal
56c0e56161SVladimir Kotal#
57628efbb5SVladimir Kotal# Run the image in a container. This is not strictly needed however
58c0e56161SVladimir Kotal# serves as additional test in automatic builds.
59c0e56161SVladimir Kotal#
60014d6520SVladimir Kotalecho "Running the image in container"
6189259090SVladimir Kotaldocker run -d $IMAGE
62675e1740SVladimir Kotaldocker ps -a
63675e1740SVladimir Kotal
645cda9bedSVladimir Kotal# This can only work on home repository since it needs encrypted variables.
655cda9bedSVladimir Kotalif [[ -n "$OPENGROK_PULL_REQUEST" ]]; then
6689259090SVladimir Kotal	echo "Not pushing Docker image for pull requests"
673e788c1bSVladimir Kotal	exit 0
683e788c1bSVladimir Kotalfi
693e788c1bSVladimir Kotal
703e788c1bSVladimir Kotal# The push only works on the main repository.
715cda9bedSVladimir Kotalif [[ "$OPENGROK_REPO_SLUG" != "oracle/opengrok" ]]; then
7289259090SVladimir Kotal	echo "Not pushing Docker image for non main repository"
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.
87628efbb5SVladimir Kotalif [ -n "$DOCKER_PASSWORD" -a -n "$DOCKER_USERNAME" -a -n "$TAGS" ]; 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:
92628efbb5SVladimir Kotal	for tag in $TAGS; 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
123628efbb5SVladimir Kotal# Update README and badge only for release builds.
124628efbb5SVladimir Kotalif [[ -n $OPENGROK_TAG ]]; then
12589259090SVladimir Kotal	TOKEN=$(curl -s -H "Content-Type: application/json" -X POST \
12689259090SVladimir Kotal	    -d '{"username": "'${DOCKER_USERNAME}'", "password": "'${DOCKER_PASSWORD}'"}' \
127e8ba77c1SVladimir Kotal	    ${API_URL}/users/login/ | jq -r .token)
12889259090SVladimir Kotal	if [[ -z $TOKEN ]]; then
12989259090SVladimir Kotal		echo "Cannot get auth token to publish the README file"
13089259090SVladimir Kotal		exit 1
13189259090SVladimir Kotal	fi
13289259090SVladimir Kotal
13389259090SVladimir Kotal	push_readme "${IMAGE}" "${TOKEN}" "docker/README.md"
134628efbb5SVladimir Kotalfi
135