简体   繁体   English

Jenkins MultiBranch-从管道文件(Jenkinsfile)参考git repo的标签

[英]Jenkins MultiBranch - Reference git repo's tag from pipeline file (Jenkinsfile)

This is the same as an answered question from over a year ago: Jenkinsfile get current tag 这与一年多以前的回答相同: Jenkinsfile获取当前标签

The accepted answer there is not working for me and I'm thinking the behavior must have changed within the pipeline since then. 那里没有被接受的答案对我没有用,并且我认为此后必须在管道内更改行为。

My repo has tags (pushed to origin!), and I've added the Jenkins option to discover tags, however I cannot harvest or reference them from the pipeline steps. 我的回购包含标签(已推送至原点!),并且添加了Jenkins选项以发现标签,但是无法从管道步骤中获取或引用它们。 It just shows up null. 它只是显示为null。

In my MultiBranch Pipeline job I have added the "Discover tags" step . 在我的MultiBranch Pipeline作业中, 我添加了“发现标签”步骤 发现标签

I have a git repo with a release tag set: 我有一个带有发行标签集的git repo:

myhost$ git fetch
myhost$ git tag
0.0.42
myhost$ git tag --sort version:refname
0.0.42

In the Jenkinsfile I've tried: 在Jenkinsfile中,我尝试过:

sh "git tag --sort version:refname | tail -1 > version.tmp"
sh "cat version.tmp"

and: 和:

sh(returnStdout: true, script: "git tag --sort version:refname | tail -1").trim()

Both of them return with nothing (null) 他们两个都不返回(空)

You should try 你应该试试

 sh "git tag --sort version:refname > tags.tmp"
 sh "tail tags.tmp -n 1 > version.tmp"
 sh "cat version.tmp"

I think this change should work. 我认为此更改应该有效。

Thanks, 谢谢,

You must make sure Jenkins fetches your repo with tags. 您必须确保Jenkins使用标签来获取您的回购。 You can see whether this is the case in the Jenkins build console. 您可以在Jenkins构建控制台中查看是否存在这种情况。 There must be a line like git fetch --no-tags ... or git fetch --tags ... . 必须有像git fetch --no-tags ...git fetch --tags ...

My Jenkins installation seems to change this behavior sometimes without obvious reason. 我的Jenkins安装似乎有时会在没有明显原因的情况下更改此行为。 To make sure it fetches tags add Advanced clone behaviors to your Pipeline job with the checkbox Do not fetch tags not marked. 为确保其获取标签,请选中“ 不要获取未标记的标签 ”复选框,将高级克隆行为添加到管道作业中。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM