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?
git fetch --tagsand then continue with your $tag code??shallow: false. Ordepth: 5(more than 1).