xref: /OpenGrok/dev/docker.sh (revision 3e788c1b97f9c483045104a2c5acc88541d015f9)
1675e1740SVladimir Kotal#!/bin/bash
2675e1740SVladimir Kotal
3675e1740SVladimir Kotal#
4*3e788c1bSVladimir Kotal# Build and optionally push new image to Docker hub.
5675e1740SVladimir Kotal#
6*3e788c1bSVladimir 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*3e788c1bSVladimir Kotalif [[ -n $TRAVIS_TAG ]]; then
17f9bac693SVladimir Kotal	VERSION="$TRAVIS_TAG"
18573d2ad6SVladimir Kotal	VERSION_SHORT=$( echo $VERSION | cut -d. -f1,2 )
19*3e788c1bSVladimir Kotalelse
20*3e788c1bSVladimir Kotal	VERSION="latest"
21*3e788c1bSVladimir Kotal	VERSION_SHORT="latest"
22*3e788c1bSVladimir Kotalfi
23573d2ad6SVladimir Kotal
24573d2ad6SVladimir Kotalif [[ -z $VERSION ]]; then
25573d2ad6SVladimir Kotal	echo "empty VERSION"
26573d2ad6SVladimir Kotal	exit 1
27573d2ad6SVladimir Kotalfi
28573d2ad6SVladimir Kotal
29573d2ad6SVladimir Kotalif [[ -z $VERSION_SHORT ]]; then
30573d2ad6SVladimir Kotal	echo "empty VERSION_SHORT"
31573d2ad6SVladimir Kotal	exit 1
32573d2ad6SVladimir Kotalfi
33f9bac693SVladimir Kotal
34c0e56161SVladimir Kotal# Build the image.
35573d2ad6SVladimir Kotaldocker build \
36573d2ad6SVladimir Kotal    -t opengrok/docker:$VERSION \
37573d2ad6SVladimir Kotal    -t opengrok/docker:$VERSION_SHORT \
38573d2ad6SVladimir Kotal    -t opengrok/docker:latest .
39573d2ad6SVladimir Kotal
40c0e56161SVladimir Kotal#
41c0e56161SVladimir Kotal# Run the image in container. This is not strictly needed however
42c0e56161SVladimir Kotal# serves as additional test in automatic builds.
43c0e56161SVladimir Kotal#
44675e1740SVladimir Kotaldocker run -d opengrok/docker
45675e1740SVladimir Kotaldocker ps -a
46675e1740SVladimir Kotal
47*3e788c1bSVladimir Kotal# Travis can only work on master since it needs encrypted variables.
48*3e788c1bSVladimir Kotalif [ "${TRAVIS_PULL_REQUEST}" != "false" ]; then
49*3e788c1bSVladimir Kotal	echo "Not publishing docker image for pull requests"
50*3e788c1bSVladimir Kotal	exit 0
51*3e788c1bSVladimir Kotalfi
52*3e788c1bSVladimir Kotal
53*3e788c1bSVladimir Kotal# The push only works on the main repository.
54*3e788c1bSVladimir Kotalif [[ "${TRAVIS_REPO_SLUG}" != "oracle/opengrok" ]]; then
55*3e788c1bSVladimir Kotal	echo "Not publishing docker image for non main repository"
56*3e788c1bSVladimir Kotal	exit 0
57*3e788c1bSVladimir Kotalfi
58*3e788c1bSVladimir Kotal
59*3e788c1bSVladimir Kotal# Allow Docker publish for release builds only.
60*3e788c1bSVladimir Kotalif [[ -z $TRAVIS_TAG ]]; then
61*3e788c1bSVladimir Kotal	echo "TRAVIS_TAG is empty"
62*3e788c1bSVladimir Kotal	exit 0
63*3e788c1bSVladimir Kotalfi
64*3e788c1bSVladimir Kotal
65*3e788c1bSVladimir Kotalif [[ -z $DOCKER_USERNAME ]]; then
66*3e788c1bSVladimir Kotal	echo "DOCKER_USERNAME is empty"
67*3e788c1bSVladimir Kotal	exit 1
68*3e788c1bSVladimir Kotalfi
69*3e788c1bSVladimir Kotal
70*3e788c1bSVladimir Kotalif [[ -z $DOCKER_PASSWORD ]]; then
71*3e788c1bSVladimir Kotal	echo "DOCKER_PASSWORD is empty"
72*3e788c1bSVladimir Kotal	exit 1
73*3e788c1bSVladimir Kotalfi
74*3e788c1bSVladimir Kotal
75675e1740SVladimir Kotal# Publish the image to Docker hub.
76675e1740SVladimir Kotalif [ -n "$DOCKER_PASSWORD" -a -n "$DOCKER_USERNAME" -a -n "$VERSION" ]; then
77c0e56161SVladimir Kotal	echo "Logging into docker"
78675e1740SVladimir Kotal	echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin
79c0e56161SVladimir Kotal
80c0e56161SVladimir Kotal	# All the tags need to be pushed individually:
81573d2ad6SVladimir Kotal	for tag in $VERSION $VERSION_SHORT latest; do
82573d2ad6SVladimir Kotal		echo "Pushing docker image for tag $tag"
83573d2ad6SVladimir Kotal		docker push opengrok/docker:$tag
84573d2ad6SVladimir Kotal	done
85675e1740SVladimir Kotalfi
86