jenkins Configure Auto Git Push on Successful Build in Jenkins Configuring the Auto Push Job


Example

Create a build job (according to your requirement). For this example I have created a freestyle job (AutoPush) to perform ANT build.

We are going to create two variables, PUSH (Choice Parameter) and TAG_NUMBER (String Parameter).

We can choose the value YES or NO for PUSH, this will decide whether to push the code to a tag or not on successful build.

We can specify a tag name (ex. 1.0.1) for TAG_NUMBER to create a new tag (ex. 1.0.1) in the remote repository with the same name or specify an existing tag name to update an existing tag.

enter image description here

Now let’s move on to the job configuration.

  1. Check the “This project is parameterized” checkbox and create a Choice Parameter called “PUSH” and provide YES and NO as the choices. This parameter will decide whether you want to push the code to a specific Tag/Release or not.

enter image description here

  1. Then create a String Parameter called “TAG_NUMBER”, using this parameter we can specify a new tag number to create a new tag or specify an existing tag number to update an existing tag.

enter image description here

  1. In Source Code Management section choose Git and provide the repository URL. This repository contains the source code that you are going to build and after a successful build a release tag will be created on the same repository.

enter image description here

  1. After adding the repository details click on advanced and provide a name to your repository which will later get referred in the Git Publisher plugin to identify the repository.

enter image description here

enter image description here

  1. Then add the build step. In this example I am building an ANT project.

enter image description here

  1. Now in “Post-build Actions” section select “Flexi Publish” plugin. Select the value “And” from the dropdown for Conditional action (Run?). Then select “String Match” from the dropdown for the Run condition (&&).

enter image description here

enter image description here

enter image description here

  1. After selecting the string match specify $PUSH as String 1 value and YES as String 2 value. So when you will run the build if you choose the value of PUSH as YES, it will compare the String 1 (=$PUSH) and String 2 (=YES) and trigger the Git push operation and if you choose NO it won’t trigger the Git push operation.
Choose the value of PUSH -> YES OR NO -> Chosen value “YES”
then, $PUSH = YES
AS String 1 = $PUSH => String 1 = YES
Again, String 2 = YES, hence String 2 == String 1 (String match)
Then, trigger the Git push action. 

enter image description here

  1. Now click on Add dropdown option to add the Git publisher action that will be triggered on the basis of the string match condition.

enter image description here

enter image description here

  1. After selecting Git Publisher, do the configuration as follows:

enter image description here

After the configuration save the job and you are done.