Skip to content

Commit 9f43483

Browse files
authored
Add Gradle enterprise setup (#2307) (#2310)
* Add gradle enterprise setup * Tweak commit scan tagging (cherry picked from commit bc96a65)
1 parent 2e8e060 commit 9f43483

File tree

4 files changed

+154
-0
lines changed

4 files changed

+154
-0
lines changed

.buildkite/hooks/pre-command

+10
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,13 @@ if [[ "$USE_DRA_CREDENTIALS" == "true" ]]; then
4646
DRA_VAULT_ADDR=https://secrets.elastic.co:8200
4747
export DRA_VAULT_ADDR
4848
fi
49+
50+
# Initialize the build scan and gobld annotations with empty/open <details> tags
51+
# This ensures that they are collapsible when they get appended to
52+
if [[ "${BUILDKITE_LABEL:-}" == *"Pipeline upload"* || "${BUILDKITE_LABEL:-}" == *"Upload Pipeline"* ]]; then
53+
cat << EOF | buildkite-agent annotate --context "gradle-build-scans" --style "info"
54+
<details>
55+
56+
<summary>Gradle build scan links</summary>
57+
EOF
58+
fi

build.gradle

+122
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
import org.elasticsearch.hadoop.gradle.buildtools.ConcatFilesTask
2+
import java.lang.management.ManagementFactory;
3+
import java.time.LocalDateTime;
24

5+
import org.elasticsearch.gradle.Architecture
6+
import org.elasticsearch.gradle.OS
7+
import static org.elasticsearch.hadoop.gradle.util.CiUtils.safeName
8+
9+
import java.lang.management.ManagementFactory
10+
import java.time.LocalDateTime
311
description = 'Elasticsearch for Apache Hadoop'
412

513
apply plugin: 'es.hadoop.build.root'
@@ -43,3 +51,117 @@ if (project.hasProperty("find-artifact")) {
4351
}
4452
}
4553
}
54+
55+
56+
57+
// Resolving this early to avoid issues with the build scan plugin in combination with the configuration cache usage
58+
def taskNames = gradle.startParameter.taskNames.join(' ')
59+
60+
develocity {
61+
62+
buildScan {
63+
64+
def onCI = System.getenv('CI') ? Boolean.parseBoolean(System.getenv('CI')) : false
65+
66+
// Disable async upload in CI to ensure scan upload completes before CI agent is terminated
67+
uploadInBackground = onCI == false
68+
69+
// Automatically publish scans from Elasticsearch CI
70+
if (onCI) {
71+
publishing.onlyIf { true }
72+
server = 'https://gradle-enterprise.elastic.co'
73+
} else if( server.isPresent() == false) {
74+
publishing.onlyIf { false }
75+
}
76+
77+
background {
78+
tag OS.current().name()
79+
tag Architecture.current().name()
80+
81+
if (onCI) { //Buildkite-specific build scan metadata
82+
String buildKiteUrl = System.getenv('BUILDKITE_BUILD_URL')
83+
def branch = System.getenv('BUILDKITE_PULL_REQUEST_BASE_BRANCH') ?: System.getenv('BUILDKITE_BRANCH')
84+
def repoMatcher = System.getenv('BUILDKITE_REPO') =~ /(https:\/\/github\.com\/|git@github\.com:)(\S+)\.git/
85+
def repository = repoMatcher.matches() ? repoMatcher.group(2) : "<unknown>"
86+
def jobLabel = System.getenv('BUILDKITE_LABEL') ?: ''
87+
def jobName = safeName(jobLabel)
88+
89+
tag 'CI'
90+
link 'CI Build', "${buildKiteUrl}#${System.getenv('BUILDKITE_JOB_ID')}"
91+
value 'Job Number', System.getenv('BUILDKITE_BUILD_NUMBER')
92+
value 'Build ID', System.getenv('BUILDKITE_BUILD_ID')
93+
value 'Job ID', System.getenv('BUILDKITE_JOB_ID')
94+
95+
value 'Pipeline', System.getenv('BUILDKITE_PIPELINE_SLUG')
96+
tag System.getenv('BUILDKITE_PIPELINE_SLUG')
97+
98+
value 'Job Name', jobName
99+
tag jobName
100+
if (jobLabel.contains("/")) {
101+
jobLabel.split("/").collect { safeName(it) }.each { matrix ->
102+
tag matrix
103+
}
104+
}
105+
106+
def uptime = ManagementFactory.getRuntimeMXBean().getUptime() / 1000;
107+
def metricsStartTime = LocalDateTime.now().minusSeconds(uptime.longValue()).minusMinutes(15).toString()
108+
def metricsEndTime = LocalDateTime.now().plusMinutes(15).toString()
109+
110+
link 'Agent Metrics',
111+
"https://es-buildkite-agents.elastic.dev/app/metrics/detail/host/${System.getenv('BUILDKITE_AGENT_NAME')}?_a=(time:(from:%27${metricsStartTime}Z%27,interval:%3E%3D1m,to:%27${metricsEndTime}Z%27))"
112+
link 'Agent Logs',
113+
"https://es-buildkite-agents.elastic.dev/app/logs/stream?logFilter=(filters:!(),query:(language:kuery,query:%27host.name:%20${System.getenv('BUILDKITE_AGENT_NAME')}%27),timeRange:(from:%27${metricsStartTime}Z%27,to:%27${metricsEndTime}Z%27))"
114+
115+
if (branch) {
116+
tag branch
117+
value 'Git Branch', branch
118+
}
119+
120+
// Add SCM information
121+
def prId = System.getenv('BUILDKITE_PULL_REQUEST')
122+
if (prId != 'false') {
123+
def prBaseUrl = (System.getenv('BUILDKITE_PULL_REQUEST_REPO') - ".git").replaceFirst("git://", "https://")
124+
value 'Git Commit ID', System.getenv('BUILDKITE_COMMIT')
125+
tag "pr/${prId}"
126+
tag 'pull-request'
127+
link 'Source', "${prBaseUrl}/tree/${System.getenv('BUILDKITE_COMMIT')}"
128+
link 'Pull Request', "https://github.com/${repository}/pull/${prId}"
129+
} else {
130+
value 'Git Commit ID', System.getenv('BUILDKITE_COMMIT')
131+
link 'Source', "https://github.com/${repository}/tree/${System.getenv('BUILDKITE_COMMIT')}"
132+
}
133+
134+
buildFinished { result ->
135+
136+
buildScanPublished { scan
137+
->
138+
// Attach build scan link as build metadata
139+
// See: https://buildkite.com/docs/pipelines/build-meta-data
140+
new ProcessBuilder('buildkite-agent', 'meta-data', 'set', "build-scan-${System.getenv('BUILDKITE_JOB_ID')}", "${scan.buildScanUri}")
141+
.start()
142+
.waitFor()
143+
144+
// Add a build annotation
145+
// See: https://buildkite.com/docs/agent/v3/cli-annotate
146+
def body = """<div class="mb3"><span class="p1 border rounded">${System.getenv('BUILDKITE_LABEL')}</span> :gradle: ${result.failures ? 'failed' : 'successful'} build: <a href="${scan.buildScanUri}"><code>gradle ${taskNames}</code></a></div>"""
147+
def process = [
148+
'buildkite-agent',
149+
'annotate',
150+
'--context',
151+
result.failures ? 'gradle-build-scans-failed' : 'gradle-build-scans',
152+
'--append',
153+
'--style',
154+
result.failures ? 'error' : 'info'
155+
].execute()
156+
process.withWriter { it.write(body) }
157+
// passing the body in as an argument has issues on Windows, so let's use stdin of the process instead
158+
process.waitFor()
159+
}
160+
}
161+
} else {
162+
tag 'LOCAL'
163+
}
164+
}
165+
}
166+
}
167+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.hadoop.gradle.util;
11+
12+
public class CiUtils {
13+
14+
static String safeName(String input) {
15+
return input.replaceAll("[^a-zA-Z0-9_\\-\\.]+", " ").trim().replaceAll(" ", "_").toLowerCase();
16+
}
17+
18+
}

settings.gradle

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ pluginManagement {
44
}
55
}
66

7+
plugins {
8+
id "com.gradle.develocity" version "3.18.1"
9+
}
10+
711
rootProject.name = "elasticsearch-hadoop"
812

913
include 'thirdparty'

0 commit comments

Comments
 (0)