OpenSearch/distribution/bwc/build.gradle

158 lines
5.9 KiB
Groovy

/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import org.elasticsearch.gradle.LoggedExec
/**
* This is a dummy project which does a local checkout of the previous
* wire compat version's branch, and builds a snapshot. This allows backcompat
* tests to test against the next unreleased version, closest to this version,
* without relying on snapshots.
*/
String bwcVersion
boolean enabled = true
if (project.name == 'bwc-stable-snapshot') {
/* bwc-stable is only used if the last version is on a stable branch instead
* of a bugfix branch */
enabled = indexCompatVersions[-1].bugfix == 0
bwcVersion = indexCompatVersions[-1]
} else if (project.name == 'bwc-release-snapshot') {
if (indexCompatVersions[-1].bugfix == 0) {
/* The last version is on a stable branch so it is handled by the bwc-stable
* project. This project will instead handle the version before that which
* *should* be on a stable branch. */
bwcVersion = indexCompatVersions[-2]
} else {
// The last version is on a release branch so it is handled by this project
bwcVersion = indexCompatVersions[-1]
}
} else {
throw new InvalidUserDataException("Unsupport project name ${project.name}")
}
if (enabled) {
apply plugin: 'distribution'
// Not published so no need to assemble
tasks.remove(assemble)
build.dependsOn.remove('assemble')
def (String major, String minor, String bugfix) = bwcVersion.split('\\.')
def (String currentMajor, String currentMinor, String currentBugfix) = version.split('\\.')
String bwcBranch
if (project.name == 'bwc-stable-snapshot') {
bwcBranch = "${major}.x"
} else {
bwcBranch = "${major}.${minor}"
}
File checkoutDir = file("${buildDir}/bwc/checkout-${bwcBranch}")
task createClone(type: LoggedExec) {
onlyIf { checkoutDir.exists() == false }
commandLine = ['git', 'clone', rootDir, checkoutDir]
}
// we use regular Exec here to ensure we always get output, regardless of logging level
task findUpstream(type: Exec) {
dependsOn createClone
workingDir = checkoutDir
commandLine = ['git', 'remote', '-v']
ignoreExitValue = true
ByteArrayOutputStream output = new ByteArrayOutputStream()
standardOutput = output
doLast {
if (execResult.exitValue != 0) {
output.toString('UTF-8').eachLine { line -> logger.error(line) }
execResult.assertNormalExitValue()
}
project.ext.upstreamExists = false
output.toString('UTF-8').eachLine {
if (it.contains("upstream")) {
project.ext.upstreamExists = true
}
}
}
}
task addUpstream(type: LoggedExec) {
dependsOn findUpstream
onlyIf { project.ext.upstreamExists == false }
workingDir = checkoutDir
commandLine = ['git', 'remote', 'add', 'upstream', 'https://github.com/elastic/elasticsearch.git']
}
task fetchLatest(type: LoggedExec) {
onlyIf { project.gradle.startParameter.isOffline() == false }
dependsOn addUpstream
workingDir = checkoutDir
commandLine = ['git', 'fetch', '--all']
}
// this is an Exec task so that the SHA that is checked out is logged
String buildMetadataKey = "bwc_refspec_${project.path.substring(1)}"
task checkoutBwcBranch(type: Exec) {
def String refspec = System.getProperty("tests.bwc.refspec", buildMetadata.get(buildMetadataKey, "upstream/${bwcBranch}"))
dependsOn fetchLatest
workingDir = checkoutDir
commandLine = ['git', 'checkout', refspec]
}
File buildMetadataFile = project.file("build/${project.name}/build_metadata")
task writeBuildMetadata(type: LoggedExec) {
dependsOn checkoutBwcBranch
workingDir = checkoutDir
commandLine = ['git', 'rev-parse', 'HEAD']
ignoreExitValue = true
ByteArrayOutputStream output = new ByteArrayOutputStream()
standardOutput = output
doLast {
if (execResult.exitValue != 0) {
output.toString('UTF-8').eachLine { line -> logger.error(line) }
execResult.assertNormalExitValue()
}
project.mkdir(buildMetadataFile.parent)
buildMetadataFile.setText("${buildMetadataKey}=${output.toString('UTF-8')}", 'UTF-8')
}
}
File bwcDeb = file("${checkoutDir}/distribution/deb/build/distributions/elasticsearch-${bwcVersion}.deb")
File bwcRpm = file("${checkoutDir}/distribution/rpm/build/distributions/elasticsearch-${bwcVersion}.rpm")
File bwcZip = file("${checkoutDir}/distribution/zip/build/distributions/elasticsearch-${bwcVersion}.zip")
task buildBwcVersion(type: GradleBuild) {
dependsOn checkoutBwcBranch, writeBuildMetadata
dir = checkoutDir
tasks = [':distribution:deb:assemble', ':distribution:rpm:assemble', ':distribution:zip:assemble']
doLast {
List missing = [bwcDeb, bwcRpm, bwcZip].grep { file ->
false == file.exists() }
if (false == missing.empty) {
throw new InvalidUserDataException(
"Building bwc version didn't generate expected files ${missing}")
}
}
}
artifacts {
'default' file: bwcDeb, name: 'elasticsearch', type: 'deb', builtBy: buildBwcVersion
'default' file: bwcRpm, name: 'elasticsearch', type: 'rpm', builtBy: buildBwcVersion
'default' file: bwcZip, name: 'elasticsearch', type: 'zip', builtBy: buildBwcVersion
}
}