3

I want to use git tags within my declarative Jenkins pipeline. My Jenkinsfile looks like this

pipeline {
    agent any
    stages {
        stage('Setup') {
            steps {
                script {
                    env.MY_GIT_TAG = sh(returnStdout: true, script: 'git tag -l --points-at HEAD')
                    // ... 
                }
            }
        }
        stage('Build'){
            // build my code etc ....
        }
        stage('Publish') {
            // push code somewhere depending on tag
            sh "curl -X POST --upload-file ./MyDeployable https://someserver/uri/MyDeployable-${env.MY_GIT_TAG}"
        }
    }
}

But the environment variable MY_GIT_TAG was always empty. After some investigation i noticed this in my Jenkins logs: git fetch --no-tags --progress ...

Is there a way to tell Jenkins to skip the --no-tags argument?

As i do not know beforehand how the commit is tagged i want to checkout the tag from git and use it as a variable. So the solution in this question is not viable here.

user2393256
  • 1,001
  • 1
  • 15
  • 26
  • 1
    I don't think you can set an environment variable like that. Look [here](https://stackoverflow.com/questions/43879733/jenkinsfile-declarative-pipeline-defining-dynamic-env-vars/43881731) – fredrik Dec 17 '18 at 13:12
  • Setting the environment variable this way works. This is only one instance where it does not because there is no git tag present (the sh command returns nothing). – user2393256 Dec 17 '18 at 13:17
  • Could you please give more details about what you really want to do? Seems like an X Y Problem this... – cantSleepNow Dec 17 '18 at 20:53
  • I added the information. But it's nothing fancy i just want to append the git tag to the file name when i upload it somewhere. – user2393256 Dec 18 '18 at 07:11

3 Answers3

2

We can use, sh(returnStdout: true, script: "git tag --sort=-creatordate | head -n 1").trim() take this into a variable and use it.

pipeline {
  agent any
    stages {
        stage('get git tag') {
            steps {
             script {
             latestTag = sh(returnStdout:  true, script: "git tag --sort=-creatordate | head -n 1").trim()
             env.BUILD_VERSION = latestTag
             echo "env-BUILD_VERSION"
             echo "${env.BUILD_VERSION}"
            }
        }
    }
  }
}
Aditya Malviya
  • 1,907
  • 1
  • 20
  • 25
1

As mentioned in the comments, the sh returns nothing. You can do env.MY_GIT_TAG = sh(returnStdout: true, script: 'git tag -l --points-at HEAD').trim() to return the stdout.

Tai Ly
  • 341
  • 1
  • 2
  • 10
  • That is exactly what i do. But the problem is that jenkins does a `git fetch --no-tags` which means that `git tag -l --points-at HEAD` won't return anything thus `env.MY_GIT_TAG` stays empty – user2393256 Dec 18 '18 at 07:13
  • 1
    Have you tried to make a custom checkout before getting the tag? You can add an extension parameter where you can set the no-tag flag to false. See in: https://issues.jenkins-ci.org/browse/JENKINS-45164 – Tai Ly Dec 18 '18 at 08:23
  • Thanks. That works. I can either create a custom checkout or another comment mentions the `Advanced Clone Behaviours` settings where i can check a `fetch tags` option. – user2393256 Dec 18 '18 at 10:01
1

As mentioned by Tai Ly there is a description of two possible solutions here

Solution 1)

You can create a custom checkout in your Jenkinsfile that sets noTags to false.

checkout([
    $class: 'GitSCM',
    branches: scm.branches,
    doGenerateSubmoduleConfigurations: scm.doGenerateSubmoduleConfigurations,
    extensions: [[$class: 'CloneOption', noTags: false, shallow: false, depth: 0, reference: '']],
    userRemoteConfigs: scm.userRemoteConfigs,
 ])

Soltion 2)

Add an "Advanced Clone Behaviours" entry in the branch source behaviors in the Jenkins web interface. It can also be set at the Organization/Team-level for GitHub/Bitbucket plugins & co.

user2393256
  • 1,001
  • 1
  • 15
  • 26