xref: /OpenGrok/dev/docker.sh (revision 573d2ad67db24b082da4604c95627cce6005eacc)
1675e1740SVladimir Kotal#!/bin/bash
2675e1740SVladimir Kotal
3675e1740SVladimir Kotal#
4675e1740SVladimir Kotal# Build and push new image to Docker hub.
5675e1740SVladimir Kotal#
6675e1740SVladimir Kotal# 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
16675e1740SVladimir Kotal# Travis can only work on master since it needs encrypted variables.
17675e1740SVladimir Kotalif [ "${TRAVIS_PULL_REQUEST}" != "false" ]; then
1871bb21afSVladimir Kotal	echo "Not building docker image for pull requests"
19675e1740SVladimir Kotal	exit 0
20675e1740SVladimir Kotalfi
21675e1740SVladimir Kotal
22f9bac693SVladimir Kotal# Allow Docker builds for release builds only.
23f9bac693SVladimir Kotalif [[ -z $TRAVIS_TAG ]]; then
2471bb21afSVladimir Kotal	echo "TRAVIS_TAG is empty"
25b505526fSVladimir Kotal	exit 0
26f9bac693SVladimir Kotalfi
27f9bac693SVladimir Kotal
2871bb21afSVladimir Kotalif [[ -z $DOCKER_USERNAME ]]; then
2971bb21afSVladimir Kotal	echo "DOCKER_USERNAME is empty"
3071bb21afSVladimir Kotal	exit 1
3171bb21afSVladimir Kotalfi
3271bb21afSVladimir Kotal
3371bb21afSVladimir Kotalif [[ -z $DOCKER_PASSWORD ]]; then
3471bb21afSVladimir Kotal	echo "DOCKER_PASSWORD is empty"
3571bb21afSVladimir Kotal	exit 1
3671bb21afSVladimir Kotalfi
3771bb21afSVladimir Kotal
38f9bac693SVladimir KotalVERSION="$TRAVIS_TAG"
39*573d2ad6SVladimir KotalVERSION_SHORT=$( echo $VERSION | cut -d. -f1,2 )
40*573d2ad6SVladimir Kotal
41*573d2ad6SVladimir Kotalif [[ -z $VERSION ]]; then
42*573d2ad6SVladimir Kotal	echo "empty VERSION"
43*573d2ad6SVladimir Kotal	exit 1
44*573d2ad6SVladimir Kotalfi
45*573d2ad6SVladimir Kotal
46*573d2ad6SVladimir Kotalif [[ -z $VERSION_SHORT ]]; then
47*573d2ad6SVladimir Kotal	echo "empty VERSION_SHORT"
48*573d2ad6SVladimir Kotal	exit 1
49*573d2ad6SVladimir Kotalfi
50f9bac693SVladimir Kotal
51c0e56161SVladimir Kotal# Build the image.
52*573d2ad6SVladimir Kotaldocker build \
53*573d2ad6SVladimir Kotal    -t opengrok/docker:$VERSION \
54*573d2ad6SVladimir Kotal    -t opengrok/docker:$VERSION_SHORT \
55*573d2ad6SVladimir Kotal    -t opengrok/docker:latest .
56*573d2ad6SVladimir Kotal
57c0e56161SVladimir Kotal#
58c0e56161SVladimir Kotal# Run the image in container. This is not strictly needed however
59c0e56161SVladimir Kotal# serves as additional test in automatic builds.
60c0e56161SVladimir Kotal#
61675e1740SVladimir Kotaldocker run -d opengrok/docker
62675e1740SVladimir Kotaldocker ps -a
63675e1740SVladimir Kotal
64675e1740SVladimir Kotal# Publish the image to Docker hub.
65675e1740SVladimir Kotalif [ -n "$DOCKER_PASSWORD" -a -n "$DOCKER_USERNAME" -a -n "$VERSION" ]; then
66c0e56161SVladimir Kotal	echo "Logging into docker"
67675e1740SVladimir Kotal	echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin
68c0e56161SVladimir Kotal
69c0e56161SVladimir Kotal	# All the tags need to be pushed individually:
70*573d2ad6SVladimir Kotal	for tag in $VERSION $VERSION_SHORT latest; do
71*573d2ad6SVladimir Kotal		echo "Pushing docker image for tag $tag"
72*573d2ad6SVladimir Kotal		docker push opengrok/docker:$tag
73*573d2ad6SVladimir Kotal	done
74675e1740SVladimir Kotalfi
75