gitjenkinsmaven-3buildnumber-maven-plugin

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


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?


Solution

  • 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.