-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild
executable file
·231 lines (185 loc) · 7.62 KB
/
build
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/bin/sh
set -e
if ! test -r ./dockerhub-userdata ; then
cat >./dockerhub-userdata <<-'USERDATA'
RUN_BUILD=y
RUN_TESTS=y
# change RUN_PUSH=n to RUN_PUSH=y if you want to push and tag the image up to docker hub or quay or whereever
RUN_PUSH=n
# Your user/organization account information used to log into dockerhub.io or quay.io or whatever registry service you are using
USERNAME=
PASSWORD=
REPOPREFIX=${USERNAME:-local}
USERDATA
echo 'Generated an empty dockerhub-userdata template. Please fill out the information in dockerhub-userdata if you want to push the images or re-run to just build locally'
exit 1
fi
if ! command -v docker ; then
echo 'Need the docker command. Please install packages to provide it' 1>&2
exit 1
fi
if ! command -v jq ; then
echo 'Need the jq command. Please install packages to provide it' 1>&2
exit 1
fi
. ./dockerhub-userdata
echo '+ . ./dockerhub-userdata'
set -x
if test "${RUN_PUSH:-n}" = "y" ; then
# I predict that accepting password as an argument will eventually be deprecated
if ! printf '%s\n' "$PASSWORD" | docker login --password-stdin -u "$USERNAME" ; then
echo 'Failed to log into the docker server.' 1>&2
exit 1
fi
printf "\n\n\e[0;32mLogin to your docker repo account was successful\e(B\e[m\n"
fi
# Try lts-alpine-jdk7 or lts-alpine-jdk11 or lts-alpine-jdk17
jenkinsBaseImage="${JENKINS_BASE_IMAGE:-lts-alpine-jdk11}"
jenkinsNameSuffix="${JENKINS_NAME_SUFFIX:-jdk11}"
#mkdir -p .auto-gen
#cat /etc/localtime > .auto-gen/localtime
#cat /etc/timezone > .auto-gen/timezone
#docker build --no-rm --build-arg TARGETARCH=amd64 -f Dockerfile -t local/docker_jenkins:alpine3.15 .
printf '\n\n\e[0;32mGathering the latest version of docker...\e(B\e[m\n'
latestDockerVersion="$(curl -sSL 'https://download.docker.com/linux/static/stable/x86_64/' | grep -oE 'docker-[0-9][0-9]*[.][0-9][0-9]*[.][0-9][0-9]*[.]tgz' | sort -rh | grep -m1 -oE '[0-9][0-9]*[.][0-9][0-9]*[.][0-9][0-9]*')"
if test -z "$latestDockerVersion" ; then
echo 'ERR: Could not gather what the latest version of docker is!'
exit 1
fi
printf '\n\n\e[0;32mGathering the latest version of docker-compose...\e(B\e[m\n'
latestComposeVersion="$(curl -sSL 'https://api.github.com/repos/docker/compose/releases/latest' | jq -r ".tag_name")"
if test -z "$latestComposeVersion" ; then
echo 'ERR: Could not gather what the latest version of docker-compose is!'
exit 1
fi
# needed for some tests and tagging. The "cut" is to get rid of the "v". If latestComposeVersion=v1.2.3 then cutComposeVersion=1.2.3
export cutComposeVersion
cutComposeVersion="$( echo "$latestComposeVersion" | cut -c2- )"
if test "${RUN_BUILD:-y}" = "y" ; then
printf '\n\n\e[0;32mNow building...\e(B\e[m\n'
docker build --pull --compress --build-arg "DOCKER_VERSION=$latestDockerVersion" --build-arg "JENKINS_BASE_IMAGE=$jenkinsBaseImage" --build-arg "DOCKER_COMPOSE_VERSION=$latestComposeVersion" -f docker-resources/Dockerfile -t local/docker-jenkins-alpine docker-resources
printf '\n\n\e[0;32mBuild was successful\e(B\e[m\n'
else
echo 'Build was skipped.'
fi
if test "${RUN_TESTS:-y}" = "y" ; then
printf '\n\n\e[0;32mNow testing...\e(B\e[m\n'
containerId="$( docker run --rm --detach --privileged --env "DEBUG_KILLAFTER_SEC=${DEBUG_KILLAFTER_SEC:-180}" --cpuset-cpus=1,3,5,7 --cpu-period=10000 local/docker-jenkins-alpine )"
# if it can't boot up in under a minute, then something is seriously wrong
sleep ${BUILD_SLEEP_SEC:-60}
for test in ./tests/*
do
printf '\n\e[0;32m> Running test %s\e(B\e[m\n' "$test"
$test "$containerId"
done
# All tests passed and the remaining container is of no use to us
docker kill "$containerId"
printf "\n\n\e[0;32mAll tested have passed! Now pushing up and tagging the image appropriately...\e(B\e[m\n"
else
echo 'Tests were skipped.'
fi
# next, extract info for tagging and push it up to dockerhub
set +ex
# the echo feedback would get pretty annoying after this point
if ! test "${RUN_PUSH:-n}" = "y" ; then
echo
echo 'BUILD WAS SUCCESSFUL! The resulting image is local/docker-jenkins-alpine. Usage example:'
echo ' docker run --rm -it --name jenkins_docker --privileged -p 8080:8080 -v "$PWD/certs/:/certs/:rw" -v "$PWD/var-lib-docker/:/var/lib/docker/:rw" -v "$PWD/jenkins_home/:/var/jenkins_home/:rw" -v "$PWD/var-cache-apk:/var/cache/apk:rw" local/docker-jenkins-alpine'
echo
exit 0
fi
printf '\n\n\e[0;32mNow pushing up online...\e(B\e[m\n'
dockerMinorVersion="${latestDockerVersion%.*}"
dockerMajorVersion="${dockerMinorVersion%.*}"
composeMinorVersion="${cutComposeVersion%.*}"
composeMajorVersion="${composeMinorVersion%.*}"
jenkinsRevisionVersion="$(docker inspect local/docker-jenkins-alpine | jq -r '.[0].ContainerConfig.Labels."org.opencontainers.image.version"')"
jenkinsMinorVersion="${jenkinsRevisionVersion%.*}"
jenkinsMajorVersion="${jenkinsMinorVersion%.*}"
if test "$jenkinsRevisionVersion" = "null" ; then
echo 'ERR: could not gather what the version of jenkins is. Aborting!' 1>&2
exit 1
fi
if printf '%q' test 1>/dev/null 2>&1 ; then
alias my_posix_printf=printf
elif /usr/bin/printf '%q' test 1>/dev/null 2>&1 ; then
alias my_posix_printf=/usr/bin/printf
else
my_posix_printf() {
format="$1"
shift
printf "$(echo "$format" | sed "s/%q/'%s'/")" "$@"
}
fi
echoback() {
my_posix_printf '$ '
my_posix_printf '%q ' "$@"
echo
"$@"
}
tagAndPushAndRmImage() (
set -e
echoback docker tag local/docker-jenkins-alpine "$1"
while true ; do
if echoback timeout 500 docker push "$1" ; then
break
elif test $? -ne 124 ; then
# if it timed out, then the exit code is 124
exit 0
fi
done
echoback docker image rm "$1"
)
if test -z "$REPOPREFIX" ; then
REPOPREFIXWITHSLASH=''
else
REPOPREFIXWITHSLASH="$REPOPREFIX/"
fi
forEachTag() {
for compVer in "compose$cutComposeVersion-" "compose$composeMinorVersion-" "compose$composeMajorVersion-" ','
do
if test -z "$compVer" ; then
# sanity check
continue
fi
for dockVer in "docker$latestDockerVersion-" "docker$dockerMinorVersion-" "docker$dockerMajorVersion-" ','
do
if test -z "$dockVer" ; then
# sanity check
continue
fi
for jenkVer in "jenkins$jenkinsRevisionVersion-" "jenkins$jenkinsMinorVersion-" "jenkins$jenkinsMajorVersion-" ','
do
if test -z "$jenkVer" ; then
# sanity check
continue
fi
nextTag="$REPOPREFIXWITHSLASH""docker-jenkins-alpine:${compVer%,}${dockVer%,}${jenkVer%,}$jenkinsNameSuffix"
if test "$3" = "tag" ; then
echo "STATUS: aliasing $nextTag -> local/docker-jenkins-alpine" 1>&2
fi
"$@" "$nextTag"
done
done
done
}
#forEachTag tagAndPushAndRmImage
# clear out any previously existing images
#docker image ls docker-jenkins-alpine --format '{{print .Tag}}' | xargs printf 'docker-jenkins-alpine:%s\n' | xargs docker rmi
echoback docker rmi $(docker image ls "$REPOPREFIXWITHSLASH""docker-jenkins-alpine" --format '{{print .Tag}}' | xargs printf "$REPOPREFIXWITHSLASH""docker-jenkins-alpine"':%s\n')
forEachTag echoback docker tag local/docker-jenkins-alpine
while true ; do
#if echoback docker push $( forEachTag echo ) ; then
# if its getting stuck, then add --max-concurrent-uploads=1 to the Start parameter of /lib/systemd/system/docker.service
if echoback docker push --all-tags "$REPOPREFIXWITHSLASH""docker-jenkins-alpine" ; then
break
elif test $? -ne 124 ; then
# if it timed out, then the exit code is 124
kill $$
fi
done
echoback docker image rm -f $(forEachTag echo)
echo
echo 'STATUS: all tags pushed up and done!'
echo
#echo 'All tags pushed up. Now cleaning the tagged images and pruning...'