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

91 views
Skip to first unread message

zil...@gmail.com

unread,
Jul 12, 2023, 10:50:01 PM7/12/23
to Jenkins Users

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? TIA


zil...@gmail.com

unread,
Jul 13, 2023, 3:46:13 PM7/13/23
to Jenkins Users
Someone from Stackoverflow answered the same question I posted there

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

Reply all
Reply to author
Forward
0 new messages