Within my Jenkins pipeline script, I would like to do something like this:
sh("git tag ${BUILD_NUMBER}")
However, this wouldn't work if git isn't found on the shell.
Is there any Jenkins plugin that can do this from a Jenkins pipeline script?
In order to create a new tag, you have to use the “git tag” command and specify the tag name that you want to create. As an example, let's say that you want to create a new tag on the latest commit of your master branch. To achieve that, execute the “git tag” command and specify the tagname.
In the Jenkins Job or Pipeline field, select a job or pipeline. In the Duration (minutes) field, enter an estimate for the length of the task. In the Tags list, select an item to attach a tag to the task. You can select multiple tags.
The immutable nature of releases and the immutable nature of tags can definitely go hand in hand, but up until few months ago Jenkins Pipeline was not able to trigger effectively off of Git tags. In this post I want to briefly share how to use tags to drive behaviors in Jenkins Pipeline.
For some workflows, you need your build pipeline to run Git commands. For example, after a CI build on a feature branch is done, the team might want to merge the branch to main. Git is available on Microsoft-hosted agents and on on-premises agents. Enable scripts to run Git commands
Of particular note is the when condition on the "Deploy" stage which is applying the tag criteria. This means the stage would only execute when the Pipeline has been triggered from a tag in Git matching the release-* Ant-style wildcard. In practice, this means that all pull requests, and branch-based Pipeline Runs result in the stage being skipped:
Also note that Jenkins has its own “pipeline” concept (formerly known as “workflows”) that are for long-running, complicated build tasks spanning multiple build slaves. This article strives to keep things as simple as possible using backwards-compatible freestyle jobs.
There is no plugin support for this currently but might be in the future:
https://issues.jenkins-ci.org/browse/JENKINS-28335
While you go over this Jira issue take a look at Andrey Makeev's temporary solution. also documented here.
Here's how I do it, where shell
and Version
are custom functions and class, respectively, and shell
is a drop-in replacement for the sh
function:
void gitTag(Version releaseVersion) {
sshagent(['devops_deploy_DEV']) {
shell 'git tag -d \$(git tag)'
shell 'git fetch --tags'
echo "New release version ${releaseVersion.normalVersion}"
shell "git tag -fa ${releaseVersion.normalVersion} -m 'Release version ${releaseVersion.normalVersion}'"
}
}
You can find the source for this here.
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