Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create git tag from within Jenkins pipeline script

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?

like image 555
octavian Avatar asked Mar 02 '17 09:03

octavian


People also ask

How do I manually create a tag in git?

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.

How do I add multiple tags in Jenkins?

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.

Can Jenkins pipeline trigger off of Git 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.

Why do I need a build pipeline for Git?

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

When does the deploy stage execute in Git pipeline?

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:

What is a Jenkins pipeline?

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.


2 Answers

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.

like image 123
eyalstoler Avatar answered Oct 19 '22 12:10

eyalstoler


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.

like image 4
rbellamy Avatar answered Oct 19 '22 14:10

rbellamy