Skip to content

Commit 83e5519

Browse files
authored
Merge pull request #682 from gradle/erichaagdev/use-start-parameter
Environment variable changes are reverted and `gradle.startParameter` is used to read system properties in init scripts
2 parents d8b86f6 + 632519d commit 83e5519

8 files changed

+76
-57
lines changed

build.gradle.kts

+1
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ val copyGradleScripts by tasks.registering(Sync::class) {
167167
from(develocityInjectionResolvable) {
168168
rename { "develocity-injection.gradle" }
169169
into("lib/scripts/gradle-init-scripts")
170+
filter(TransformDevelocityInjectionScript())
170171
}
171172
from(layout.projectDirectory.dir("components/scripts")) {
172173
include("README.md")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Transforms usages of 'System.getProperty' in the Develocity injection script
3+
* to 'gradle.startParameter'. This is required because 'System.getProperty'
4+
* cannot be used to reliably read system properties from init scripts in Gradle
5+
* 7.0.2 and earlier.
6+
*/
7+
class TransformDevelocityInjectionScript : (String) -> String {
8+
9+
override fun invoke(content: String): String {
10+
return content
11+
.replace("static getInputParam(String name) {", "def getInputParam = { String name ->")
12+
.replace("System.getProperty(name)", "gradle.startParameter.systemPropertiesArgs[name]")
13+
14+
// The 'getInputParam' method is no longer static so it must be redefined within the
15+
// 'enableDevelocityInjection' method
16+
.replace("void enableDevelocityInjection() {", """
17+
void enableDevelocityInjection() {
18+
def getInputParam = { String name ->
19+
def ENV_VAR_PREFIX = ''
20+
def envVarName = ENV_VAR_PREFIX + name.toUpperCase().replace('.', '_').replace('-', '_')
21+
return gradle.startParameter.systemPropertiesArgs[name] ?: System.getenv(envVarName)
22+
}
23+
24+
""".trimIndent())
25+
}
26+
27+
}

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

+10-9
Original file line numberDiff line numberDiff line change
@@ -194,18 +194,19 @@ validate_build_config() {
194194
}
195195

196196
execute_build() {
197-
info "Running build:"
198-
print_gradle_command
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
199202

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-
}
203+
# shellcheck disable=SC2206 # we want tasks to expand with word splitting in this case
204+
args+=(clean ${tasks})
206205

207-
print_gradle_command() {
206+
info "Running build:"
208207
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[@]}"
209210
}
210211

211212
# Overrides summary.sh#print_experiment_specific_summary_info

components/scripts/gradle/gradle-init-scripts/configure-build-validation.gradle

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import org.gradle.util.GradleVersion
22
import java.nio.charset.StandardCharsets
33

4-
static getInputParam(String name) {
4+
def getInputParam = { String name ->
55
def ENV_VAR_PREFIX = ''
66
def envVarName = ENV_VAR_PREFIX + name.toUpperCase().replace('.', '_').replace('-', '_')
7-
return System.getProperty(name) ?: System.getenv(envVarName)
7+
return gradle.startParameter.systemPropertiesArgs[name] ?: System.getenv(envVarName)
88
}
99

1010
def isTopLevelBuild = !gradle.parent
@@ -34,12 +34,16 @@ def registerBuildScanActions = { def buildScan, def rootProjectName ->
3434
// the configuration of the build, and given its value changes between consecutive build invocations
3535
// it would always invalidate the configuration cache model from the first build invocation
3636
// in the second build invocation
37-
def getInputParam = { String name ->
38-
def ENV_VAR_PREFIX = ''
39-
def envVarName = ENV_VAR_PREFIX + name.toUpperCase().replace('.', '_').replace('-', '_')
37+
//
38+
// System.getProperty can be used here because system properties can be read at *execution* time
39+
// safely for Gradle 7.0.2 and earlier, and we must do so anyway because referencing a Gradle
40+
// script object, e.g., 'gradle.startParameter', from a Groovy closure is not compatible with
41+
// configuration cache
42+
def getRunNumInputParam = { String name ->
43+
def envVarName = name.toUpperCase().replace('.', '_').replace('-', '_')
4044
return System.getProperty(name) ?: System.getenv(envVarName)
4145
}
42-
def runNum = getInputParam('develocity.build-validation.runNum')
46+
def runNum = getRunNumInputParam('develocity.build-validation.runNum')
4347
def buildScanUri = publishedBuildScan.buildScanUri
4448
def buildScanId = publishedBuildScan.buildScanId
4549
def port = (buildScanUri.port != -1) ? ':' + buildScanUri.port : ''

components/scripts/gradle/gradle-init-scripts/configure-local-build-caching.gradle

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
static getInputParam(String name) {
1+
def getInputParam = { String name ->
22
def ENV_VAR_PREFIX = ''
33
def envVarName = ENV_VAR_PREFIX + name.toUpperCase().replace('.', '_').replace('-', '_')
4-
return System.getProperty(name) ?: System.getenv(envVarName)
4+
return gradle.startParameter.systemPropertiesArgs[name] ?: System.getenv(envVarName)
55
}
66

77
def expDir = getInputParam('develocity.build-validation.expDir')

components/scripts/gradle/gradle-init-scripts/configure-remote-build-caching.gradle

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
static getInputParam(String name) {
1+
def getInputParam = { String name ->
22
def ENV_VAR_PREFIX = ''
33
def envVarName = ENV_VAR_PREFIX + name.toUpperCase().replace('.', '_').replace('-', '_')
4-
return System.getProperty(name) ?: System.getenv(envVarName)
4+
return gradle.startParameter.systemPropertiesArgs[name] ?: System.getenv(envVarName)
55
}
66

77
def remoteBuildCacheUrl = getInputParam('develocity.build-validation.remoteBuildCacheUrl')

components/scripts/lib/gradle.sh

+23-37
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#!/usr/bin/env bash
22

33
invoke_gradle() {
4-
local run_num envs
5-
envs=()
4+
local run_num args
5+
args=()
66
run_num=$1
77
shift
88

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

15-
envs+=(
16-
DEVELOCITY_INJECTION_INIT_SCRIPT_NAME=develocity-injection.gradle
17-
DEVELOCITY_INJECTION_ENABLED=true
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
1820
)
1921

2022
if [ "$enable_ge" == "on" ]; then
21-
envs+=(
22-
GRADLE_PLUGIN_REPOSITORY_URL=https://plugins.gradle.org/m2
23-
DEVELOCITY_PLUGIN_VERSION="3.14.1"
24-
DEVELOCITY_CCUD_PLUGIN_VERSION="2.0.2"
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"
2527
)
2628
fi
2729

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

41-
envs+=(
42-
DEVELOCITY_BUILD_VALIDATION_EXPDIR="${EXP_DIR}"
43-
DEVELOCITY_BUILD_VALIDATION_EXPID="${EXP_SCAN_TAG}"
44-
DEVELOCITY_BUILD_VALIDATION_RUNID="${RUN_ID}"
45-
DEVELOCITY_BUILD_VALIDATION_RUNNUM="${run_num}"
46-
DEVELOCITY_BUILD_VALIDATION_SCRIPTSVERSION="${SCRIPT_VERSION}"
47-
DEVELOCITY_CAPTURE_FILE_FINGERPRINTS=true
48-
)
49-
50-
local args
51-
args=(
52-
--init-script "${INIT_SCRIPTS_DIR}/develocity-injection.gradle"
53-
--init-script "${INIT_SCRIPTS_DIR}/configure-build-validation.gradle"
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
5444
-Dpts.enabled=false
5545
)
5646

@@ -67,13 +57,9 @@ invoke_gradle() {
6757
rm -f "${EXP_DIR}/errors.txt"
6858

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

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

release/changes.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
> [!IMPORTANT]
22
> The distributions of the Develocity Build Validation Scripts prefixed with `gradle-enterprise` are deprecated and will be removed in a future release. Migrate to the distributions prefixed with `develocity` instead.
33
4-
- [NEW] TBD
4+
[FIX] Scripts do not detect published build scans for Gradle 5.x causing experiments to fail

0 commit comments

Comments
 (0)