badbishop
badbishop

Reputation: 1648

Jenkins multibranch pipeline: how to get most recent git tag?

My project build relies on git tags to determine the version to use for an artifact. I'm trying to create a Jenkins multibranch pipeline. That means, I need somehow to get an equivalent of the output of

git describe --tags

into my Jenkins pipeline. No matter what I do with all kind of "Advanced behaviors", I get a detached HEAD and git saying it has nothing to describe.

This document https://jenkins.io/doc/pipeline/steps/workflow-scm-step/ mentions $class: GitTagMessageExtension with an optional boolean parameter useMostRecentTag, which, by description should provide exactly what I need, but I cannot find the equivalent for it in pipeline snippet generator, and when, in Jenkisfile, I include

checkout(
            [$class: 'GitSCM', 
            branches: [[name: '**']], 
            doGenerateSubmoduleConfigurations: false, 
            extensions: [
                [$class: 'CloneOption', depth: 0, noTags: false, reference: '', shallow: false], 
                [$class: 'GitTagMessageExtension', useMostRecentTag:true]
            ], 
            submoduleCfg: [], 
            userRemoteConfigs: [[credentialsId: 'xxx', url: 'xxx']]
            ]
        )

I'm getting an error

java.lang.UnsupportedOperationException: no known implementation of class hudson.plugins.git.extensions.GitSCMExtension is named GitTagMessageExtension

EDIT:

As pointed by @ioannis-barakos, the GitTagMessageExtension plugin was missing. However, after installing it, the promise of

If you ticked the Use most recent tag option, and the revision checked out has no git tag associated with it, the parent commits will be searched for a git tag, and the rules stated above will apply to the first parent commit with a git tag.

doesn't hold. It just runs git describe --tags <commit-hash> against detached head and exports exactly nothing.

So, does anyone know how to handle it? What am I missing?

Upvotes: 0

Views: 3141

Answers (2)

Ioannis Barakos
Ioannis Barakos

Reputation: 1369

You probably do not have the Git Tag Message Plugin installed in your Jenkins.

The GitTagMessageExtension is provided by that plugin (as in here)

Make sure that you have the following plugin installed in your Jenkins plugins.

Git Tag Message Plugin

Below is a working example that searches for tags in all branches (origin/**). Have in mind that a credentialsId should have been configured in jenkins holding the username/password of the jenkins account and a RelativeTargetDirectory class should be set for the download/clone location.

script {
    checkout([
      $class: 'GitSCM',
      branches: [[name: "origin/**"]],
      doGenerateSubmoduleConfigurations: false,
      extensions: [[
      $class: 'RelativeTargetDirectory',
        relativeTargetDir: "/tmp/jenkins/git"],
        [$class: 'CloneOption', depth: 0, noTags: false, reference: '', shallow: false], 
        [$class: 'GitTagMessageExtension', useMostRecentTag:true] 
        ],
        submoduleCfg: [],
        userRemoteConfigs: [[
             credentialsId: 'ioannis.barakos',
             url: 'https://git.example.com/git/example'
        ]]
    ])            
}

Upvotes: 0

badbishop
badbishop

Reputation: 1648

It was all false alarm: I just forgot to git push --tags

There is one counter-intuitive catch, though: ticking "Discover Tags" is not enough, one has to also choose "Advanced clone behaviors" and tick "Fetch tags"

Upvotes: 1

Related Questions