Jenkins pipeline - try catch for particular stage and subsequent conditional step

允我心安 提交于 2019-11-27 21:36:48

问题


I'm trying to replicate the equivalent of a conditional stage in Jenkins pipeline using a try / catch around a preceding stage, which then sets a success variable, which is used to trigger the conditional stage.

It appears that a try catch block is the way to go, setting a success var to SUCCESS or FAILED, which is used as part of a when statement later (as part of the conditional stage).

The code I am using is as follows:

pipeline {
    agent any
    stages {
        try{
            stage("Run unit tests"){
                steps{
                    sh  '''
                        # Run unit tests without capturing stdout or logs, generates cobetura reports
                        cd ./python
                        nosetests3 --with-xcoverage --nocapture --with-xunit --nologcapture --cover-package=application
                        cd ..
                    '''
                    currentBuild.result = 'SUCCESS'
                }
            }
        } catch(Exception e) {
            // Do something with the exception 
            currentBuild.result = 'SUCCESS'
        }

        stage ('Speak') {
            when {
                expression { currentBuild.result == 'SUCCESS' }
            }
            steps{
                echo "Hello, CONDITIONAL"
            }
        }
    }
}

The latest syntax error I am receiving is as follows:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup 
failed:
WorkflowScript: 4: Expected a stage @ line 4, column 9.
       try{

I've also tried lots of variations.

Am I taking the wrong approach here? This seems like a fairly common requirement.

Thanks.


回答1:


This might solve your problem depending on what you are going for. Stages are only run when the preceding stages succeed, so if you actually have two stages like in your example, and if you want the second to only run when the first succeeds, you want to ensure that the first stage fails appropriately when tests fail. Catching will prevent the (desirable) failure. Finally will preserve the failure, and can also still be used to grab your test results.

So here, the second stage will only run when the tests pass, and the test results will be recorded regardless:

pipeline {
  agent any
  stages {
    stage("Run unit tests"){
      steps {
        script {
          try {
            sh  '''
              # Run unit tests without capturing stdout or logs, generates cobetura reports
              cd ./python
              nosetests3 --with-xcoverage --nocapture --with-xunit --nologcapture --cover-package=application
              cd ..
              '''
          } finally {
            junit 'nosetests.xml'
          }
        }
      }
    }
    stage ('Speak') {
      steps{
        echo "Hello, CONDITIONAL"
      }
    }
  }
}

Note that I'm actually using try in a declarative pipeline, but like StephenKing says, you can't just use try directly (you have to wrap arbitrary groovy code in the script step).



来源:https://stackoverflow.com/questions/43293501/jenkins-pipeline-try-catch-for-particular-stage-and-subsequent-conditional-ste

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!