0

I want to fail this stage if cntKO > 0 but I can only fail the build. Is there any option to just fail the stage?

stages {

     stage('Run the Create users based on Env. defined') {
        options { 
            timestamps () 
            timeout(time: 6, unit: 'HOURS')
        }
        steps {
            
           bat 'python batch\\CloudValidation\\Cloudusercreation-csvfile.py %release%'
           bat "batch\\CloudValidation\\usercreationoncloudnew.bat %ws% %release% %username% %pwd% %tenant% %POD%"
           script{
            def pipeline_log1 = currentBuild.rawBuild.getLog(10000);
            def result1 = pipeline_log1.find { it.contains('Total Number of KO Users') }
            if (result1) {
                   User_KO = result1.split("Total Number of KO Users:")
                   cntko = User_KO[1].toInteger()
                   if (cntko > 0)
                    {
                      echo ('Build failed due to' + result1)
                      **currentBuild.result = 'FAILURE'**
                    }
                }
          
           }
        }
    }
    
   
     stage('xyz) {
        options { 
            timestamps () 
            timeout(time: 6, unit: 'HOURS')
        }
        steps {
            
            bat "batch\\CloudValidation\\CreatePvtCollabSpace.bat 
      
        }
    }

can you please let me know how to address the need?

  • Does this answer your question? [Continue Jenkins pipeline past failed stage](https://stackoverflow.com/questions/40600621/continue-jenkins-pipeline-past-failed-stage) – CAustin Jul 15 '23 at 00:27

1 Answers1

0

If you are already able to fail the build, then just catch the error and only fail the stage without failing the whole build. To do this, wrap your steps in catchError:

steps {
   catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
      ....
   }  
}
M B
  • 2,700
  • 2
  • 15
  • 20
  • This will work if there is an error in the pipeline stage but I want to fail the stage if cntKO > 0. Forcibly mark the failure based on cntKO – Kamal Baldawa Jul 18 '23 at 17:35
  • Inside the `if` block, you can add `error "Error message"` and this will forcibly fail the build. However, if you had wrapped the block with the above `catchError`, this failure will be caught. – M B Jul 19 '23 at 07:34