"git submodule foreach" syntax error in Jenkins pipeline, but works on command line?

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

"git submodule foreach" syntax error in Jenkins pipeline, but works on command line?

问题

我可以在命令行上执行以下操作,我只想要检出一个名为`feature/*`的分支,如果子模块没有该分支,我想要检出`master`分支。

$ git submodule foreach 'git checkout feature/test-new-scripts || git checkout master'

然而,如果我在Jenkins流水线中执行相同的命令,我会收到语法错误。

sh """
git submodule foreach \'git checkout feature/test-new-scripts || git checkout master\'
"""

我会收到以下错误消息
  • git submodule foreach "git checkout feature/test-new-scripts
    Entering 'module1'
    "git: 1: "git: Syntax error: Unterminated quoted string
    fatal: run_command returned non-zero status for module1
    .
  • git checkout master"
    error: pathspec 'master"' did not match any file(s) known to git
我尝试过以下语法,只是将命令本身的单引号 (`'...'`) 更改为双引号 (`"..."`),但无济于事。

sh """
git submodule foreach \"git checkout feature/test-new-scripts || git checkout master\"
"""


实现我想要的操作的正确语法是什么?
英文:

I can execute the following on a command line, where I just want to checkout a feature/* branch, and if the sub module does not have it, I want to checkout master.

$ git submodule foreach 'git checkout feature/test-new-scripts || git checkout master'

However, if I execute the same command in a Jenkins pipeline, I get a syntax error.

sh """
  git submodule foreach \\'git checkout feature/test-new-scripts || git checkout master\\'
"""

I get the following error

+ git submodule foreach "git checkout feature/test-new-scripts
Entering 'module1'
"git: 1: "git: Syntax error: Unterminated quoted string
fatal: run_command returned non-zero status for module1
.
+ git checkout master"
error: pathspec 'master"' did not match any file(s) known to git

I've tried the following syntax, where I just changed the single quotes ('...') in the command itself to double quotes ("..."), to no avail

sh """
  git submodule foreach \\"git checkout feature/test-new-scripts || git checkout master\\"
"""

What is the correct syntax do accomplish what I want to do?

答案1

得分: 0

正如错误消息所指示的,这确实是与sh步骤方法参数中Groovy字符串转换有关的问题。我们可以稍作调整以进行清理:

sh(label: 'Git Submodule Checkout', script: "git submodule foreach 'git checkout feature/test-new-scripts || git checkout master'")

通过Jenkins GitSCM类,也可以实现这个目标。

英文:

As the error message indicates this is indeed an issue with Groovy string casting in the sh step method parameter. We can clean this up a bit to assist:

sh(label: 'Git Submodule Checkout', script: "git submodule foreach 'git checkout feature/test-new-scripts || git checkout master'")

It may also be possible to achieve this with the GitSCM class as well by the way.

huangapple
  • 本文由 发表于 2023年7月13日 10:16:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76675491.html
匿名

发表评论

匿名网友

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

确定