1
+ #! /bin/bash
2
+
3
+ #
4
+ # Build and optionally push new image to Docker hub.
5
+ #
6
+ # When pushing, this script uses the following secure variables:
7
+ # - DOCKER_USERNAME
8
+ # - DOCKER_PASSWORD
9
+ #
10
+ # These are set via https://github.com/oracle/opengrok/settings/secrets
11
+ #
12
+
13
+ set -e
14
+
15
+ echo " Running linter"
16
+ docker run --rm -i hadolint/hadolint:2.6.0 < Dockerfile || exit 1
17
+
18
+ API_URL=" https://hub.docker.com/v2"
19
+ IMAGE=" opengrok/docker"
20
+
21
+ if [[ -n $OPENGROK_REF && $OPENGROK_REF == refs/tags/* ]]; then
22
+ OPENGROK_TAG=${OPENGROK_REF# " refs/tags/" }
23
+ fi
24
+
25
+ if [[ -n $OPENGROK_TAG ]]; then
26
+ VERSION=" $OPENGROK_TAG "
27
+ VERSION_SHORT=$( echo $VERSION | cut -d. -f1,2 )
28
+
29
+ if [[ -z $VERSION ]]; then
30
+ echo " empty VERSION"
31
+ exit 1
32
+ fi
33
+
34
+ if [[ -z $VERSION_SHORT ]]; then
35
+ echo " empty VERSION_SHORT"
36
+ exit 1
37
+ fi
38
+
39
+ echo " Version: $VERSION "
40
+ echo " Short version: $VERSION_SHORT "
41
+
42
+ TAGS=" $VERSION $VERSION_SHORT latest"
43
+
44
+ echo " Building docker image for release ($TAGS )"
45
+ docker build \
46
+ -t $IMAGE :$VERSION \
47
+ -t $IMAGE :$VERSION_SHORT \
48
+ -t $IMAGE :latest .
49
+ else
50
+ TAGS=" master"
51
+
52
+ echo " Building docker image for master"
53
+ docker build -t $IMAGE :master .
54
+ fi
55
+
56
+ #
57
+ # Run the image in a container. This is not strictly needed however
58
+ # serves as additional test in automatic builds.
59
+ #
60
+ echo " Running the image in container"
61
+ docker run -d $IMAGE
62
+ docker ps -a
63
+
64
+ # This can only work on home repository since it needs encrypted variables.
65
+ if [[ -n " $OPENGROK_PULL_REQUEST " ]]; then
66
+ echo " Not pushing Docker image for pull requests"
67
+ exit 0
68
+ fi
69
+
70
+ # The push only works on the main repository.
71
+ if [[ " $OPENGROK_REPO_SLUG " != " oracle/opengrok" ]]; then
72
+ echo " Not pushing Docker image for non main repository"
73
+ exit 0
74
+ fi
75
+
76
+ if [[ -z $DOCKER_USERNAME ]]; then
77
+ echo " DOCKER_USERNAME is empty, exiting"
78
+ exit 0
79
+ fi
80
+
81
+ if [[ -z $DOCKER_PASSWORD ]]; then
82
+ echo " DOCKER_PASSWORD is empty, exiting"
83
+ exit 0
84
+ fi
85
+
86
+ # Publish the image to Docker hub.
87
+ if [ -n " $DOCKER_PASSWORD " -a -n " $DOCKER_USERNAME " -a -n " $TAGS " ]; then
88
+ echo " Logging into Docker Hub"
89
+ echo " $DOCKER_PASSWORD " | docker login -u " $DOCKER_USERNAME " --password-stdin
90
+
91
+ # All the tags need to be pushed individually:
92
+ for tag in $TAGS ; do
93
+ echo " Pushing Docker image for tag $tag "
94
+ docker push $IMAGE :$tag
95
+ done
96
+ fi
0 commit comments