4

This is the same as an answered question from over a year ago: Jenkinsfile get current tag

The accepted answer there is not working for me and I'm thinking the behavior must have changed within the pipeline since then.

My repo has tags (pushed to origin!), and I've added the Jenkins option to discover tags, however I cannot harvest or reference them from the pipeline steps. It just shows up null.

In my MultiBranch Pipeline job I have added the "Discover tags" step. discover tags

I have a git repo with a release tag set:

myhost$ git fetch
myhost$ git tag
0.0.42
myhost$ git tag --sort version:refname
0.0.42

In the Jenkinsfile I've tried:

sh "git tag --sort version:refname | tail -1 > version.tmp"
sh "cat version.tmp"

and:

sh(returnStdout: true, script: "git tag --sort version:refname | tail -1").trim()

Both of them return with nothing (null)

StephenKing
  • 36,187
  • 11
  • 83
  • 112
emmdee
  • 1,541
  • 3
  • 25
  • 46
  • 1
    Have you tried running `git fetch --tags` just to make sure that Jenkins fetches them during cloning? I'm not sure if it does this out of the box. Discovering (to create new jobs) does not necessarily mean they are available when cloning. – StephenKing Jan 17 '18 at 05:56
  • I just validated that this is the correct answer - if you answer the question I'll mark it as the accepted answer. – emmdee Jan 18 '18 at 22:21
  • @StephenKing: I tried to add `sh 'git fetch --tags'` during the first step (right after `checkout scm`. But I ran into the following issue: `+ git fetch --tags. fatal: could not read Username for 'https://bitbucket.org': No such device or address'.` What am I missing here? – natterstefan Nov 03 '18 at 11:54

2 Answers2

6

You must make sure Jenkins fetches your repo with tags. You can see whether this is the case in the Jenkins build console. There must be a line like git fetch --no-tags ... or git fetch --tags ....

My Jenkins installation seems to change this behavior sometimes without obvious reason. To make sure it fetches tags add Advanced clone behaviors to your Pipeline job with the checkbox Do not fetch tags not marked.

enter image description here

Florian
  • 1,036
  • 10
  • 15
0

You should try

 sh "git tag --sort version:refname > tags.tmp"
 sh "tail tags.tmp -n 1 > version.tmp"
 sh "cat version.tmp"

I think this change should work.

Thanks,

Surendra Deshpande
  • 328
  • 1
  • 3
  • 14