Skip to content

Commit ed1054d

Browse files
committed
CI/Build: Add Gradle Build Scans
Gradle Build Scans, free of use, collect a lot of information about a Gradle build, including the actual output of failed test. This becomes quite convenient when inspecting test failures in CI and a lot of other information about Gradle builds. [Example build scan](https://scans.gradle.com/s/jpuykotf4hac6)
1 parent f9433d2 commit ed1054d

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

.github/workflows/gradle.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ jobs:
3939
permissions:
4040
contents: read
4141

42+
env:
43+
# https://docs.gradle.com/develocity/gradle-plugin/current/#via_environment_variable
44+
DEVELOCITY_ACCESS_KEY: ${{secrets.GE_ACCESS_TOKEN}}
45+
4246
steps:
4347
- uses: actions/checkout@v4
4448
- name: Set up JDK 21

settings.gradle.kts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,70 @@ dependencyResolutionManagement {
7373
}
7474

7575
gradle.beforeProject { version = baseVersion }
76+
77+
plugins { id("com.gradle.develocity") version ("3.18.2") }
78+
79+
// Use Apache's Gradle Enterprise instance only in CI and only if the access-key is present
80+
// See
81+
// https://cwiki.apache.org/confluence/pages/viewpage.action?pageId=263426153#ProjectOnboardingInstructionsforDevelocity-GitHubActions
82+
// https://docs.gradle.com/develocity/gradle-plugin/current/#via_environment_variable
83+
val hasDevelocityAccessKey =
84+
System.getenv("DEVELOCITY_ACCESS_KEY") != null &&
85+
System.getenv("DEVELOCITY_ACCESS_KEY").isNotBlank()
86+
val isCI = System.getenv("CI") != null
87+
88+
if (isCI) {
89+
logger.lifecycle(
90+
"Running build in CI ${if (hasDevelocityAccessKey) "with" else "without"} Develocity access"
91+
)
92+
}
93+
94+
// Publish a build-scan automatically in CI, else only on demand.
95+
develocity {
96+
if (isCI) {
97+
if (hasDevelocityAccessKey) {
98+
// The GE instance for Apache rejects unauthenticated build scan uploads, so we publish those
99+
// to Gradle's public one. This means, that CI runs without secrets, aka all PR CI runs, get
100+
// the build scans published at Gradle's infra, while other CI runs publish to Apache's infra.
101+
server = "https://ge.apache.org"
102+
}
103+
buildScan {
104+
termsOfUseUrl = "https://gradle.com/terms-of-service"
105+
termsOfUseAgree = "yes"
106+
// Add some potentially interesting information from the environment
107+
listOf(
108+
"GITHUB_ACTION_REPOSITORY",
109+
"GITHUB_ACTOR",
110+
"GITHUB_BASE_REF",
111+
"GITHUB_HEAD_REF",
112+
"GITHUB_JOB",
113+
"GITHUB_REF",
114+
"GITHUB_REPOSITORY",
115+
"GITHUB_RUN_ID",
116+
"GITHUB_RUN_NUMBER",
117+
"GITHUB_SHA",
118+
"GITHUB_WORKFLOW"
119+
)
120+
.forEach { e ->
121+
val v = System.getenv(e)
122+
if (v != null) {
123+
value(e, v)
124+
}
125+
}
126+
val ghUrl = System.getenv("GITHUB_SERVER_URL")
127+
if (ghUrl != null) {
128+
val ghRepo = System.getenv("GITHUB_REPOSITORY")
129+
val ghRunId = System.getenv("GITHUB_RUN_ID")
130+
link("Summary", "$ghUrl/$ghRepo/actions/runs/$ghRunId")
131+
link("PRs", "$ghUrl/$ghRepo/pulls")
132+
}
133+
obfuscation { ipAddresses { addresses -> addresses.map { _ -> "0.0.0.0" } } }
134+
}
135+
} else {
136+
val isBuildScan = gradle.startParameter.isBuildScan
137+
buildScan {
138+
publishing { onlyIf { isBuildScan } }
139+
obfuscation { ipAddresses { addresses -> addresses.map { _ -> "0.0.0.0" } } }
140+
}
141+
}
142+
}

0 commit comments

Comments
 (0)