在Jenkins中手动触发的流水线中如何获取分支名称?

huangapple go评论50阅读模式
英文:

How to get Branch Name in a manually triggered pipeline in Jenkins?

问题

我有一个在Jenkins中的自由风格项目,有时手动触发,我想使用提交哈希获取分支名称或提交消息。

我找到了这个示例,但当我尝试使用它时,出现错误。
错误 "期望 ')',找到 ':' "

sh(returnStdout: true, script: 'git for-each-ref --format=\'%(objectname) %(refname:short)\' refs/remotes/origin --contains 29c2b3672b | awk "/a/ {print \$2}"').trim()

感谢@AVTUNEY,我能够修复这个错误。

但我一直在使用这个git命令,对于某些提交哈希,这个命令可以正常工作,对于另一些提交哈希,它不能正常工作。

有人知道为什么会发生这种情况吗?

英文:

I have a freestyle project in Jenkins which sometimes is triggered manually, and I want to get the branch name or the commit message using the commit hash.

I have found this example but I'm getting an error when I try to use it.
Error "expecting ')', found ':' "

sh(returnStdout: true, script: 'git for-each-ref --format='%(objectname) %(refname:short)' refs/remotes/origin --contains 29c2b3672b | awk "/a/ {print \$2}"').trim()

Thanks to @AVTUNEY I was able to fix the error.

But I have been using this git command and with some commit hash this works well and with some others this doesn't work properly.

Does anyone have any idea why this happens?

答案1

得分: 0

你需要转义 --format 中的单引号。

这样做可以:

sh(returnStdout: true, script: "git for-each-ref --format=\"%(objectname) %(refname:short)\" refs/remotes/origin --contains 29c2b3672b | awk '/a/ {print $2}'").trim()

英文:

You need to escape single quotes in --format

This will work:

sh(returnStdout: true, script: "git for-each-ref --format=\"%(objectname) %(refname:short)\" refs/remotes/origin --contains 29c2b3672b | awk '/a/ {print $2}'").trim()

huangapple
  • 本文由 发表于 2023年4月16日 23:29:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76028650.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定