Skip to content

Commit 6d98bd9

Browse files
committed
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
1 parent 8c6d884 commit 6d98bd9

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

buildSrc/src/main/groovy/org/elasticsearch/gradle/BuildPlugin.groovy

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ package org.elasticsearch.gradle
2121
import com.carrotsearch.gradle.junit4.RandomizedTestingTask
2222
import nebula.plugin.extraconfigurations.ProvidedBasePlugin
2323
import org.apache.tools.ant.taskdefs.condition.Os
24+
import org.eclipse.jgit.lib.Constants
25+
import org.eclipse.jgit.lib.RepositoryBuilder
2426
import org.elasticsearch.gradle.precommit.PrecommitTasks
2527
import org.gradle.api.GradleException
2628
import org.gradle.api.InvalidUserDataException
@@ -35,7 +37,6 @@ import org.gradle.api.artifacts.ModuleVersionIdentifier
3537
import org.gradle.api.artifacts.ProjectDependency
3638
import org.gradle.api.artifacts.ResolvedArtifact
3739
import org.gradle.api.artifacts.dsl.RepositoryHandler
38-
import org.gradle.api.file.CopySpec
3940
import org.gradle.api.plugins.JavaPlugin
4041
import org.gradle.api.publish.maven.MavenPublication
4142
import org.gradle.api.publish.maven.plugins.MavenPublishPlugin
@@ -508,6 +509,17 @@ class BuildPlugin implements Plugin<Project> {
508509
if (jarTask.manifest.attributes.containsKey('Change') == false) {
509510
logger.warn('Building without git revision id.')
510511
jarTask.manifest.attributes('Change': 'Unknown')
512+
} else {
513+
/*
514+
* The info-scm plugin assumes that if GIT_COMMIT is set it was set by Jenkins to the commit hash for this build.
515+
* However, that assumption is wrong as this build could be a sub-build of another Jenkins build for which GIT_COMMIT
516+
* is the commit hash for that build. Therefore, if GIT_COMMIT is set we calculate the commit hash ourselves.
517+
*/
518+
if (System.getenv("GIT_COMMIT") != null) {
519+
final String hash = new RepositoryBuilder().findGitDir(project.buildDir).build().resolve(Constants.HEAD).name
520+
final String shortHash = hash?.substring(0, 7)
521+
jarTask.manifest.attributes('Change': shortHash)
522+
}
511523
}
512524
}
513525
// add license/notice files

0 commit comments

Comments
 (0)