Skip to content

Commit 80ff83e

Browse files
gradle task migrate to the new artifacts-api (#17232) (#17237)
This commit migrates gradle task to the new artifacts-api - remove dependency on staging artifacts - all builds use snapshot artifacts - resolve version from current branch, major.x, previous minor, with priority given in that order. Co-authored-by: Andrea Selva <[email protected]> (cherry picked from commit 0a74568) Co-authored-by: kaisecheng <[email protected]>
1 parent 7416df2 commit 80ff83e

File tree

1 file changed

+31
-66
lines changed

1 file changed

+31
-66
lines changed

build.gradle

+31-66
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@ subprojects {
146146
}
147147

148148
version = versionMap['logstash-core']
149-
String artifactVersionsApi = "https://artifacts-api.elastic.co/v1/versions"
150149

151150
tasks.register("configureArchitecture") {
152151
String arch = System.properties['os.arch']
@@ -172,33 +171,28 @@ tasks.register("configureArtifactInfo") {
172171
description "Set the url to download stack artifacts for select stack version"
173172

174173
doLast {
175-
def versionQualifier = System.getenv('VERSION_QUALIFIER')
176-
if (versionQualifier) {
177-
version = "$version-$versionQualifier"
178-
}
179-
180-
boolean isReleaseBuild = System.getenv('RELEASE') == "1" || versionQualifier
181-
String apiResponse = artifactVersionsApi.toURL().text
182-
183-
def dlVersions = new JsonSlurper().parseText(apiResponse)
184-
String qualifiedVersion = dlVersions['versions'].grep(isReleaseBuild ? ~/^${version}$/ : ~/^${version}-SNAPSHOT/)[0]
185-
if (qualifiedVersion == null) {
186-
if (!isReleaseBuild) {
187-
project.ext.set("useProjectSpecificArtifactSnapshotUrl", true)
188-
project.ext.set("stackArtifactSuffix", "${version}-SNAPSHOT")
189-
return
174+
def splitVersion = version.split('\\.')
175+
int major = splitVersion[0].toInteger()
176+
int minor = splitVersion[1].toInteger()
177+
String branch = "${major}.${minor}"
178+
String fallbackMajorX = "${major}.x"
179+
boolean isFallBackPreviousMajor = minor - 1 < 0
180+
String fallbackBranch = isFallBackPreviousMajor ? "${major-1}.x" : "${major}.${minor-1}"
181+
def qualifiedVersion = ""
182+
183+
for (b in [branch, fallbackMajorX, fallbackBranch]) {
184+
def url = "https://storage.googleapis.com/artifacts-api/snapshots/${b}.json"
185+
try {
186+
def snapshotInfo = new JsonSlurper().parseText(url.toURL().text)
187+
qualifiedVersion = snapshotInfo.version
188+
println "ArtifactInfo version: ${qualifiedVersion}"
189+
break
190+
} catch (Exception e) {
191+
println "Failed to fetch branch ${branch} from ${url}: ${e.message}"
190192
}
191-
throw new GradleException("could not find the current artifact from the artifact-api ${artifactVersionsApi} for ${version}")
192193
}
193-
// find latest reference to last build
194-
String buildsListApi = "${artifactVersionsApi}/${qualifiedVersion}/builds/"
195-
apiResponse = buildsListApi.toURL().text
196-
def dlBuilds = new JsonSlurper().parseText(apiResponse)
197-
def stackBuildVersion = dlBuilds["builds"][0]
198194

199-
project.ext.set("artifactApiVersionedBuildUrl", "${artifactVersionsApi}/${qualifiedVersion}/builds/${stackBuildVersion}")
200-
project.ext.set("stackArtifactSuffix", qualifiedVersion)
201-
project.ext.set("useProjectSpecificArtifactSnapshotUrl", false)
195+
project.ext.set("artifactApiVersion", qualifiedVersion)
202196
}
203197
}
204198

@@ -441,23 +435,13 @@ tasks.register("downloadFilebeat") {
441435

442436
doLast {
443437
download {
444-
String beatVersion = project.ext.get("stackArtifactSuffix")
445-
String downloadedFilebeatName = "filebeat-${beatVersion}-${project.ext.get("beatsArchitecture")}"
438+
String beatsVersion = project.ext.get("artifactApiVersion")
439+
String downloadedFilebeatName = "filebeat-${beatsVersion}-${project.ext.get("beatsArchitecture")}"
446440
project.ext.set("unpackedFilebeatName", downloadedFilebeatName)
447441

448-
if (project.ext.get("useProjectSpecificArtifactSnapshotUrl")) {
449-
def res = SnapshotArtifactURLs.packageUrls("beats", beatVersion, downloadedFilebeatName)
450-
project.ext.set("filebeatSnapshotUrl", System.getenv("FILEBEAT_SNAPSHOT_URL") ?: res.packageUrl)
451-
project.ext.set("filebeatDownloadLocation", "${projectDir}/build/${downloadedFilebeatName}.tar.gz")
452-
} else {
453-
// find url of build artifact
454-
String artifactApiUrl = "${project.ext.get("artifactApiVersionedBuildUrl")}/projects/beats/packages/${downloadedFilebeatName}.tar.gz"
455-
String apiResponse = artifactApiUrl.toURL().text
456-
def buildUrls = new JsonSlurper().parseText(apiResponse)
457-
458-
project.ext.set("filebeatSnapshotUrl", System.getenv("FILEBEAT_SNAPSHOT_URL") ?: buildUrls["package"]["url"])
459-
project.ext.set("filebeatDownloadLocation", "${projectDir}/build/${downloadedFilebeatName}.tar.gz")
460-
}
442+
def res = SnapshotArtifactURLs.packageUrls("beats", beatsVersion, downloadedFilebeatName)
443+
project.ext.set("filebeatSnapshotUrl", System.getenv("FILEBEAT_SNAPSHOT_URL") ?: res.packageUrl)
444+
project.ext.set("filebeatDownloadLocation", "${projectDir}/build/${downloadedFilebeatName}.tar.gz")
461445

462446
src project.ext.filebeatSnapshotUrl
463447
onlyIfNewer true
@@ -493,20 +477,12 @@ tasks.register("checkEsSHA") {
493477
description "Download ES version remote's fingerprint file"
494478

495479
doLast {
496-
String esVersion = project.ext.get("stackArtifactSuffix")
480+
String esVersion = project.ext.get("artifactApiVersion")
497481
String downloadedElasticsearchName = "elasticsearch-${esVersion}-${project.ext.get("esArchitecture")}"
498482
String remoteSHA
499483

500-
if (project.ext.get("useProjectSpecificArtifactSnapshotUrl")) {
501-
def res = SnapshotArtifactURLs.packageUrls("elasticsearch", esVersion, downloadedElasticsearchName)
502-
remoteSHA = res.packageShaUrl
503-
} else {
504-
// find url of build artifact
505-
String artifactApiUrl = "${project.ext.get("artifactApiVersionedBuildUrl")}/projects/elasticsearch/packages/${downloadedElasticsearchName}.tar.gz"
506-
String apiResponse = artifactApiUrl.toURL().text
507-
def buildUrls = new JsonSlurper().parseText(apiResponse)
508-
remoteSHA = buildUrls.package.sha_url.toURL().text
509-
}
484+
def res = SnapshotArtifactURLs.packageUrls("elasticsearch", esVersion, downloadedElasticsearchName)
485+
remoteSHA = res.packageShaUrl
510486

511487
def localESArchive = new File("${projectDir}/build/${downloadedElasticsearchName}.tar.gz")
512488
if (localESArchive.exists()) {
@@ -540,25 +516,14 @@ tasks.register("downloadEs") {
540516

541517
doLast {
542518
download {
543-
String esVersion = project.ext.get("stackArtifactSuffix")
519+
String esVersion = project.ext.get("artifactApiVersion")
544520
String downloadedElasticsearchName = "elasticsearch-${esVersion}-${project.ext.get("esArchitecture")}"
545521

546522
project.ext.set("unpackedElasticsearchName", "elasticsearch-${esVersion}")
547523

548-
if (project.ext.get("useProjectSpecificArtifactSnapshotUrl")) {
549-
def res = SnapshotArtifactURLs.packageUrls("elasticsearch", esVersion, downloadedElasticsearchName)
550-
project.ext.set("elasticsearchSnapshotURL", System.getenv("ELASTICSEARCH_SNAPSHOT_URL") ?: res.packageUrl)
551-
project.ext.set("elasticsearchDownloadLocation", "${projectDir}/build/${downloadedElasticsearchName}.tar.gz")
552-
} else {
553-
// find url of build artifact
554-
String artifactApiUrl = "${project.ext.get("artifactApiVersionedBuildUrl")}/projects/elasticsearch/packages/${downloadedElasticsearchName}.tar.gz"
555-
String apiResponse = artifactApiUrl.toURL().text
556-
557-
def buildUrls = new JsonSlurper().parseText(apiResponse)
558-
559-
project.ext.set("elasticsearchSnapshotURL", System.getenv("ELASTICSEARCH_SNAPSHOT_URL") ?: buildUrls["package"]["url"])
560-
project.ext.set("elasticsearchDownloadLocation", "${projectDir}/build/${downloadedElasticsearchName}.tar.gz")
561-
}
524+
def res = SnapshotArtifactURLs.packageUrls("elasticsearch", esVersion, downloadedElasticsearchName)
525+
project.ext.set("elasticsearchSnapshotURL", System.getenv("ELASTICSEARCH_SNAPSHOT_URL") ?: res.packageUrl)
526+
project.ext.set("elasticsearchDownloadLocation", "${projectDir}/build/${downloadedElasticsearchName}.tar.gz")
562527

563528
src project.ext.elasticsearchSnapshotURL
564529
onlyIfNewer true

0 commit comments

Comments
 (0)