Ignore GIT_COMMIT when calculating commit hash

When finding the commit hash for the build to place in the JAR manifest
(which is used to identity the build), the scm-info plugin assumes that
GIT_COMMIT is the commit for this build. That assumption is wrong, this
build could be a sub-build of another build that GIT_COMMIT belongs
to. If GIT_COMMIT is set, we ignore the commit hash calculated by
scm-info and calculate the hash ourselves.

Relates #28082
This commit is contained in:
Jason Tedor 2018-01-04 14:58:34 -05:00 committed by GitHub
parent cb2334a78b
commit 4656c55639
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 13 additions and 1 deletions

View File

@ -21,6 +21,8 @@ package org.elasticsearch.gradle
import com.carrotsearch.gradle.junit4.RandomizedTestingTask
import nebula.plugin.extraconfigurations.ProvidedBasePlugin
import org.apache.tools.ant.taskdefs.condition.Os
import org.eclipse.jgit.lib.Constants
import org.eclipse.jgit.lib.RepositoryBuilder
import org.elasticsearch.gradle.precommit.PrecommitTasks
import org.gradle.api.GradleException
import org.gradle.api.InvalidUserDataException
@ -35,7 +37,6 @@ import org.gradle.api.artifacts.ModuleVersionIdentifier
import org.gradle.api.artifacts.ProjectDependency
import org.gradle.api.artifacts.ResolvedArtifact
import org.gradle.api.artifacts.dsl.RepositoryHandler
import org.gradle.api.file.CopySpec
import org.gradle.api.plugins.JavaPlugin
import org.gradle.api.publish.maven.MavenPublication
import org.gradle.api.publish.maven.plugins.MavenPublishPlugin
@ -509,6 +510,17 @@ class BuildPlugin implements Plugin<Project> {
if (jarTask.manifest.attributes.containsKey('Change') == false) {
logger.warn('Building without git revision id.')
jarTask.manifest.attributes('Change': 'Unknown')
} else {
/*
* The info-scm plugin assumes that if GIT_COMMIT is set it was set by Jenkins to the commit hash for this build.
* However, that assumption is wrong as this build could be a sub-build of another Jenkins build for which GIT_COMMIT
* is the commit hash for that build. Therefore, if GIT_COMMIT is set we calculate the commit hash ourselves.
*/
if (System.getenv("GIT_COMMIT") != null) {
final String hash = new RepositoryBuilder().findGitDir(project.buildDir).build().resolve(Constants.HEAD).name
final String shortHash = hash?.substring(0, 7)
jarTask.manifest.attributes('Change': shortHash)
}
}
}
// add license/notice files