Skip to content

Commit 876ff03

Browse files
kaisechengandsel
authored andcommitted
gradle task migrate to the new artifacts-api (#17232)
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)
1 parent c6149e6 commit 876ff03

File tree

1 file changed

+31
-66
lines changed

1 file changed

+31
-66
lines changed

build.gradle

Lines changed: 31 additions & 66 deletions
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

@@ -437,23 +431,13 @@ tasks.register("downloadFilebeat") {
437431

438432
doLast {
439433
download {
440-
String beatVersion = project.ext.get("stackArtifactSuffix")
441-
String downloadedFilebeatName = "filebeat-${beatVersion}-${project.ext.get("beatsArchitecture")}"
434+
String beatsVersion = project.ext.get("artifactApiVersion")
435+
String downloadedFilebeatName = "filebeat-${beatsVersion}-${project.ext.get("beatsArchitecture")}"
442436
project.ext.set("unpackedFilebeatName", downloadedFilebeatName)
443437

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

458442
src project.ext.filebeatSnapshotUrl
459443
onlyIfNewer true
@@ -489,20 +473,12 @@ tasks.register("checkEsSHA") {
489473
description "Download ES version remote's fingerprint file"
490474

491475
doLast {
492-
String esVersion = project.ext.get("stackArtifactSuffix")
476+
String esVersion = project.ext.get("artifactApiVersion")
493477
String downloadedElasticsearchName = "elasticsearch-${esVersion}-${project.ext.get("esArchitecture")}"
494478
String remoteSHA
495479

496-
if (project.ext.get("useProjectSpecificArtifactSnapshotUrl")) {
497-
def res = SnapshotArtifactURLs.packageUrls("elasticsearch", esVersion, downloadedElasticsearchName)
498-
remoteSHA = res.packageShaUrl
499-
} else {
500-
// find url of build artifact
501-
String artifactApiUrl = "${project.ext.get("artifactApiVersionedBuildUrl")}/projects/elasticsearch/packages/${downloadedElasticsearchName}.tar.gz"
502-
String apiResponse = artifactApiUrl.toURL().text
503-
def buildUrls = new JsonSlurper().parseText(apiResponse)
504-
remoteSHA = buildUrls.package.sha_url.toURL().text
505-
}
480+
def res = SnapshotArtifactURLs.packageUrls("elasticsearch", esVersion, downloadedElasticsearchName)
481+
remoteSHA = res.packageShaUrl
506482

507483
def localESArchive = new File("${projectDir}/build/${downloadedElasticsearchName}.tar.gz")
508484
if (localESArchive.exists()) {
@@ -536,25 +512,14 @@ tasks.register("downloadEs") {
536512

537513
doLast {
538514
download {
539-
String esVersion = project.ext.get("stackArtifactSuffix")
515+
String esVersion = project.ext.get("artifactApiVersion")
540516
String downloadedElasticsearchName = "elasticsearch-${esVersion}-${project.ext.get("esArchitecture")}"
541517

542518
project.ext.set("unpackedElasticsearchName", "elasticsearch-${esVersion}")
543519

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

559524
src project.ext.elasticsearchSnapshotURL
560525
onlyIfNewer true

0 commit comments

Comments
 (0)