0

I am using a Jenkins declarative pipeline with a Docker agent (Windows label). In my pipeline, I want to get the current git tag for the commit being built, so I can use it for artifact naming.

My checkout stage looks like this:(i tried both)

first way


stage('Checkout') {
      steps {
        checkout scm 
      }
    }

second way


stage('Checkout') {
      steps {
        checkout(
          extensions: [
              [$class: 'CloneOption', depth: 1, shallow: true]
          ]
          )
      }
    }

After this, I run the following PowerShell command to get the tag:

$tag = git -C "$env:WORKSPACE" tag --points-at HEAD | Select-Object -First 1
if ([string]::IsNullOrEmpty($tag)) {
    $tag = "notag"
}

Even though the commit is tagged in the repository, $tag is always empty ("notag"). I have tried both default and shallow clone checkouts, but Jenkins does not seem to fetch tags.

How can I make Jenkins fetch git tags so that git tag --points-at HEAD works in my pipeline? Is there a recommended way to ensure tags are available after checkout?

3
  • In your powershell command why dont you just run the git fetch --tags and then continue with your $tag code?? Commented Sep 29 at 4:00
  • @SiddharthKaul i included this line but still not getting any tags * branch HEAD -> FETCH_HEAD Commented Sep 29 at 4:25
  • Some shots in the dark: Try shallow: false. Or depth: 5 (more than 1). Commented Sep 29 at 6:47

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.