Skip to content

Commit 5fd0927

Browse files
committed
Use environment variables to configure Gradle init scripts
System properties cannot be reliably read from init scripts in certain versions of Gradle. This is not the case for environment variables.
1 parent 5a22aff commit 5fd0927

File tree

2 files changed

+45
-33
lines changed

2 files changed

+45
-33
lines changed

components/scripts/gradle/05-validate-remote-build-caching-ci-local.sh

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -194,19 +194,18 @@ validate_build_config() {
194194
}
195195

196196
execute_build() {
197-
local args
198-
args=(--build-cache --init-script "${INIT_SCRIPTS_DIR}/configure-remote-build-caching.gradle")
199-
if [ -n "${remote_build_cache_url}" ]; then
200-
args+=("-Ddevelocity.build-validation.remoteBuildCacheUrl=${remote_build_cache_url}")
201-
fi
197+
info "Running build:"
198+
print_gradle_command
202199

203-
# shellcheck disable=SC2206 # we want tasks to expand with word splitting in this case
204-
args+=(clean ${tasks})
200+
# shellcheck disable=SC2086 # we want tasks to expand with word splitting in this case
201+
invoke_gradle 1 \
202+
--build-cache \
203+
--init-script "${INIT_SCRIPTS_DIR}/configure-remote-build-caching.gradle" \
204+
clean ${tasks}
205+
}
205206

206-
info "Running build:"
207+
print_gradle_command() {
207208
info "./gradlew --build-cache -Dscan.tag.${EXP_SCAN_TAG} -Dscan.value.runId=${RUN_ID} -Dpts.enabled=false clean ${tasks}$(print_extra_args)"
208-
209-
invoke_gradle 1 "${args[@]}"
210209
}
211210

212211
# Overrides summary.sh#print_experiment_specific_summary_info

components/scripts/lib/gradle.sh

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
#!/usr/bin/env bash
22

33
invoke_gradle() {
4-
local run_num args
5-
args=()
4+
local run_num
65
run_num=$1
76
shift
87

@@ -12,35 +11,45 @@ invoke_gradle() {
1211
cd "${project_dir}" > /dev/null 2>&1 || die "ERROR: Subdirectory ${project_dir} (set with --project-dir) does not exist in ${project_name}" "${INVALID_INPUT}"
1312
fi
1413

15-
args+=(
16-
--init-script "${INIT_SCRIPTS_DIR}/develocity-injection.gradle"
17-
--init-script "${INIT_SCRIPTS_DIR}/configure-build-validation.gradle"
18-
-Ddevelocity.injection.init-script-name=develocity-injection.gradle
19-
-Ddevelocity.injection-enabled=true
14+
envs+=(
15+
DEVELOCITY_INJECTION_INIT_SCRIPT_NAME=develocity-injection.gradle
16+
DEVELOCITY_INJECTION_ENABLED=true
2017
)
2118

2219
if [ "$enable_ge" == "on" ]; then
23-
args+=(
24-
-Dgradle.plugin-repository.url=https://plugins.gradle.org/m2
25-
-Ddevelocity.plugin.version="3.14.1"
26-
-Ddevelocity.ccud.plugin.version="2.0.2"
20+
envs+=(
21+
GRADLE_PLUGIN_REPOSITORY_URL=https://plugins.gradle.org/m2
22+
DEVELOCITY_PLUGIN_VERSION="3.14.1"
23+
DEVELOCITY_CCUD_PLUGIN_VERSION="2.0.2"
2724
)
2825
fi
2926

3027
if [ -n "${ge_server}" ]; then
31-
args+=(
32-
-Ddevelocity.build-validation.url="${ge_server}"
33-
-Ddevelocity.build-validation.allow-untrusted-server=false
28+
envs+=(
29+
DEVELOCITY_BUILD_VALIDATION_URL="${ge_server}"
30+
DEVELOCITY_BUILD_VALIDATION_ALLOW_UNTRUSTED_SERVER=false
31+
)
32+
fi
33+
34+
if [ -n "${remote_build_cache_url}" ]; then
35+
envs+=(
36+
DEVELOCITY_BUILD_VALIDATION_REMOTEBUILDCACHEURL="${remote_build_cache_url}"
3437
)
3538
fi
3639

37-
args+=(
38-
-Ddevelocity.build-validation.expDir="${EXP_DIR}"
39-
-Ddevelocity.build-validation.expId="${EXP_SCAN_TAG}"
40-
-Ddevelocity.build-validation.runId="${RUN_ID}"
41-
-Ddevelocity.build-validation.runNum="${run_num}"
42-
-Ddevelocity.build-validation.scriptsVersion="${SCRIPT_VERSION}"
43-
-Ddevelocity.capture-file-fingerprints=true
40+
envs+=(
41+
DEVELOCITY_BUILD_VALIDATION_EXPDIR="${EXP_DIR}"
42+
DEVELOCITY_BUILD_VALIDATION_EXPID="${EXP_SCAN_TAG}"
43+
DEVELOCITY_BUILD_VALIDATION_RUNID="${RUN_ID}"
44+
DEVELOCITY_BUILD_VALIDATION_RUNNUM="${run_num}"
45+
DEVELOCITY_BUILD_VALIDATION_SCRIPTSVERSION="${SCRIPT_VERSION}"
46+
DEVELOCITY_CAPTURE_FILE_FINGERPRINTS=true
47+
)
48+
49+
local args
50+
args=(
51+
--init-script "${INIT_SCRIPTS_DIR}/develocity-injection.gradle"
52+
--init-script "${INIT_SCRIPTS_DIR}/configure-build-validation.gradle"
4453
-Dpts.enabled=false
4554
)
4655

@@ -57,9 +66,13 @@ invoke_gradle() {
5766
rm -f "${EXP_DIR}/errors.txt"
5867

5968
debug "Current directory: $(pwd)"
60-
debug ./gradlew "${args[@]}"
69+
# shellcheck disable=SC2145
70+
debug export "${envs[@]}"';' ./gradlew "${args[@]}"
6171

62-
if ./gradlew "${args[@]}"; then
72+
# The parenthesis below will intentionally create a subshell. This causes the
73+
# environment variables to only be exported for the child process and not leak
74+
# to the rest of the script.
75+
if (export "${envs[@]}"; ./gradlew "${args[@]}"); then
6376
build_outcomes+=("SUCCESSFUL")
6477
else
6578
build_outcomes+=("FAILED")

0 commit comments

Comments
 (0)