Jenkins脚本化管道:sshCommand执行statusCode

时间:2019-11-07 00:03:13

标签: jenkins-pipeline

在我的jenkins脚本化管道中,我正在远程计算机上运行bash脚本。我尝试了以下几种方法,但未满足要求:  -由于我有传递给远程服务器命令以运行sshScript不支持的参数  -publish over ssh plugin在我也不想要的詹金斯日志中显示我的execCommand。

因此,我使用sshPut将我的bash脚本放置在远程服务器上,并使用sshCommand以及带有参数的远程脚本在该服务器上运行它。一切都很好,除非遇到错误,我需要退出并做其他事情。发生的情况是如果发生错误,jenkins作业出口将异常执行。可以通过为sshCommand设置failOnError: false来覆盖它;但是那样的话,乔布就永远不会失败。

我需要,如果sshCommand:退出并出现错误,那么我需要执行诸如send slackNotify之类的操作。那么,有没有像statusCodeexit-bhalah这样的东西可以像!= 0那样比较并执行某些功能呢?

我在想类似

stage('Deploy'){

// some blocks here

 sshCommand remote: remote, failOnError: false, command: "bash Filescript.sh $ARGS1"

  if (statusCode != 0){
     //do my thing here
    }
}

1 个答案:

答案 0 :(得分:0)

您可以使用try-catch:

stage('Deploy'){
// some blocks here
  try {  
    sshCommand remote: remote, command: "bash Filescript.sh $ARGS1"
  }
  catch {
    //do my thing here
  }
}
相关问题