Skip to content

Commit 6bf6008

Browse files
wilkinsonagaryrussell
authored andcommitted
Add opt-in integration with Gradle's build scans
1 parent b53c934 commit 6bf6008

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

build.gradle

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,26 @@ plugins {
1414
id 'base'
1515
id 'project-report'
1616
id 'idea'
17+
id 'com.gradle.build-scan' version '2.4.2'
1718
id 'org.sonarqube' version '2.7.1'
1819
id 'org.asciidoctor.convert' version '1.6.1'
1920
id 'org.ajoberstar.grgit' version '3.1.1'
2021
id 'io.spring.nohttp' version '0.0.3.RELEASE'
2122
id 'io.spring.dependency-management' version '1.0.8.RELEASE' apply false
2223
}
2324

25+
if (System.getenv('GRADLE_ENTERPRISE_URL')) {
26+
apply from: "$rootDir/gradle/build-scan-user-data.gradle"
27+
buildScan {
28+
captureTaskInputFiles = true
29+
obfuscation {
30+
ipAddresses { addresses -> addresses.collect { address -> '0.0.0.0'} }
31+
}
32+
publishAlways()
33+
server = System.getenv('GRADLE_ENTERPRISE_URL')
34+
}
35+
}
36+
2437
apply plugin: 'io.spring.nohttp'
2538

2639
description = 'Spring Kafka'

gradle/build-scan-user-data.gradle

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
tagOs()
2+
tagIde()
3+
tagCiOrLocal()
4+
addCiMetadata()
5+
addGitMetadata()
6+
addTestTaskMetadata()
7+
8+
void tagOs() {
9+
buildScan.tag System.getProperty('os.name')
10+
}
11+
12+
void tagIde() {
13+
if (System.getProperty('idea.version')) {
14+
buildScan.tag 'IntelliJ IDEA'
15+
} else if (System.getProperty('eclipse.buildId')) {
16+
buildScan.tag 'Eclipse'
17+
}
18+
}
19+
20+
void tagCiOrLocal() {
21+
buildScan.tag(isCi() ? 'CI' : 'LOCAL')
22+
}
23+
24+
void addGitMetadata() {
25+
buildScan.background {
26+
def gitCommitId = execAndGetStdout('git', 'rev-parse', '--short=8', '--verify', 'HEAD')
27+
def gitBranchName = execAndGetStdout('git', 'rev-parse', '--abbrev-ref', 'HEAD')
28+
def gitStatus = execAndGetStdout('git', 'status', '--porcelain')
29+
30+
if(gitCommitId) {
31+
def commitIdLabel = 'Git Commit ID'
32+
value commitIdLabel, gitCommitId
33+
link 'Git commit build scans', customValueSearchUrl([(commitIdLabel): gitCommitId])
34+
}
35+
if (gitBranchName) {
36+
tag gitBranchName
37+
value 'Git branch', gitBranchName
38+
}
39+
if (gitStatus) {
40+
tag 'dirty'
41+
value 'Git status', gitStatus
42+
}
43+
}
44+
}
45+
46+
void addCiMetadata() {
47+
def ciBuild = 'CI BUILD'
48+
if (isBamboo()) {
49+
buildScan.link ciBuild, System.getenv('bamboo_resultsUrl')
50+
}
51+
}
52+
53+
void addTestTaskMetadata() {
54+
allprojects {
55+
tasks.withType(Test) { test ->
56+
doFirst {
57+
buildScan.value "Test#maxParallelForks[${test.path}]", test.maxParallelForks.toString()
58+
}
59+
}
60+
}
61+
}
62+
63+
boolean isCi() {
64+
isBamboo()
65+
}
66+
67+
boolean isBamboo() {
68+
System.getenv('bamboo_resultsUrl')
69+
}
70+
71+
String execAndGetStdout(String... args) {
72+
def stdout = new ByteArrayOutputStream()
73+
exec {
74+
commandLine(args)
75+
standardOutput = stdout
76+
}
77+
return stdout.toString().trim()
78+
}
79+
80+
String customValueSearchUrl(Map<String, String> search) {
81+
def query = search.collect { name, value ->
82+
"search.names=${encodeURL(name)}&search.values=${encodeURL(value)}"
83+
}.join('&')
84+
85+
"$buildScan.server/scans?$query"
86+
}
87+
88+
String encodeURL(String url) {
89+
URLEncoder.encode(url, 'UTF-8')
90+
}

0 commit comments

Comments
 (0)