git Jenkinsfile 获取当前标签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37488178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jenkinsfile get current tag
提问by ligi
Is there a way to get the current tag ( or null if there is none ) for a job in a Jenkinsfile? The background is that I only want to build some artifacts ( android APKs ) when this commit has a tag. I tried:
有没有办法为 Jenkinsfile 中的工作获取当前标签(如果没有,则为 null )?背景是当这个提交有一个标签时,我只想构建一些工件(android APKs)。我试过:
env.TAG_NAME
and
和
binding.variables.get("TAG_NAME")
both are always null - even though this ( https://issues.jenkins-ci.org/browse/JENKINS-34520) indicates otherwise
两者始终为空 - 即使这(https://issues.jenkins-ci.org/browse/JENKINS-34520)另有说明
回答by Víctor Romero
I'd consider returnStdout
rather than writing to a file:
我会考虑returnStdout
而不是写入文件:
sh(returnStdout: true, script: "git tag --sort version:refname | tail -1").trim()
sh(returnStdout: true, script: "git tag --sort version:refname | tail -1").trim()
回答by Florian
All the other answers yield an output in any case even if HEAD is not tagged. The question was however to return the currenttag and "null" if there is nothing like that.
即使 HEAD 没有标记,所有其他答案在任何情况下都会产生输出。然而,问题是如果没有类似的东西,则返回当前标签和“null”。
git tag --contains
yields the tag name name if and only if HEAD is tagged.
git tag --contains
当且仅当 HEAD 被标记时,才产生标记名称 name。
For Jenkins Pipelines it should look like this:
对于 Jenkins Pipelines,它应该是这样的:
sh(returnStdout: true, script: "git tag --contains").trim()
sh(returnStdout: true, script: "git tag --contains").trim()
回答by Raphael
If the current build is a tag build -- as in, when { buildingTag() }
was "true" -- then the (undocumented) environment variable BRANCH_NAME
contains the name of the tag being build.
如果当前构建是标记构建——如在,when { buildingTag() }
是“真”——那么(未记录的)环境变量BRANCH_NAME
包含正在构建的标记的名称。
回答by kuhnroyal
The TAG_NAME
should work now at least in declarative pipelines.
现在TAG_NAME
应该至少在声明性管道中工作。
When
condition actually filters on this property. BRANCH_NAME
has the same value.
When
条件实际上过滤了这个属性。BRANCH_NAME
具有相同的值。
stage('release') {
when {
tag 'release-*'
}
steps {
echo "Building $BRANCH_NAME"
echo "Building $TAG_NAME"
}
}
回答by K Beiske
I believe the git command that does what you want is git tag --points-at=HEAD
this will list all tags pointing to the current commit or HEAD as it usually called in git. Since HEAD is also the default argument you can also do just git tag --points-at
.
我相信可以执行您想要的操作的 git 命令git tag --points-at=HEAD
将列出指向当前提交或 HEAD 的所有标签,因为它通常在 git 中调用。由于 HEAD 也是默认参数,因此您也可以只执行git tag --points-at
.
The pipeline step for executing and returning the git tags one for each line, then becomes:
执行和返回每行一个 git 标签的管道步骤,然后变为:
sh(returnStdout: true, script: "git tag --points-at")
sh(returnStdout: true, script: "git tag --points-at")
回答by Christoph Schwalbe
best way from my site is:
从我的网站最好的方法是:
git tag --sort=-creatordate | head -n 1
with:
和:
latestTag = sh(returnStdout: true, script: "git tag --sort=-creatordate | head -n 1").trim()
Than you can handle with simple regex, for prefix/suffix/version_number what is to do with the tag.
比你可以用简单的正则表达式处理,前缀/后缀/版本号与标签有什么关系。
other solution:
其他解决方案:
git describe --tags --abbrev=0
of course this is the current/latest tag in git. Independent from writing.
当然,这是 git 中的当前/最新标签。独立于写作。
sh(returnStdout: true, script: "git describe --tags --abbrev=0").trim()
回答by rbellamy
sh "git tag --sort version:refname | tail -1 > version.tmp"
String tag = readFile 'version.tmp'