Jenkins trigger build on tag creation

Published on:

We want Jenkins to trigger builds from github any time a new tag is created.

In the Jenkins job we need to know.

  • the tag name
  • the branch name

This works.

export HASH=$(git rev-parse HEAD)
export BRANCH=$(basename $(git branch -r --contains ${HASH}))
export TAG=$(basename $(git describe --all --exact-match ${HASH}))

echo "HASH: $HASH"
echo "BRANCH: $BRANCH"
echo "TAG: $TAG"

However, getting that to work is very fiddly.

Make sure you have the following values in Source Code Management - git - repositories

  • refspec: +refs/tags/*:refs/remotes/origin/tags/*
  • branch specifier: **
  • additional behaviors: wipe out repository and force clone

Under build triggers check Build when a change is pushed to GitHub