Get git SHA-1 of code merged by jenkins into maven

1.5k views Asked by At

I am using the "Merge before build" option of the git plugin to build a maven job. I have tried using ${GIT_BRANCH} and ${GIT_COMMIT} in my maven pom to write the commit information into my built artifacts but these variable are set to the branch being merged and its commit SHA-1.

Is there a way that I can find the SHA-1 of merged code and pass it into maven?

2

There are 2 answers

1
Stephen Connolly On BEST ANSWER

The Mojo's Buildnumber Maven Plugin can fetch this information for you.

The project site has not been fully updated to reflect the fact that it works with GIT.

<build>
  <plugins>
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>buildnumber-maven-plugin</artifactId>
      <version>1.1</version>
      <executions>
        <execution>
          <phase>validate</phase>
          <goals>
            <goal>create</goal>
          </goals>
        </execution>
      </executions>
      <configuration>
        <doCheck>true</doCheck>
        <doUpdate>true</doUpdate>
      </configuration>
    </plugin>
  </plugins>
</build>

That will set the ${buildNumber} property to the full GIT hash of your workspace.

It also will have the side-effect of ensuring that the build is the same on either a developers machine or the CI build server.

0
Aaron Digulla On

You can also try the maven git commit id plugin. It will provide you with a lot of information from Git, it supports fancy formatting and many different use cases.

Direct link to the documentation.