Skip to content

Commit 51190c7

Browse files
authored
[BUILD] Rework build cache authentication on CI (#122296) (#122618)
This is required to update to newer version of gradle enterprise and the gradle enterprise plugin. (cherry picked from commit 9c19538) # Conflicts: # .ci/init.gradle
1 parent 04917e1 commit 51190c7

File tree

5 files changed

+27
-117
lines changed

5 files changed

+27
-117
lines changed

.buildkite/hooks/pre-command

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ export GRADLE_BUILD_CACHE_USERNAME
4444
GRADLE_BUILD_CACHE_PASSWORD=$(vault read -field=password secret/ci/elastic-elasticsearch/migrated/gradle-build-cache)
4545
export GRADLE_BUILD_CACHE_PASSWORD
4646

47+
DEVELOCITY_ACCESS_KEY="gradle-enterprise.elastic.co=$(vault read -field=accesskey secret/ci/elastic-elasticsearch/migrated/gradle-build-cache)"
48+
export DEVELOCITY_ACCESS_KEY
49+
4750
BUILDKITE_API_TOKEN=$(vault read -field=token secret/ci/elastic-elasticsearch/buildkite-api-token)
4851
export BUILDKITE_API_TOKEN
4952

.ci/init.gradle

Lines changed: 12 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -1,126 +1,22 @@
1-
import com.bettercloud.vault.VaultConfig
2-
import com.bettercloud.vault.Vault
3-
4-
initscript {
5-
repositories {
6-
mavenCentral()
7-
}
8-
dependencies {
9-
classpath 'com.bettercloud:vault-java-driver:4.1.0'
10-
}
11-
}
12-
13-
boolean USE_ARTIFACTORY = false
14-
15-
if (System.getenv('VAULT_ADDR') == null) {
16-
// When trying to reproduce errors outside of CI, it can be useful to allow this to just return rather than blowing up
17-
if (System.getenv('CI') == null) {
18-
return
19-
}
20-
21-
throw new GradleException("You must set the VAULT_ADDR environment variable to use this init script.")
22-
}
23-
24-
if (System.getenv('VAULT_ROLE_ID') == null && System.getenv('VAULT_SECRET_ID') == null && System.getenv('VAULT_TOKEN') == null) {
25-
// When trying to reproduce errors outside of CI, it can be useful to allow this to just return rather than blowing up
26-
if (System.getenv('CI') == null) {
27-
return
28-
}
29-
30-
throw new GradleException("You must set either the VAULT_ROLE_ID and VAULT_SECRET_ID environment variables, " +
31-
"or the VAULT_TOKEN environment variable to use this init script.")
32-
}
33-
34-
final String vaultPathPrefix = System.getenv('VAULT_ADDR') ==~ /.+vault-ci.+\.dev.*/ ? "secret/ci/elastic-elasticsearch/migrated" : "secret/elasticsearch-ci"
35-
36-
final String vaultToken = System.getenv('VAULT_TOKEN') ?: new Vault(
37-
new VaultConfig()
38-
.address(System.env.VAULT_ADDR)
39-
.engineVersion(1)
40-
.build()
41-
)
42-
.withRetries(5, 1000)
43-
.auth()
44-
.loginByAppRole("approle", System.env.VAULT_ROLE_ID, System.env.VAULT_SECRET_ID)
45-
.getAuthClientToken()
46-
47-
final Vault vault = new Vault(
48-
new VaultConfig()
49-
.address(System.env.VAULT_ADDR)
50-
.engineVersion(1)
51-
.token(vaultToken)
52-
.build()
53-
)
54-
.withRetries(5, 1000)
55-
56-
57-
if (USE_ARTIFACTORY) {
58-
final Map<String, String> artifactoryCredentials = vault.logical()
59-
.read("${vaultPathPrefix}/artifactory.elstc.co")
60-
.getData()
61-
logger.info("Using elastic artifactory repos")
62-
Closure configCache = {
63-
return {
64-
name "artifactory-gradle-release"
65-
url "https://artifactory.elstc.co/artifactory/gradle-release"
66-
credentials {
67-
username artifactoryCredentials.get("username")
68-
password artifactoryCredentials.get("token")
69-
}
70-
}
71-
}
72-
settingsEvaluated { settings ->
73-
settings.pluginManagement {
74-
repositories {
75-
maven configCache()
76-
}
77-
}
78-
}
79-
projectsLoaded {
80-
allprojects {
81-
buildscript {
82-
repositories {
83-
maven configCache()
84-
}
85-
}
86-
repositories {
87-
maven configCache()
88-
}
89-
}
90-
}
91-
}
1+
final String buildCacheUrl = System.getProperty('org.elasticsearch.build.cache.url')
2+
final boolean buildCachePush = Boolean.valueOf(System.getProperty('org.elasticsearch.build.cache.push', 'false'))
923

934
gradle.settingsEvaluated { settings ->
945
settings.pluginManager.withPlugin("com.gradle.develocity") {
956
settings.develocity {
96-
server = 'https://gradle-enterprise.elastic.co'
7+
server = "https://gradle-enterprise.elastic.co"
978
}
98-
}
99-
}
100-
101-
102-
final String buildCacheUrl = System.getProperty('org.elasticsearch.build.cache.url')
103-
final boolean buildCachePush = Boolean.valueOf(System.getProperty('org.elasticsearch.build.cache.push', 'false'))
104-
105-
if (buildCacheUrl) {
106-
final Map<String, String> buildCacheCredentials = System.getenv("GRADLE_BUILD_CACHE_USERNAME") ? [:] : vault.logical()
107-
.read("${vaultPathPrefix}/gradle-build-cache")
108-
.getData()
109-
gradle.settingsEvaluated { settings ->
110-
settings.buildCache {
111-
local {
112-
// Disable the local build cache in CI since we use ephemeral workers and it incurs an IO penalty
113-
enabled = false
114-
}
115-
remote(HttpBuildCache) {
116-
url = buildCacheUrl
117-
push = buildCachePush
118-
credentials {
119-
username = System.getenv("GRADLE_BUILD_CACHE_USERNAME") ?: buildCacheCredentials.get("username")
120-
password = System.getenv("GRADLE_BUILD_CACHE_PASSWORD") ?: buildCacheCredentials.get("password")
9+
if (buildCacheUrl) {
10+
settings.buildCache {
11+
local {
12+
// Disable the local build cache in CI since we use ephemeral workers and it incurs an IO penalty
13+
enabled = false
14+
}
15+
remote(settings.develocity.buildCache) {
16+
enabled = true
17+
push = buildCachePush
12118
}
12219
}
12320
}
12421
}
12522
}
126-

build-conventions/settings.gradle

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
* your election, the "Elastic License 2.0", the "GNU Affero General Public
77
* License v3.0 only", or the "Server Side Public License, v 1".
88
*/
9+
10+
plugins {
11+
id "com.gradle.develocity" version "3.18.1"
12+
}
13+
914
rootProject.name = 'build-conventions'
1015

1116
dependencyResolutionManagement {

build-tools-internal/settings.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ pluginManagement {
88
includeBuild "../build-tools"
99
}
1010

11+
plugins {
12+
id "com.gradle.develocity" version "3.18.1"
13+
}
14+
1115
dependencyResolutionManagement {
1216
versionCatalogs {
1317
buildLibs {

build-tools/settings.gradle

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
pluginManagement {
1010
includeBuild "../build-conventions"
1111
}
12-
12+
plugins {
13+
id "com.gradle.develocity" version "3.18.1"
14+
}
1315
include 'reaper'
1416

1517
dependencyResolutionManagement {

0 commit comments

Comments
 (0)