Try-catch block in Jenkins pipeline script

后端 未结 5 1680
有刺的猬
有刺的猬 2020-12-13 18:16

I\'m trying to use the following code to execute builds, and in the end, execute post build actions when builds were successful. Still, I get a MultipleCompilationErrorsExce

5条回答
  •  情深已故
    2020-12-13 19:10

    try like this (no pun intended btw)

    script {
      try {
          sh 'do your stuff'
      } catch (Exception e) {
          echo 'Exception occurred: ' + e.toString()
          sh 'Handle the exception!'
      }
    }
    

    The key is to put try...catch in a script block in declarative pipeline syntax. Then it will work. This might be useful if you want to say continue pipeline execution despite failure (eg: test failed, still you need reports..)

提交回复
热议问题