英文:
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()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论