Jenkins - try to catch a specific stage and the next conditional step

I am trying to reproduce the equivalent of a conditional stage in the Jenkins pipeline using try / catch in the previous stage, which then sets the success variable that is used to start the conditional stage.

The catch try block seems to be the path that sets var success for SUCCESS or FAILED, which is used as part of the when statement later (as part of the conditional step).

The code I use 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 last syntax error I get is as follows:

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

I also tried many options.

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

Thanks.

+6
1

, . , , , , , , , , , , . () . , .

, , :

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"
      }
    }
  }
}

, try , StephenKing, try ( groovy script).

+8

Source: https://habr.com/ru/post/1016403/


All Articles