Skip to content

Commit e82571f

Browse files
authored
Update build scan configuration to support Buildkite (#93715)
1 parent 8813bb8 commit e82571f

File tree

2 files changed

+53
-41
lines changed

2 files changed

+53
-41
lines changed

build-tools-internal/src/main/groovy/elasticsearch.build-scan.gradle

Lines changed: 52 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,10 @@ import org.elasticsearch.gradle.Architecture
1010
import org.elasticsearch.gradle.OS
1111
import org.elasticsearch.gradle.internal.info.BuildParams
1212
import org.gradle.initialization.BuildRequestMetaData
13-
import org.elasticsearch.gradle.internal.test.rerun.TestRerunPlugin
14-
import java.util.concurrent.TimeUnit
15-
16-
long startTime = project.gradle.services.get(BuildRequestMetaData).getStartTime()
1713

1814
buildScan {
1915
URL jenkinsUrl = System.getenv('JENKINS_URL') ? new URL(System.getenv('JENKINS_URL')) : null
16+
String buildKiteUrl = System.getenv('BUILDKITE_BUILD_URL') ? System.getenv('BUILDKITE_BUILD_URL') : null
2017

2118
// Automatically publish scans from Elasticsearch CI
2219
if (jenkinsUrl?.host?.endsWith('elastic.co') || jenkinsUrl?.host?.endsWith('elastic.dev')) {
@@ -25,12 +22,6 @@ buildScan {
2522
}
2623

2724
background {
28-
String buildNumber = System.getenv('BUILD_NUMBER')
29-
String buildUrl = System.getenv('BUILD_URL')
30-
String jobName = System.getenv('JOB_NAME')
31-
String nodeName = System.getenv('NODE_NAME')
32-
String jobBranch = System.getenv('ghprbTargetBranch') ?: System.getenv('JOB_BRANCH')
33-
3425
tag OS.current().name()
3526
tag Architecture.current().name()
3627

@@ -39,23 +30,25 @@ buildScan {
3930
tag 'FIPS'
4031
}
4132

42-
// Link to Jenkins worker logs and system metrics
43-
if (nodeName) {
44-
link 'System logs', "https://ci-stats.elastic.co/app/infra#/logs?" +
45-
"&logFilter=(expression:'host.name:${nodeName}',kind:kuery)"
46-
buildFinished {
47-
link 'System metrics', "https://ci-stats.elastic.co/app/metrics/detail/host/" +
48-
"${nodeName}?_g=()&metricTime=(autoReload:!f,refreshInterval:5000," +
49-
"time:(from:${startTime - TimeUnit.MILLISECONDS.convert(5, TimeUnit.MINUTES)},interval:%3E%3D1m," +
50-
"to:${System.currentTimeMillis() + TimeUnit.MILLISECONDS.convert(5, TimeUnit.MINUTES)}))"
51-
}
52-
}
53-
5433
// Jenkins-specific build scan metadata
5534
if (jenkinsUrl) {
5635
// Disable async upload in CI to ensure scan upload completes before CI agent is terminated
5736
uploadInBackground = false
5837

38+
String buildNumber = System.getenv('BUILD_NUMBER')
39+
String buildUrl = System.getenv('BUILD_URL')
40+
String jobName = System.getenv('JOB_NAME')
41+
String nodeName = System.getenv('NODE_NAME')
42+
String jobBranch = System.getenv('ghprbTargetBranch') ?: System.getenv('JOB_BRANCH')
43+
44+
// Link to Jenkins worker logs and system metrics
45+
if (nodeName) {
46+
link 'System logs', "https://ci-stats.elastic.co/app/infra#/logs?&logFilter=(expression:'host.name:${nodeName}',kind:kuery)"
47+
buildFinished {
48+
link 'System metrics', "https://ci-stats.elastic.co/app/metrics/detail/host/${nodeName}"
49+
}
50+
}
51+
5952
// Parse job name in the case of matrix builds
6053
// Matrix job names come in the form of "base-job-name/matrix_param1=value1,matrix_param2=value2"
6154
def splitJobName = jobName.split('/')
@@ -98,26 +91,45 @@ buildScan {
9891
value 'Git Commit ID', BuildParams.gitRevision
9992
link 'Source', "https://github.com/elastic/elasticsearch/tree/${BuildParams.gitRevision}"
10093
}
94+
} else if (buildKiteUrl) { //Buildkite-specific build scan metadata
95+
// Disable async upload in CI to ensure scan upload completes before CI agent is terminated
96+
uploadInBackground = false
97+
98+
def branch = System.getenv('BUILDKITE_BRANCH')
99+
def repoMatcher = System.getenv('BUILDKITE_REPO') =~ /(https:\/\/github\.com\/|git@github\.com:)(\S+)\.git/
100+
def repository = repoMatcher.matches() ? repoMatcher.group(2) : "<unknown>"
101+
tag 'CI'
102+
link 'CI Build', buildKiteUrl
103+
value 'Job Number', System.getenv('BUILDKITE_BUILD_NUMBER')
104+
105+
106+
// Add SCM information
107+
def prId = System.getenv('BUILDKITE_PULL_REQUEST')
108+
if (prId) {
109+
def prBaseUrl = (System.getenv('BUILDKITE_PULL_REQUEST_REPO') - ".git")
110+
value 'Git Commit ID', System.getenv('BUILDKITE_COMMIT')
111+
tag "pr/${prId}"
112+
tag 'pull-request'
113+
link 'Source', "${prBaseUrl}/tree/${System.getenv('BUILDKITE_COMMIT')}"
114+
link 'Pull Request', "https://github.com/${repository}/pull/${prId}"
115+
} else {
116+
value 'Git Commit ID', BuildParams.gitRevision
117+
link 'Source', "https://github.com/${repository}/tree/${BuildParams.gitRevision}"
118+
tag branch
119+
}
120+
121+
buildScanPublished { scan ->
122+
// Attach build scan link as build metadata
123+
// See: https://buildkite.com/docs/pipelines/build-meta-data
124+
['buildkite-agent', 'meta-data', 'set', "build-scan-${System.getenv('BUILDKITE_JOB_ID')}", "${scan.buildScanUri}"].execute()
125+
126+
// Add a build annotation
127+
// See: https://buildkite.com/docs/agent/v3/cli-annotate
128+
def body = """<div class="mb3"><span class="p1 border rounded">${System.getenv('BUILDKITE_LABEL')}</span> :gradle: build ran: <a href="${scan.buildScanUri}"><code>gradle ${gradle.startParameter.taskNames.join(' ')}</code></a></div>"""
129+
['buildkite-agent', 'annotate', '--context', 'gradle-build-scans', '--append', '--style', 'info', body].execute()
130+
}
101131
} else {
102132
tag 'LOCAL'
103133
}
104134
}
105135
}
106-
107-
subprojects {
108-
project.getPlugins().withType(TestRerunPlugin) {
109-
tasks.withType(Test).configureEach(new Action<Test>() {
110-
@Override
111-
void execute(Test test) {
112-
test.doLast(new Action<Test>() {
113-
@Override
114-
void execute(Test t) {
115-
if(t.rerun.didRerun.get() == true) {
116-
buildScan.tag 'unexpected-test-jvm-exit'
117-
}
118-
}
119-
})
120-
}
121-
})
122-
}
123-
}

build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/info/GlobalBuildInfoPlugin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public void apply(Project project) {
112112
params.setGitOrigin(gitInfo.getOrigin());
113113
params.setBuildDate(ZonedDateTime.now(ZoneOffset.UTC));
114114
params.setTestSeed(getTestSeed());
115-
params.setIsCi(System.getenv("JENKINS_URL") != null);
115+
params.setIsCi(System.getenv("JENKINS_URL") != null || System.getenv("BUILDKITE_BUILD_URL") != null);
116116
params.setDefaultParallel(ParallelDetector.findDefaultParallel(project));
117117
params.setInFipsJvm(Util.getBooleanProperty("tests.fips.enabled", false));
118118
params.setIsSnapshotBuild(Util.getBooleanProperty("build.snapshot", true));

0 commit comments

Comments
 (0)