【发布时间】:2020-12-06 10:08:18
【问题描述】:
我正在尝试在 jenkins 管道中执行 git checkout 并尝试仅找出特定文件中添加的行。 然而,动态分支名称上的美元符号让我犯了错误,但同样的事情在管道的其他地方也有效。代码中唯一的新增内容是 git diff 行,用于仅查找特定文件中添加的新行。你能帮我实现吗?
工作代码:
script{
withCreentials(...){
sh returnStdout: true, script: """
gwit checkout -B ${env.branch}
git config user.name 'xxx'
git config user.email 'xxx@xxx.com'
git add sample.txt
git commit -m "updated from pipeline"
git push origin HEAD:${env.branch}
"""
}
}
代码无效:
script{
withCreentials(...){
sh returnStdout: true, script: """
git checkout -B ${env.branch}
git config user.name 'xxx'
git config user.email 'xxx@xxx.com'
git diff HEAD^:sample.txt HEAD:sample.txt --color=always|perl -wine 'print $1 if /^\e\[32m\+\e\[m\e\[32m(.*)\e\[m$/'> output.txt
}
}
错误是:
illegal string boy character
solution: either escape a literal dollar sign "\$5" or bracket the value expression "${5}"
git checkout -B ${env.branch}
^
【问题讨论】:
-
您可以先将多行命令拆分为几个单行命令,然后查看是哪一行导致了问题。
-
如果我删除上面提到的 git diff 命令..一切运行正常..但是当我添加它时,错误指向结帐行..这就是为什么很难理解这个问题
-
您的线路有
print $1,错误为either escape a literal dollar sign "\$5" or bracket the value expression "${5}"。您是否尝试按照错误消息的提示进行修复? -
试过..没有运气..但很确定问题是在 git diff 命令中转义......直到现在我无法解决
标签: git jenkins groovy gitlab jenkins-pipeline