forked from broadinstitute/docker-gradle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker-build.sh
executable file
·180 lines (144 loc) · 5.89 KB
/
docker-build.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/bin/bash
# this program will read a list of gradle versions from a build config
# file. For each version it will determine if a version already exists on
# docker hub for that version. If it does it skips that version and
# only builds versions that do not already exist on docker hub.
# this script will build both the Oracle and openJDK versions in order for
# us to validate that both versions pass our tests.
# A FORCE env var can be set to 1 (one) in order to force the building of
# all versions of gradle regardless if they already exist on dockerhub.
# This is useful to ensure the docker image has the newest JDK version
# since currently only the major version (8) is specified for JDK and the
# script will get what ever the latest version 8 of JDK exists at time of build
# force build flag
FORCE=${FORCE:-0}
# init vars
config_file="build-list.cfg"
config_ok=0
# flag to determine if new docker image should be built
build_oracle=0
build_openjdk=0
# Generic error outputting function
errorout() {
if [ $1 -ne 0 ];
then
echo "${2}"
exit $1
fi
}
if [ -f "${config_file}" ]
then
# check if any valid entries in config file
# valid entries have a valid alphanumeric valut in first column
if egrep -q "^[a-zA-Z0-9]+" ${config_file}
then
config_ok=1
else
errorout 1 "No valid entries in config file: ${config_file}"
fi
else
errorout 1 "Missing build list config file: ${config_file}"
fi
egrep "^[a-zA-Z0-9]+" ${config_file} | while read version rest
do
# ensure Dockerfile does not exist
rm -f Dockerfile
echo "Checking if gradle version (${version}) needs to be built..."
# initialize flag for this version based on FORCE_BUILD value
build_openjdk=${FORCE}
build_oracle=${FORCE}
# do not both checking if docker image exists if we are forcing a build
if [ "${FORCE}" = 0 ]
then
echo "Checking for existence of previous build docker (broadinstitute/gradle:oracle-${version})..."
# see if version exists on docker hub
docker pull broadinstitute/gradle:oracle-${version}
retcode=$?
if [ "${retcode}" -ne "0" ]
then
echo "Need to build Oracle version"
build_oracle=1
fi
echo "Checking for existence of previous build docker (broadinstitute/gradle:openjdk-${version})..."
# see if version exists on docker hub
docker pull broadinstitute/gradle:openjdk-${version}
retcode=$?
if [ "${retcode}" -ne "0" ]
then
echo "Need to build OpenJDK version "
build_openjdk=1
fi
fi
if [ "${build_oracle}" -eq "1" ]
then
echo "Building docker (broadinstitute/gradle:oracle-${version})"
# create Dockerfile from template
sed -e "s;GRADLE_NUMBER;${version};" < Dockerfile.oracle > Dockerfile
# add some Jenkins labels to designate this build
echo "LABEL GIT_BRANCH=${GIT_BRANCH}" >> Dockerfile
echo "LABEL GIT_COMMIT=${GIT_COMMIT}" >> Dockerfile
echo "LABEL BUILD_URL=${BUILD_URL}" >> Dockerfile
docker build -t broadinstitute/gradle:oracle-${version} .
retcode=$?
errorout $retcode "ERROR: Build failed!"
echo "Tagging docker (broadinstitute/gradle:oracle-${version}_${BUILD_NUMBER})"
docker tag broadinstitute/gradle:oracle-${version} broadinstitute/gradle:oracle-${version}_${BUILD_NUMBER}
retcode=$?
errorout $retcode "Build successful but could not tag to build number"
echo "Pushing docker (broadinstitute/gradle:oracle-${version})"
docker push broadinstitute/gradle:oracle-${version}
echo "Pushing images to dockerhub"
retcode=$?
errorout $retcode "Pushing new image to docker hub"
echo "Pushing docker (broadinstitute/gradle:oracle-${version}_${BUILD_NUMBER})"
docker push broadinstitute/gradle:oracle-${version}_${BUILD_NUMBER}
retcode=$?
errorout $retcode "Pushing build_number tag image to docker hub"
# clean up all built and pulled images
cleancode=0
echo "Cleaning up pulled and built images"
docker rmi broadinstitute/gradle:oracle-${version}
retcode=$?
cleancode=$(($cleancode + $retcode))
docker rmi broadinstitute/oracle-${version}_${BUILD_NUMBER}
retcode=$?
errorout $cleancode "Some images were not able to be cleaned up"
fi
if [ "${build_openjdk}" -eq "1" ]
then
echo "Building docker (broadinstitute/gradle:openjdk-${version})"
# create Dockerfile from template
sed -e "s;GRADLE_NUMBER;${version};" < Dockerfile.openjdk > Dockerfile
# add some Jenkins labels to designate this build
echo "LABEL GIT_BRANCH=${GIT_BRANCH}" >> Dockerfile
echo "LABEL GIT_COMMIT=${GIT_COMMIT}" >> Dockerfile
echo "LABEL BUILD_URL=${BUILD_URL}" >> Dockerfile
docker build -t broadinstitute/gradle:openjdk-${version} .
retcode=$?
errorout $retcode "ERROR: Build failed!"
echo "Tagging docker (broadinstitute/gradle:oracle-${version}_${BUILD_NUMBER})"
docker tag broadinstitute/gradle:openjdk-${version} broadinstitute/gradle:openjdk-${version}_${BUILD_NUMBER}
retcode=$?
errorout $retcode "Build successful but could not tag to build number"
echo "Pushing docker (broadinstitute/gradle:oracle-${version})"
docker push broadinstitute/gradle:openjdk-${version}
echo "Pushing images to dockerhub"
retcode=$?
errorout $retcode "Pushing new image to docker hub"
echo "Pushing docker (broadinstitute/gradle:oracle-${version}_${BUILD_NUMBER})"
docker push broadinstitute/gradle:openjdk-${version}_${BUILD_NUMBER}
retcode=$?
errorout $retcode "Pushing build_number tag image to docker hub"
# clean up all built and pulled images
cleancode=0
echo "Cleaning up pulled and built images"
docker rmi broadinstitute/gradle:openjdk-${version}
retcode=$?
cleancode=$(($cleancode + $retcode))
docker rmi broadinstitute/openjdk-${version}_${BUILD_NUMBER}
retcode=$?
errorout $cleancode "Some images were not able to be cleaned up"
fi
done
test -f Dockerfile && rm -f Dockerfile
exit 0