【问题标题】:Make build UNSTABLE if text found in console log using jenkinsfile (jenkins pipeline)如果使用 jenkinsfile(jenkins 管道)在控制台日志中找到文本,则构建 UNSTABLE
【发布时间】:2018-04-25 02:09:58
【问题描述】:

我正在尝试登录实例并检查文件 test.txt 是否不为空,然后 echo .. 使用 jenkins 管道(jenkinsfile)使构建不稳定,但这不起作用。 我有这个:

post {
        always {
          sh "ssh ubuntu@$Ip 'if [ -s test.txt ] ; then echo some text && cat test.txt'"
        currentBuild.result = 'UNSTABLE'
          }
        }

我是否可以通过最新构建的控制台日志来解析以查找某些内容,例如:some text,如果找到了,我想让构建不稳定

【问题讨论】:

标签: shell jenkins groovy jenkins-pipeline


【解决方案1】:

您需要从脚本中返回标准输出:

String stdOut = sh returnStdout: true, script: "ssh ubuntu@$Ip 'if [ -s test.txt ] ; then echo some text && cat test.txt'"

if (stdOut == "") {
   currentBuild.status = 'UNSTABLE'
}

或者,您可以使用returnStatus 返回脚本的退出代码。可以找到sh 步骤的文档here

【讨论】:

  • 感谢您的回复。上面的代码给了我这个错误:bash: -c: line 1: syntax error: unexpected end of file ERROR: script returned exit code 1
  • 当我有 "ssh ubuntu@ip 'if [ -s test.txt ] ;then echo some text; fi '" 时它可以工作,但是当我对附加的代码进行测试时(String stdout.... 的东西,它什么也没做