-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.sh
executable file
·363 lines (333 loc) · 10.1 KB
/
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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#!/usr/bin/env bash
set -euo pipefail
usage() {
cat >/dev/stderr <<EOF
$0 [options] <command>
Perform a Maven build suitable for automation in the Lighthouse environment.
Automatically configures Maven to use GitHub artifacts
Options
--github-username <user>
The user name for GitHub
--github-token <token>
The token for the GitHub user
--initialize-build <file>
The name of script to run prior to building with Maven.
By default, this is initialize-build.sh
If this file exists, it will be executed.
Commands
non-release
Perform a basic mvn install.
release
Perform a mvn deploy using the release profile.
${1:-}
EOF
exit 1
}
INITIALIZE_BUILD="initialize-build.sh"
init() {
if [ "${DEBUG:=false}" == "true" ]; then set -x; fi
requireOpt github-username GITHUB_USERNAME
requireOpt github-token GITHUB_TOKEN
git config user.name libertybot
git config user.email "<none>"
git remote --verbose
export GH_TOKEN="${GITHUB_TOKEN}"
}
main() {
local args
local longOpts="debug,github-username:,github-token:,initialize-build:"
local shortOpts=""
if ! args=$(getopt -l "$longOpts" -o "$shortOpts" -- "$@"); then usage; fi
eval set -- "$args"
while true
do
case "$1" in
--debug) DEBUG=true;;
--github-username) GITHUB_USERNAME="$2";;
--github-token) GITHUB_TOKEN="$2";;
--initialize-build) INITIALIZE_BUILD="$2";;
--) shift; break;;
esac
shift
done
init
if [ $# == 1 ]; then COMMAND="$1"; shift; fi
if [ -z "${COMMAND:-}" ]; then usage "Command must be specified"; fi
MVN_ARGS="$@"
case $COMMAND in
non-release) nonReleaseBuild;;
release) releaseBuild;;
*) usage "unknown command: $COMMAND";;
esac
}
commitNextSnapshot() {
mvn $MVN_ARGS versions:set -DprocessAllModules=true -DgenerateBackupPoms=false -DnextSnapshot=true
local snapshotVersion
snapshotVersion=$(mvn ${MVN_ARGS} -N -q org.codehaus.mojo:exec-maven-plugin:exec \
-Dexec.executable='echo' \
-Dexec.args='${project.version}')
git diff
git add $(git status -s | grep "^ M" | cut -c4-)
local message
message="Next snapshot ${snapshotVersion}"
git commit -m "${message}"
# Need this for the failed merge message
export NEXT_VERSION=${snapshotVersion:-unknown}
}
commitReleaseVersion() {
local releaseVersion="${1:-}"
if [ -z "${releaseVersion:-}" ]
then
log "Release version could not be determined." "ERROR"
exit 1
fi
git diff
git add $(git status -s | grep "^ M" | cut -c4-)
local message
message="Release ${releaseVersion} - GitHub Workflow: ${GITHUB_WORKFLOW} ${GITHUB_RUN_ID}"
git commit -m "${message}"
git tag --force -m "${message}" ${releaseVersion}
}
configureSettings() {
SETTINGS=$(mktemp settings.xml.XXXX)
trap "rm $SETTINGS" EXIT
defaultSettings>$SETTINGS
}
createGitHubRelease() {
local releaseVersion="${1:-}"
log "Creating Release ${releaseVersion} in GitHub"
local tagRange
tagRange=$(git tag \
| grep -E '[0-9]+\.[0-9]+\.[0-9]' \
| sort --reverse --version-sort \
| head -2 \
| paste -sd : \
| sed 's/:/.../')
local commitHistory=$(mktemp)
git log --format=format:'COMMIT: %s%n%b' \
--invert-grep \
--grep='Next snapshot' \
--grep='Merge branch' \
--grep='REBUILD REQUIRED' \
"${tagRange}" \
| sed -e 's/^ *\* \+//' \
| awk '/COMMIT: Release .*/ {$1="";print;next}
/COMMIT:/ {$1="";printf "-";print;next}
/[a-z]/ {printf " - ";print}' \
| tee ${commitHistory}
gh release create ${releaseVersion} \
--verify-tag \
--title "Release ${releaseVersion}" \
--notes-file ${commitHistory}
}
defaultSettings() {
cat<<EOF
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<servers>
<server>
<id>github</id>
<username>\${github.username}</username>
<password>\${github.token}</password>
</server>
</servers>
<profiles>
<profile>
<id>github</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<repositories>
<repository>
<id>github</id>
<url>https://maven.pkg.github.com/department-of-veterans-affairs/all</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>github</id>
<url>https://maven.pkg.github.com/department-of-veterans-affairs/all</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
<!-- Add last to access first -->
<profile>
<id>central-evil-twin</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<repositories>
<repository>
<id>central-evil-twin</id>
<url>https://repo.maven.apache.org/maven2</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>central-evil-twin</id>
<url>https://repo.maven.apache.org/maven2</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
</settings>
EOF
}
isJavaVersionSupported() {
if [ -z "${JAVA_VERSION:-}" ]
then
log "Cannot determine containers java version, assuming it's supported." "WARN"
return 0
else
log "Found installed java version: ${JAVA_VERSION}"
fi
local desiredVersion=$(mvn ${MVN_ARGS} -N -q org.codehaus.mojo:exec-maven-plugin:exec \
-Dexec.executable='echo' \
-Dexec.args='${java.version}')
if [ -z "${desiredVersion:-}" ]
then
log "Cannot determine compiler target version, assuming it's supported." "WARN"
return 0
fi
local javaMajorVersion=
javaMajorVersion=$(echo ${JAVA_VERSION} | cut -d '.' -f 1)
if [ "${javaMajorVersion}" != "${desiredVersion%%.*}" ]
then
log "Container java version '${javaMajorVersion}' does not match desired java version '${desiredVersion%%.*}' (${desiredVersion}). Aborting build..." "ERROR"
exit 1
fi
return 0
}
log() {
local message="${1}"
local logLevel="${2:-INFO}"
echo "[${logLevel}] ${message}"
}
mergeMainBranch() {
local pullResult
set +e
pullResult=$(git pull --no-edit --no-ff --no-tags)
if [ $? != 0 ]
then
cat <<EOF
+----------------------------------------------------------+
| OH NOES! |
+----------------------------------------------------------+
| |
| CHANGES HAVE BEEN MADE SINCE THIS BUILD STARTED THAT |
| CANNOT BE AUTOMATICALLY MERGED. |
| |
| WE CANNOT PUSH TAGS OR CHANGES TO GITHUB. IF AN |
| ARTIFACT WAS DEPLOYED TO AN ARTIFACT SERVER, YOU WILL |
| NEED TO REMOVE IT, OVERWRITE IT, OR MANUALLY UPDATE |
| THE MAVEN VERSION TO SKIP PAST THIS FAILED BUILD. |
| USE ${NEXT_VERSION} OR LATER. |
| |
+----------------------------------------------------------+
EOF
exit 1
fi
pullResult="${pullResult,,}"
pullResult="${pullResult// /-}"
set -e
if [ "${pullResult}" != "already-up-to-date." ]
then
local mergeMessage
mergeMessage=$(git log -3 --no-merges --format="%s" | tail -n -1)
git commit --amend -m "REBUILD REQUIRED: ${mergeMessage}" -m "${pullResult}"
fi
}
nextRelease() {
mvn $MVN_ARGS versions:set -DprocessAllModules=true -DgenerateBackupPoms=false -DremoveSnapshot=true 1>&2
local releaseVersion
releaseVersion=$(mvn $MVN_ARGS -N -q org.codehaus.mojo:exec-maven-plugin:exec -Dexec.executable='echo' -Dexec.args='${project.version}')
echo "RELEASE_VERSION=${releaseVersion}" >> $GITHUB_ENV
echo ${releaseVersion}
}
nonReleaseBuild() {
log "Building in NON_RELEASE mode."
setupBuild
removeSnapshotsFromCache
MVN_ARGS+=" --update-snapshots"
MVN_ARGS+=" -Ddocker.skip=true"
set -x
mvn $MVN_ARGS install
set +x
}
releaseBuild() {
log "Building in RELEASE mode."
setupBuild
removeSnapshotsFromCache
# Snyk builds things using the directory structure not maven
# To allow snyk scanning to complete in SecRel, we need to make sure the snapshots get cached
log "Building old SNAPSHOT version for Snyk."
mvn ${MVN_ARGS} clean install -P"!standard" -DskipTests
local releaseVersion
releaseVersion=$(nextRelease)
log "Building release version: ${releaseVersion}"
set -x
mvn $MVN_ARGS -U -Prelease clean deploy
set +x
commitReleaseVersion "${releaseVersion}"
commitNextSnapshot
mergeMainBranch
git push --tags --force
git push
createGitHubRelease "${releaseVersion}"
}
removeSnapshotsFromCache() {
log "Removing old SNAPSHOT versions."
for f in $(find ~/.m2/repository/ -type d -name "*-SNAPSHOT")
do
rm -rf $f || true
done
}
requireOpt() {
local name="$1"
local var="$2"
local value="$(eval echo \${${var}:-})"
if [ -z "${value:-}" ]
then
usage "$name not specified, use option --$name or environment variable $var"
fi
}
runInitializeBuild() {
if [ ! -f "$INITIALIZE_BUILD" ]; then return; fi
log "Found $INITIALIZE_BUILD"
bash -c $(readlink -f $INITIALIZE_BUILD)
}
setupBuild() {
git remote --verbose
configureSettings
MVN_ARGS+=" --batch-mode"
MVN_ARGS+=" --settings ${SETTINGS}"
MVN_ARGS+=" -Dgithub.username=${GITHUB_USERNAME}"
MVN_ARGS+=" -Dgithub.token=${GITHUB_TOKEN}"
MVN_ARGS+=" -Dgit.enforceBranchNames=false"
MVN_ARGS+=" -Dmaven.resolver.transport=wagon"
if ! isJavaVersionSupported; then echo "Skipping build..."; return; fi
mvn --version
runInitializeBuild
}
main "$@"
exit 0