[英]How can I read a GIT tag from the current branch into a variable?
I want to use git tags within my declarative Jenkins pipeline.我想在我的声明式 Jenkins 管道中使用 git 标签。 My Jenkinsfile looks like this
我的 Jenkinsfile 看起来像这样
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.但是环境变量
MY_GIT_TAG
始终为空。 After some investigation i noticed this in my Jenkins logs: git fetch --no-tags --progress...
经过一些调查后,我在我的 Jenkins 日志中注意到了这一点:
git fetch --no-tags --progress...
Is there a way to tell Jenkins to skip the --no-tags
argument?有没有办法告诉 Jenkins 跳过
--no-tags
参数?
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.因为我事先不知道提交是如何标记的,所以我想从 git 中检出标记并将其用作变量。 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.我们可以使用,
sh(returnStdout: true, script: "git tag --sort=-creatordate | head -n 1").trim()
将其放入变量中并使用它。
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.如评论中所述,
sh
不返回任何内容。 You can do env.MY_GIT_TAG = sh(returnStdout: true, script: 'git tag -l --points-at HEAD').trim()
to return the stdout.您可以执行
env.MY_GIT_TAG = sh(returnStdout: true, script: 'git tag -l --points-at HEAD').trim()
返回标准输出。
As mentioned by Tai Ly there is a description of two possible solutions here正如 Tai Ly 所提到的,这里描述了两种可能的解决方案
Solution 1)解决方案 1)
You can create a custom checkout in your Jenkinsfile that sets noTags
to false.您可以在 Jenkinsfile 中创建自定义签出,将
noTags
设置为 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)解决方案 2)
Add an "Advanced Clone Behaviours" entry in the branch source behaviors in the Jenkins web interface.在 Jenkins Web 界面的分支源行为中添加“高级克隆行为”条目。 It can also be set at the Organization/Team-level for GitHub/Bitbucket plugins & co.
它也可以在 GitHub/Bitbucket 插件等的组织/团队级别设置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.