This repository has been archived by the owner on May 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrebuild-HEAD.sh
executable file
·233 lines (207 loc) · 7.58 KB
/
rebuild-HEAD.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
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
231
232
233
#!/usr/bin/env bash
fatal()
{
echo "fatal: $1" 1>&2
exit 1
}
usage()
{
echo "usage: $0 [-r] <file.buildspec>
-r: rebuild also latest release" 1>&2
exit 1
}
rebuildLatest='false'
while getopts ":r" option; do
case "${option}" in
r)
rebuildLatest='true'
;;
*)
usage
;;
esac
done
shift $((OPTIND-1))
buildspec=$1
[ -z "${buildspec}" ] && usage
echo "Rebuilding from spec ${buildspec}"
. ${buildspec} || fatal "could not source ${buildspec}"
echo "- ${groupId}:${artifactId}"
echo "- gitRepo: ${gitRepo}"
echo "- jdk: ${jdk}"
echo "- command: ${command}"
echo "- buildinfo: ${buildinfo}"
base="$PWD"
pushd `dirname ${buildspec}` >/dev/null || fatal "could not move into ${buildspec}"
# prepare source, using provided Git repository
[ -d buildcache ] || mkdir buildcache
cd buildcache
[ -d ${artifactId} ] || git clone ${gitRepo} ${artifactId} || fatal "failed to clone ${artifactId}"
cd ${artifactId}
git checkout master || fatal "failed to git checkout master"
git pull || fatal "failed to git pull"
pwd
# the effective rebuild command for latest, adding artifact:buildinfo goal to compare with central content
mvn_rebuild_latest="${command} -V -e artifact:buildinfo -Dreference.repo=central -Dreference.compare.save -Dbuildinfo.reproducible"
# the effective rebuild commands for master HEAD, adding artifact:buildinfo goal and install on first run to compare on second
mvn_rebuild_1="${command} -V -e install:install artifact:buildinfo"
mvn_rebuild_2="${command} -V -e artifact:buildinfo -Dreference.repo=central -Dreference.compare.save -Dbuildinfo.reproducible"
mvnBuildDocker() {
local mvnCommand mvnImage crlfDocker
mvnCommand="$1"
crlfDocker="no"
# select Docker image to match required JDK version: https://hub.docker.com/_/maven
case ${jdk} in
6 | 7)
mvnImage=maven:3.6.1-jdk-${jdk}-alpine
crlfDocker="yes"
;;
8)
mvnImage=maven:3.6.3-jdk-${jdk}-slim
crlfDocker="yes"
;;
9)
mvnImage=maven:3-jdk-${jdk}-slim
;;
14)
mvnImage=maven:3.6.3-jdk-${jdk}
;;
15 | 16 | 17)
mvnImage=maven:3.6.3-openjdk-${jdk}-slim
;;
*)
mvnImage=maven:3.6.3-jdk-${jdk}-slim
esac
echo "Rebuilding using Docker image ${mvnImage}"
local docker_command="docker run -it --rm --name rebuild-central -v $PWD:/var/maven/app -v $base:/var/maven/.m2 -v $base/.npm:/.npm -u $(id -u ${USER}):$(id -g ${USER}) -e MAVEN_CONFIG=/var/maven/.m2 -w /var/maven/app"
local mvn_docker_params="-Duser.home=/var/maven"
if [[ "${newline}" == crlf* ]]
then
if [[ "${crlfDocker}" == "yes" ]]
then
echo -e "\033[2m${docker_command} ${mvnImage} \033[1m${mvnCommand} ${mvn_docker_params} -Dline.separator=\$'\\\\r\\\\n'\033[0m"
${docker_command} ${mvnImage} ${mvnCommand} ${mvn_docker_params} -Dline.separator=$'\r\n'
else
mvnCommand="$(echo "${mvnCommand}" | sed "s_^mvn _/var/maven/.m2/mvncrlf _")"
echo -e "\033[2m${docker_command} ${mvnImage} \033[1m${mvnCommand} ${mvn_docker_params}\033[0m"
${docker_command} ${mvnImage} ${mvnCommand} ${mvn_docker_params}
fi
else
echo -e "\033[2m${docker_command} ${mvnImage} \033[1m${mvnCommand} ${mvn_docker_params}\033[0m"
${docker_command} ${mvnImage} ${mvnCommand} ${mvn_docker_params}
fi
}
# TODO not tested
mvnBuildLocal() {
local mvnCommand="$1"
echo "Rebuilding using local JDK ${jdk}"
# TODO need to define settings with ${base}/repository local repository to avoid mixing reproducible-central dependencies with day to day builds
if [[ "${newline}" == crlf* ]]
then
${mvnCommand} -Dline.separator=$'\r\n'
else
${mvnCommand}
fi
}
# by default, build with Docker
# TODO: on parameter, use instead mvnBuildLocal after selecting JDK
# jenv shell ${jdk}
# sdk use java ${jdk}
if ${rebuildLatest}
then
echo "******************************************************"
echo "* rebuilding latest release and comparing to central *"
echo "******************************************************"
# git checkout latest tag then rebuild latest release
if [ -z "${latest}" ]
then
# auto-detect last Git tag
gitTag="`git describe --abbrev=0`"
version="${gitTag}"
echo "last Git tag is ${gitTag}"
else
version="${latest}"
echo "configured latest is ${latest} with Git tag ${gitTag}"
fi
echo -e "\033[1mgit checkout ${version}\033[0m"
git checkout ${version} || fatal "failed to git checkout latest ${version}"
if [ "${newline}" == "crlf" ]
then
echo "converting newlines to crlf"
git ls-files --eol | grep w/lf | cut -c 40- | xargs -d '\n' unix2dos
fi
mvnBuildDocker "${mvn_rebuild_latest}" || fatal "failed to build latest"
git reset --hard
dos2unix ${buildinfo}* || fatal "failed to convert buildinfo newlines"
sed -i 's/\(reference_[^=]*\)=\([^"].*\)/\1="\2"/' ${buildinfo}*.compare # waiting for MARTIFACT-19
cp ${buildinfo}* ../.. || fatal "failed to copy buildinfo artifacts latest ${version}"
. ${buildinfo}*.compare
if [[ ${ko} > 0 ]]
then
echo -e " ok=${ok}"
echo -e " okFiles=\"${okFiles}\""
echo -e " \033[31;1mko=${ko}\033[0m"
echo -e " koFiles=\"${koFiles}\""
if [ -n "${reference_java_version}" ]
then
echo -e " check .buildspec \033[1mjdk=${jdk}\033[0m vs reference \033[1mjava.version=${reference_java_version}\033[0m"
fi
if [ -n "${reference_os_name}" ]
then
echo -e " check .buildspec \033[1mnewline=${newline}\033[0m vs reference \033[1mos.name=${reference_os_name}\033[0m"
fi
else
echo -e " \033[32;1mok=${ok}\033[0m"
echo -e " okFiles=\"${okFiles}\""
fi
fi
# work on master HEAD
git checkout master || fatal "failed to git checkout master"
currentCommit="`git rev-parse HEAD`"
prevCommitFile="../`basename $(pwd)`.HEAD"
if [ "${currentCommit}" == "`cat ${prevCommitFile}`" ]
then
echo "*******************************************"
echo "* no new commit on HEAD, skipping rebuild *"
echo "*******************************************"
echo "$(pwd).HEAD"
else
echo "*******************************************************"
echo "* rebuilding master HEAD SNAPSHOT twice and comparing *"
echo "*******************************************************"
if [ "${newline}" == "crlf" ]
then
echo "converting newlines to crlf"
git ls-files --eol | grep w/lf | cut -c 40- | xargs -d '\n' unix2dos
fi
# rebuild HEAD SNAPSHOT twice
mvnBuildDocker "${mvn_rebuild_1}" || fatal "failed to build first time"
mvnBuildDocker "${mvn_rebuild_2}" || fatal "failed to build second time"
git reset --hard
dos2unix ${buildinfo}* || fatal "failed to convert buildinfo newlines"
sed -i 's/\(reference_[^=]*\)=\([^"].*\)/\1="\2"/' ${buildinfo}*.compare # waiting for MARTIFACT-19
cp ${buildinfo}* ../.. || fatal "failed to copy buildinfo artifacts HEAD"
# TODO detect if buildinfo.commit has changed: if not, restore previous buildinfo since update is mostly noise
. ${buildinfo}*.compare
if [[ ${ko} > 0 ]]
then
echo -e " ok=${ok}"
echo -e " okFiles=\"${okFiles}\""
echo -e " \033[31;1mko=${ko}\033[0m"
echo -e " koFiles=\"${koFiles}\""
if [ -n "${reference_java_version}" ]
then
echo -e " check .buildspec \033[1mjdk=${jdk}\033[0m vs reference \033[1mjava.version=${reference_java_version}\033[0m"
fi
if [ -n "${reference_os_name}" ]
then
echo -e " check .buildspec \033[1mnewline=${newline}\033[0m vs reference \033[1mos.name=${reference_os_name}\033[0m"
fi
else
echo -e " \033[32;1mok=${ok}\033[0m"
echo -e " okFiles=\"${okFiles}\""
fi
echo -n "${currentCommit}" > ${prevCommitFile}
fi
echo
popd > /dev/null