简体   繁体   English

詹金斯管道 | Groovy 异常处理

[英]Jenkins Pipeline | Groovy exception handling

Am writing a Groovy script in Jenkins pipeline, which executes shell script on a remote server.我正在 Jenkins 管道中编写一个 Groovy 脚本,它在远程服务器上执行 shell 脚本。 Based on the output of shell script, I should handle the exception.根据shell脚本的输出,我应该处理异常。

If shell script output = 'xyz' > Build success如果 shell 脚本输出 = 'xyz' > 构建成功

If shell script output != 'xyz' > Throw Exception, build failure.如果 shell 脚本输出 != 'xyz' > Throw Exception,则构建失败。

Any help would be highly appreciated!任何帮助将不胜感激!

My Script我的脚本

def check()
{
    try 
    {
        println "Check started"
        
        sh "echo -e '' >> Result.txt"
        sh "ssh -q -o StrictHostKeyChecking=no test_agent@Bihkik1123.xyz.com  /home/test_agent/check.sh >> Result.txt"

    
        println "Check completed"
    }
    catch(Exception e) 
    {
       throw e;
    }
}

You can mark the stage as FAILED in case the keyword is not matched for eg.如果关键字不匹配,您可以将阶段标记为 FAILED。 :

  stage('test') {
        println "Check started"
        sh "echo 'xyz' > /tmp/results.txt"
        
        shellReturn = sh(returnStdout: true, script: """
            cat /tmp/results.txt
        """).trim()
        
        if(shellReturn == /xyz/){
          currentBuild.result = 'SUCCESS'      
        } else{
          println "FAILED"
          currentBuild.result = 'FAILED'
        }
      }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM