I am trying to get the latest tag from a repo Jenkinsfile (using Blue Ocean Declarative Pipelines) but it throws an error.
Command:
def tag = sh(script: 'git describe --tags $(git rev-list --tags --max-count=1)', returnStdout: true).trim()
Expected output:
v2.4.1
But I get:
fatal: No names found, cannot describe anything.
I went into the workspace and executed the command manually and received the same fatal error but when I clone manually I am able to get the version. Am I missing something with Jenkins Pipelines?
Note: I am able to get the commit id and branch name. Not the tags.
The problem was that pipelines do not fetch tags. You have to go into the advanced clone settings and enable fetch tags.
The solution is described here: https://issues.jenkins-ci.org/browse/JENKINS-45164
Instead of using checkout scm use:
checkout([
    $class: 'GitSCM',
    branches: scm.branches,
    doGenerateSubmoduleConfigurations: scm.doGenerateSubmoduleConfigurations,
    extensions: [[$class: 'CloneOption', noTags: false, shallow: false, depth: 0, reference: '']],
    userRemoteConfigs: scm.userRemoteConfigs,
])
Note, it will require by approving script access to methods scm.branches, scm.userRemoteConfigs etc
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