3
votes

I want to trigger jenkins build if a new tag is pushed to a remote repository. I have seen number of posts, but none seems to be working for me. My build is triggered successfully when I push a tag having new commits in it, but if I push a new tag on old commits it does not trigger the build.

I have configured it using git plugin in Jenkins and adding Refscpec value as +refs/tags/*:refs/remotes/origin/tags/* and Branch specifier as */tags/*

Now if run:

git push origin master
git tag release-v1
git push origin release-v1

Build is triggered successfully for tag release-v1 But now if i do:

git push origin release-v2

Build is not triggered.

This means jenkins is always looking for commit ids, if there is a new commit id linked with the tag it will build the job. But i want the jenkins job to run in case I want to release already committed code for another feature with a new tag name.

1
Is the new tag on the same commit? - evolutionxbox
@evolutionxbox Yes it is on same commit - jass
In a similar question I showed how you can run a build when a tag exists, but the code can easily be tweaked only to run when a tag with a certain pattern exists. --- That being said, there's no real reason to re-run a build on the same commit, unless the build scripts have changed. - evolutionxbox
@evolutionxbox Actually I have a reason to run a build on same commit. I want to merge my code to master branch, tag it as 'beta-release-x' and deploy beta pipeline. Once beta testing is complete, I want to tag the same code as 'production-release-x' and deploy the production pipeline from same jenkins job. - jass

1 Answers

1
votes

I was facing the same issue and created a workaround for that. I decided to tag with suffix _uat, _prod etc. which helped me to achieve the goal. You will also require two Jenkins jobs for that. One will trigger another if condition matched.

Step 1 git tag -a release-v1_uat -m "Commit message"
git push origin release-v1_uat

Create two Jenkins Jobs

Job 1
Define repository
In Advance section's "Refspec" field put:
+refs/tags/_uat:refs/remotes/origin/tags/_uat

In "Branches to build" section:
**/tags/*_uat

BuildTriggers
CHECK-> GitHub hook trigger for GITScm polling

BUILDSTEP
ExecuteShell
TAG=$(git describe --tags --abbrev=0)
echo $TAG
echo $TAG > /tmp/tagname
result=echo $TAG | sed 's/.*\(....\)/\1/'
if [[ $result == _uat ]]; then echo yes; else (exit 1); fi

Click on Advance right below execute shell
Exit code to set build unstable (It will prevent the job to get triggered from any other Tag)
Put 1 in the Box

Post-build Actions
Check: Delete WorkSpace After Build

Job 2 (Your Main Job)
Go to "Build after other projects are built"
Mention you job 1 name in the box and select "Trigger only if build is stable"

Do not forget to get your tag value from the file by doing cat /tmp/tagname which generated in the job 1

There is always a room for improvement, please share if you have some better workaround/solution.