我需要在jenkins中过滤一个文件。只要结果不为空,筛选就会起作用。但是,如果结果输出为空,则管道将以ERROR: script returned exit code 1 Finished: FAILURE
失败
示例:
#!groovy
pipeline {
agent any
stages {
stage ('mystage') {
steps {
script {
sh "echo '' > myfile"
sh "echo 'foo 0' >> myfile"
sh "echo 'foo 1' >> myfile"
sh "grep foo myfile"
sh "grep ba myfile"
}
}
}
}
}
输出:
+ echo ''
[Pipeline] sh
+ echo 'foo 0'
[Pipeline] sh
+ echo 'foo 1'
[Pipeline] sh
+ grep foo myfile
foo 0
foo 1
[Pipeline] sh
+ grep ba myfile
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 1
Finished: FAILURE
将输出路由到具有CCD_ 2的文件不起作用。在这种边缘情况下,如果管道没有故障,我如何输出grep结果?
添加像sh "echo 'dummyline that won't match' >> myfile"
这样的虚设行似乎有效,但却是一种破解。有干净的解决方案吗?
我们可以在变量中获取返回值:
def ret = sh(script: 'grep ba myfile', returnStdout: true)
更多信息:https://www.jenkins.io/doc/pipeline/steps/workflow-durable-task-step/#code-sh代码shell脚本
注意,您还可以添加returnStatus: true
,这样即使命令失败,jenkins步骤也不会失败。