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.
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}"
}
}
}
}
}
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.
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