0

Jenkins Git Plugin sets GIT_COMMIT environment variable with Git commit SHA1 id which is 40 characters long and I need to shorten it to 6 characters. Ant captures GIT_COMMIT environment variable and I somehow should shorten it.

Ali Sadik Kumlali
  • 631
  • 1
  • 6
  • 14

1 Answers1

0

Here is the Ant script that works for me:

<property environment="env"/>

<!-- https://stackoverflow.com/a/37896164/5903564 -->
<macrodef name="getsubstring">
  <attribute name="src"/>
  <attribute name="start"/>
  <attribute name="length"/>
  <attribute name="result"/>
  <sequential>
    <loadresource property="@{result}">
      <string value="@{src}}" />
        <filterchain>
          <tokenfilter>
            <replaceregex pattern="^.{@{start}}(.{@{length}}).*" replace="\1" />
          </tokenfilter>
        </filterchain>
    </loadresource>
  </sequential>
</macrodef>

<!-- Get the first 6 characters of git commit id -->
<getsubstring src="${env.GIT_COMMIT}" start="0" length="6" result="short-commit-id"/>

<echo>Short Git commit id: ${short-commit-id}</echo>

If I did not use Jenkins and did not have GIT_COMMIT variable, I would follow How to lookup the latest git commit hash from an ant build script to get commit id, then shorten it.

Ali Sadik Kumlali
  • 631
  • 1
  • 6
  • 14