This repository has been archived by the owner on Nov 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
build-locally.sh
executable file
·484 lines (413 loc) · 16.5 KB
/
build-locally.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
#! /usr/bin/env bash
#
# Copyright contributors to the Galasa project
#
# SPDX-License-Identifier: EPL-2.0
#
#-----------------------------------------------------------------------------------------
#
# Objectives: Build this repository code locally.
#
#-----------------------------------------------------------------------------------------
# Where is this script executing from ?
BASEDIR=$(dirname "$0");pushd $BASEDIR 2>&1 >> /dev/null ;BASEDIR=$(pwd);popd 2>&1 >> /dev/null
# echo "Running from directory ${BASEDIR}"
export ORIGINAL_DIR=$(pwd)
# cd "${BASEDIR}"
cd "${BASEDIR}/.."
WORKSPACE_DIR=$(pwd)
#-----------------------------------------------------------------------------------------
#
# Set Colors
#
#-----------------------------------------------------------------------------------------
bold=$(tput bold)
underline=$(tput sgr 0 1)
reset=$(tput sgr0)
red=$(tput setaf 1)
green=$(tput setaf 76)
white=$(tput setaf 7)
tan=$(tput setaf 202)
blue=$(tput setaf 25)
#-----------------------------------------------------------------------------------------
#
# Headers and Logging
#
#-----------------------------------------------------------------------------------------
underline() { printf "${underline}${bold}%s${reset}\n" "$@"
}
h1() { printf "\n${underline}${bold}${blue}%s${reset}\n" "$@"
}
h2() { printf "\n${underline}${bold}${white}%s${reset}\n" "$@"
}
debug() { printf "${white}[.] %s${reset}\n" "$@"
}
info() { printf "${white}[➜] %s${reset}\n" "$@"
}
success() { printf "${white}[${green}✔${white}] ${green}%s${reset}\n" "$@"
}
error() { printf "${white}[${red}✖${white}] ${red}%s${reset}\n" "$@"
}
warn() { printf "${white}[${tan}➜${white}] ${tan}%s${reset}\n" "$@"
}
bold() { printf "${bold}%s${reset}\n" "$@"
}
note() { printf "\n${underline}${bold}${blue}Note:${reset} ${blue}%s${reset}\n" "$@"
}
#-----------------------------------------------------------------------------------------
# Functions
#-----------------------------------------------------------------------------------------
function usage {
info "Syntax: build-locally.sh [OPTIONS]"
cat << EOF
Options are:
-h | --help : Display this help text
Environment Variables:
SOURCE_MAVEN :
Used to indicate where parts of the OBR can be obtained.
Optional. Defaults to https://development.galasa.dev/main/maven-repo/managers/
LOGS_DIR :
Controls where logs are placed.
Optional. Defaults to creating a new temporary folder
GPG_PASSPHRASE :
Mandatory.
Controls how the obr is signed. Needs to be the alias of the private key of a
public-private gpg pair. eg: For development you could use your signing github
passphrase.
EOF
}
function check_exit_code () {
# This function takes 3 parameters in the form:
# $1 an integer value of the returned exit code
# $2 an error message to display if $1 is not equal to 0
if [[ "$1" != "0" ]]; then
error "$2"
exit 1
fi
}
#-----------------------------------------------------------------------------------------
# Process parameters
#-----------------------------------------------------------------------------------------
exportbuild_type=""
while [ "$1" != "" ]; do
case $1 in
-h | --help ) usage
exit
;;
* ) error "Unexpected argument $1"
usage
exit 1
esac
shift
done
if [[ -z $GPG_PASSPHRASE ]]; then
error "Environment variable GPG_PASSPHRASE needs to be set."
usage
exit 1
fi
#-----------------------------------------------------------------------------------------
# Main logic.
#-----------------------------------------------------------------------------------------
source_dir="."
project=$(basename ${BASEDIR})
h1 "Building ${project}"
# Over-rode SOURCE_MAVEN if you want to build from a different maven repo...
if [[ -z ${SOURCE_MAVEN} ]]; then
export SOURCE_MAVEN=https://development.galasa.dev/main/maven-repo/managers/
info "SOURCE_MAVEN repo defaulting to ${SOURCE_MAVEN}."
info "Set this environment variable if you want to over-ride this value."
else
info "SOURCE_MAVEN set to ${SOURCE_MAVEN} by caller."
fi
# Create a temporary dir.
# Note: This bash 'spell' works in OSX and Linux.
if [[ -z ${LOGS_DIR} ]]; then
export LOGS_DIR=$(mktemp -d 2>/dev/null || mktemp -d -t "galasa-logs")
info "Logs are stored in the ${LOGS_DIR} folder."
info "Over-ride this setting using the LOGS_DIR environment variable."
else
mkdir -p ${LOGS_DIR} 2>&1 > /dev/null # Don't show output. We don't care if it already existed.
info "Logs are stored in the ${LOGS_DIR} folder."
info "Over-ridden by caller using the LOGS_DIR variable."
fi
info "Using source code at ${source_dir}"
cd ${BASEDIR}/${source_dir}
if [[ "${DEBUG}" == "1" ]]; then
OPTIONAL_DEBUG_FLAG="-debug"
else
OPTIONAL_DEBUG_FLAG="-info"
fi
log_file=${LOGS_DIR}/${project}.txt
info "Log will be placed at ${log_file}"
date > ${log_file}
#------------------------------------------------------------------------------------
function get_galasabld_binary_location {
# What's the architecture-variable name of the build tool we want for this local build ?
export ARCHITECTURE=$(uname -m) # arm64 or amd64
if [ $ARCHITECTURE == "x86_64" ]; then
export ARCHITECTURE="amd64"
fi
raw_os=$(uname -s) # eg: "Darwin"
os=""
case $raw_os in
Darwin*)
os="darwin"
;;
Windows*)
os="windows"
;;
Linux*)
os="linux"
;;
*)
error "Failed to recognise which operating system is in use. $raw_os"
exit 1
esac
export GALASA_BUILD_TOOL_NAME=galasabld-${os}-${ARCHITECTURE}
# Favour the galasabld tool if it's on the path, else use a locally-built version or fail if not available.
GALASABLD_ON_PATH=$(which galasabld)
rc=$?
if [[ "${rc}" == "0" ]]; then
info "Using the 'galasabld' tool which is on the PATH"
GALASA_BUILD_TOOL_PATH=${GALASABLD_ON_PATH}
else
GALASABLD_ON_PATH=$(which $GALASA_BUILD_TOOL_NAME)
rc=$?
if [[ "${rc}" == "0" ]]; then
info "Using the '$GALASA_BUILD_TOOL_NAME' tool which is on the PATH"
GALASA_BUILD_TOOL_PATH=${GALASABLD_ON_PATH}
else
info "The galasa build tool 'galasabld' or '$GALASA_BUILD_TOOL_NAME' is not on the path."
export GALASA_BUILD_TOOL_PATH=${WORKSPACE_DIR}/buildutils/bin/${GALASA_BUILD_TOOL_NAME}
if [[ ! -e ${GALASA_BUILD_TOOL_PATH} ]]; then
error "Cannot find the $GALASA_BUILD_TOOL_NAME tools on locally built workspace."
info "Try re-building the buildutils project"
exit 1
else
info "Using the $GALASA_BUILD_TOOL_NAME tool at ${GALASA_BUILD_TOOL_PATH}"
fi
fi
fi
}
#------------------------------------------------------------------------------------
function read_component_version {
h2 "Getting the component version"
export component_version=$(cat release.yaml | grep "version" | head -1 | cut -f2 -d':' | xargs)
success "Component version is $component_version"
}
#------------------------------------------------------------------------------------
function download_dependencies {
h2 "Downloading the dependencies to get release.yaml information"
cd ${BASEDIR}/dependency-download
gradle getDeps \
-Dgalasa.source.repo=${SOURCE_MAVEN} \
-Dgalasa.central.repo=https://repo.maven.apache.org/maven2/
rc=$?
if [[ "${rc}" != "0" ]]; then
error "Failed to download dependencies. rc=$rc"
exit 1
fi
success "OK - dependencies downloaded."
}
#------------------------------------------------------------------------------------
function check_dependencies_present {
h2 "Checking dependencies are present..."
export framework_manifest_path=${BASEDIR}/dependency-download/build/dependencies/dev.galasa.framework.manifest.yaml
export managers_manifest_path=${BASEDIR}/dependency-download/build/dependencies/dev.galasa.managers.manifest.yaml
# export framework_manifest_path=${WORKSPACE_DIR}/framework/release.yaml
# export managers_manifest_path=${WORKSPACE_DIR}/managers/release.yaml
declare -a required_files=(
${WORKSPACE_DIR}/${project}/dev.galasa.uber.obr/pom.template
${framework_manifest_path}
${WORKSPACE_DIR}/extensions/release.yaml
${managers_manifest_path}
${WORKSPACE_DIR}/obr/release.yaml
)
for required_file in "${required_files[@]}"
do
if [[ -e "${required_file}" ]]; then
success "OK - File ${required_file} is present."
else
error "File ${required_file} is required, but missing. Clone the sibling project to make sure it exists."
exit 1
fi
done
}
#------------------------------------------------------------------------------------
function construct_bom_pom_xml {
h2 "Generating a bom pom.xml from a template, using all the versions of everything..."
cd ${WORKSPACE_DIR}/${project}/galasa-bom
# Check local build version
export GALASA_BUILD_TOOL_PATH=${WORKSPACE_DIR}/buildutils/bin/${GALASA_BUILD_TOOL_NAME}
info "Using galasabld tool ${GALASA_BUILD_TOOL_PATH}"
cmd="${GALASA_BUILD_TOOL_PATH} template \
--releaseMetadata ${framework_manifest_path} \
--releaseMetadata ${WORKSPACE_DIR}/extensions/release.yaml \
--releaseMetadata ${managers_manifest_path} \
--releaseMetadata ${WORKSPACE_DIR}/obr/release.yaml \
--template pom.template \
--output pom.xml \
--bom \
"
echo "Command is $cmd" >> ${log_file}
$cmd 2>&1 >> ${log_file}
rc=$?
if [[ "${rc}" != "0" ]]; then
error "Failed to convert release.yaml files into a pom.xml ${project}. log file is ${log_file}"
exit 1
fi
success "pom.xml built ok - log is at ${log_file}"
}
#------------------------------------------------------------------------------------
function construct_uber_obr_pom_xml {
h2 "Generating a pom.xml from a template, using all the versions of everything..."
cd ${WORKSPACE_DIR}/${project}/dev.galasa.uber.obr
# Check local build version
export GALASA_BUILD_TOOL_PATH=${WORKSPACE_DIR}/buildutils/bin/${GALASA_BUILD_TOOL_NAME}
info "Using galasabld tool ${GALASA_BUILD_TOOL_PATH}"
cmd="${GALASA_BUILD_TOOL_PATH} template \
--releaseMetadata ${framework_manifest_path} \
--releaseMetadata ${WORKSPACE_DIR}/extensions/release.yaml \
--releaseMetadata ${managers_manifest_path} \
--releaseMetadata ${WORKSPACE_DIR}/obr/release.yaml \
--template pom.template \
--output pom.xml \
--obr \
"
echo "Command is $cmd" >> ${log_file}
$cmd 2>&1 >> ${log_file}
rc=$?
if [[ "${rc}" != "0" ]]; then
error "Failed to convert release.yaml files into a pom.xml ${project}. log file is ${log_file}"
exit 1
fi
success "pom.xml built ok - log is at ${log_file}"
}
#------------------------------------------------------------------------------------
function check_developer_attribution_present {
h2 "Checking that pom has developer attribution."
cat ${BASEDIR}/galasa-bom/pom.template | grep "<developers>" >> /dev/null
rc=$?
if [[ "${rc}" != "0" ]]; then
error "The pom.template must have developer attribution inside. \
This is needed so that we can publish artifacts to maven central."
exit 1
fi
success "OK. Pom template contains developer attribution, which maven central needs at the point we publish."
}
#------------------------------------------------------------------------------------
function build_generated_bom_pom {
h2 "Building the generated pom.xml to package-up things into an OBR we can publish..."
cd ${BASEDIR}/galasa-bom
mvn \
-Dgpg.passphrase=${GPG_PASSPHRASE} \
-Dgalasa.source.repo=${SOURCE_MAVEN} \
-Dgalasa.central.repo=https://repo.maven.apache.org/maven2/ install \
2>&1 >> ${log_file}
rc=$? ; if [[ "${rc}" != "0" ]]; then
error "Failed to push built obr into maven repo ${project}. log file is ${log_file}"
exit 1
fi
success "OK"
}
#------------------------------------------------------------------------------------
function build_generated_uber_obr_pom {
h2 "Building the generated pom.xml to package-up things into an OBR we can publish..."
cd ${BASEDIR}/dev.galasa.uber.obr
mvn \
-Dgpg.passphrase=${GPG_PASSPHRASE} \
-Dgalasa.source.repo=${SOURCE_MAVEN} \
-Dgalasa.central.repo=https://repo.maven.apache.org/maven2/ install \
2>&1 >> ${log_file}
rc=$? ; if [[ "${rc}" != "0" ]]; then
error "Failed to push built obr into maven repo ${project}. log file is ${log_file}"
exit 1
fi
success "OK"
}
#------------------------------------------------------------------------------------
function generate_javadoc_pom_xml {
h2 "Generate a pom.xml we can use with the javadoc"
#------------------------------------------------------------------------------------
cd ${WORKSPACE_DIR}/obr/javadocs
${GALASA_BUILD_TOOL_PATH} template \
--releaseMetadata ${WORKSPACE_DIR}/framework/release.yaml \
--releaseMetadata ${WORKSPACE_DIR}/extensions/release.yaml \
--releaseMetadata ${WORKSPACE_DIR}/managers/release.yaml \
--releaseMetadata ${WORKSPACE_DIR}/obr/release.yaml \
--template pom.template \
--output pom.xml \
--javadoc
rc=$? ; if [[ "${rc}" != "0" ]]; then error "Failed to create the pom.xml for javadoc" ; exit 1 ; fi
success "OK - pom.xml file created at ${WORKSPACE_DIR}/obr/javadocs/pom.xml"
}
#------------------------------------------------------------------------------------
function build_javadoc_pom {
h2 "Building the javadoc with maven"
cd ${WORKSPACE_DIR}/obr/javadocs
mvn clean install \
--settings ${WORKSPACE_DIR}/obr/settings.xml \
--batch-mode \
--errors \
--fail-at-end \
-Dgpg.skip=true \
-Dgalasa.source.repo=${SOURCE_MAVEN} \
-Dgalasa.central.repo=https://repo.maven.apache.org/maven2/ \
-Dmaven.javadoc.failOnError=true
rc=$? ; if [[ "${rc}" != "0" ]]; then error "maven failed for javadoc build" ; exit 1 ; fi
success "OK - Build the galasa-uber-javadoc-*.zip file:"
ls ${WORKSPACE_DIR}/obr/javadocs/target/*.zip
}
#------------------------------------------------------------------------------------
function check_secrets {
h2 "updating secrets baseline"
cd ${BASEDIR}
detect-secrets scan --update .secrets.baseline
rc=$?
check_exit_code $rc "Failed to run detect-secrets. Please check it is installed properly"
success "updated secrets file"
h2 "running audit for secrets"
detect-secrets audit .secrets.baseline
rc=$?
check_exit_code $rc "Failed to audit detect-secrets."
#Check all secrets have been audited
secrets=$(grep -c hashed_secret .secrets.baseline)
audits=$(grep -c is_secret .secrets.baseline)
if [[ "$secrets" != "$audits" ]]; then
error "Not all secrets found have been audited"
exit 1
fi
success "secrets audit complete"
h2 "Removing the timestamp from the secrets baseline file so it doesn't always cause a git change."
mkdir -p temp
rc=$?
check_exit_code $rc "Failed to create a temporary folder"
cat .secrets.baseline | grep -v "generated_at" > temp/.secrets.baseline.temp
rc=$?
check_exit_code $rc "Failed to create a temporary file with no timestamp inside"
mv temp/.secrets.baseline.temp .secrets.baseline
rc=$?
check_exit_code $rc "Failed to overwrite the secrets baseline with one containing no timestamp inside."
success "secrets baseline timestamp content has been removed ok"
}
# #------------------------------------------------------------------------------------
# h2 "Packaging the javadoc into a docker file"
# #------------------------------------------------------------------------------------
# cd ${WORKSPACE_DIR}/obr/javadocs
# docker --file ${WORKSPACE_DIR}/automation/dockerfiles/javadocs/javadocs-image-dockerfile .
# rc=$? ; if [[ "${rc}" != "0" ]]; then error "Failed to create the docker image containing the javadoc" ; exit 1 ; fi
# success "OK"
read_component_version
download_dependencies
get_galasabld_binary_location
check_dependencies_present
construct_uber_obr_pom_xml
construct_bom_pom_xml
check_developer_attribution_present
build_generated_uber_obr_pom
build_generated_bom_pom
h1 "Building the javadoc using the OBR..."
generate_javadoc_pom_xml
build_javadoc_pom
check_secrets
success "Project ${project} built - OK - log is at ${log_file}"