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?
As soon as we will hit the URL in the browser, Jenkins will trigger the build automatically like in the below image.
With the following configuration, you can make a job build all tags:
+refs/tags/*:refs/remotes/origin/tags/*
*/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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With