Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible for Jenkins to automatically detect and build newly created tags in a git repo?

Tags:

git

jenkins

It would be nice for our Jenkins CI server to automatically detect, deploy and build tags as they are created in our Github repository.

Is this possible?

like image 835
Ricardo Gladwell Avatar asked Oct 18 '11 10:10

Ricardo Gladwell


People also ask

Can Jenkins build be triggered automatically?

As soon as we will hit the URL in the browser, Jenkins will trigger the build automatically like in the below image.


2 Answers

With the following configuration, you can make a job build all tags:

  1. Make the job fetch tags as if they were branches: Click on the Advanced button below the repository URL and enter the Refspec +refs/tags/*:refs/remotes/origin/tags/*
  2. Have it build all tag "branches" with the Branch Specifier */tags/*
  3. Enable SCM polling, so that the job detects new tags.

This approach has one drawback: The job will build all tags and not just newly added tags. So after you have created the job, it will be triggered once for every existing tag. So you probably want to have the job do nothing at first, then wait until all existing tags have been processed, and only then configure the build steps you want to be done for every new tag.

Since tags don't change in git, the job will then only be triggered once for every new tag.

like image 174
oberlies Avatar answered Sep 24 '22 17:09

oberlies


To overcome the drawback of @oberlies' answer that all tags will be built I'm using a special trigger build instead. The trigger build uses the same git repository and branch as the main build and the following (post) build steps.

Build -> Execute shell:

# Get the most recent release tag. PATTERN="release-tag-[0-9][0-9]-[0-9][0-9][0-9][0-9]" TAG=$(git log --tags=$PATTERN --no-walk --pretty="format:%d" | grep -m 1 -o $PATTERN)  # Due to a Jenkins limitation (https://issues.jenkins-ci.org/browse/JENKINS-8952) # when passing environment variables we have to write the tag to a file and # inject it later again. mv release.properties release-old.properties || true echo "TAG = $TAG" > release.properties  # Fail the build if the most recent release tag did not change. ! diff release.properties release-old.properties 

Build -> Inject environment variables:

Properties File Path: release.properties 

Post-build Actions -> : Trigger parameterized build on other projects

Projects to build: <your main project> Trigger when build is: Stable Parameters: TAG=$TAG 

Finally, in your main build, tick "This build is parameterized" with the following string parameter

Name: TAG Default Value: <your release branch> 

And in the "Source Code management" section use "$TAG" in the "Branches to build" field.

like image 36
sschuberth Avatar answered Sep 22 '22 17:09

sschuberth