Search code examples
gitjenkinsgithubant

Would like to compare git commit differences from Jenkins using a build.xml file


I am trying to do Salesforce deployment using Jenkins from a git-hub branch. My need is to do delta deployment hence i need to compare the current commit and previous commit in branch. Based on commit difference i would like to create a src folder with the files which got changed between two commits. I am trying to collect current and previous commit using env.GIT_COMMIT and env.GIT_PREVIOUS_SUCCESSFUL_COMMIT, in below way:

<macrodef name="gitDiff">
		<sequential>
			<exec executable="git" outputproperty="git.diff">
				<arg value="diff" />
				<arg value="env.GIT_COMMIT" />
				<arg value="env.GIT_PREVIOUS_SUCCESSFUL_COMMIT" />
			</exec>
			<echo message="Git difference is ${git.diff}."/>
		</sequential>
</macrodef>

When i am trying to print current and previous commit i am not getting any values.

<echo>Current GIT Commit : ${env.GIT_COMMIT}</echo>
<echo>Previous Known Successful GIT Commit : ${env.GIT_PREVIOUS_SUCCESSFUL_COMMIT}</echo>

Output of ant target is like below:

Property "env.GIT_COMMIT" has not been set
[echo] Current GIT Commit : ${env.GIT_COMMIT}
Property "env.GIT_PREVIOUS_SUCCESSFUL_COMMIT" has not been set
[echo] Previous Known Successful GIT Commit : ${env.GIT_PREVIOUS_SUCCESSFUL_COMMIT}

Any suggestions how i can get the values in env.GIT_COMMIT env.GIT_PREVIOUS_SUCCESSFUL_COMMIT


Solution

  • First, you need to set a prefix for your environment variables with the property task.

    <property environment="env" />
    

    The other problem is that the environment variables in your git exec task are set as literal text instead of property references. You need to use the ${} syntax:

    <macrodef name="gitDiff">
        <sequential>
            <exec executable="git" outputproperty="git.diff">
                <arg value="diff" />
                <arg value="${env.GIT_COMMIT}" />
                <arg value="${env.GIT_PREVIOUS_SUCCESSFUL_COMMIT}" />
            </exec>
            <echo message="Git difference is ${git.diff}."/>
        </sequential>
    </macrodef>