Jes*_*e S 74 jenkins jenkins-workflow jenkins-pipeline
例如:
var output=sh "echo foo";
echo "output=$output";
Run Code Online (Sandbox Code Playgroud)
我会得到:
output=0
Run Code Online (Sandbox Code Playgroud)
所以,显然我得到退出代码而不是标准输出.是否有可能将stdout捕获到管道变量中,这样我就可以得到:
output=foo
作为我的结果?
ilo*_*ahz 198
现在,该sh步骤支持通过提供参数返回stdoutreturnStdout.
// These should all be performed at the point where you've
// checked out your sources on the slave. A 'git' executable
// must be available.
// Most typical, if you're not cloning into a sub directory
gitCommit = sh(returnStdout: true, script: 'git rev-parse HEAD').trim()
// short SHA, possibly better for chat notifications, etc.
shortCommit = gitCommit.take(6)
Run Code Online (Sandbox Code Playgroud)
看这个例子.
小智 44
注意:已经解决了链接的Jenkins问题.
如在JENKINS-26133中提到的那样,不可能将shell输出作为变量.作为一种解决方法,建议使用临时文件中的写入读取.所以,你的例子看起来像:
sh "echo foo > result";
def output=readFile('result').trim()
echo "output=$output";
Run Code Online (Sandbox Code Playgroud)
尝试这个:
def get_git_sha(git_dir='') {
dir(git_dir) {
return sh(returnStdout: true, script: 'git rev-parse HEAD').trim()
}
}
node(BUILD_NODE) {
...
repo_SHA = get_git_sha('src/FooBar.git')
echo repo_SHA
...
}
Run Code Online (Sandbox Code Playgroud)
测试于:
您也可以尝试使用此函数来捕获 StdErr StdOut 并返回代码。
def runShell(String command){
def responseCode = sh returnStatus: true, script: "${command} &> tmp.txt"
def output = readFile(file: "tmp.txt")
if (responseCode != 0){
println "[ERROR] ${output}"
throw new Exception("${output}")
}else{
return "${output}"
}
}
Run Code Online (Sandbox Code Playgroud)
注意:
&>name means 1>name 2>name -- redirect stdout and stderr to the file name
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
74788 次 |
| 最近记录: |