title: How to exit from the Jenkins pipeline if a stage sets build fail/unstable status?
body: <p>I have a declarative Jenkins pipeline with <code>stage1</code>, <code>stage2</code>, <code>stage3</code> and so on. I want to stop <code>stage2</code> from running if <code>stage1</code> sets the build unstable/fail.</p>
  
  <p>I know I can stop the steps in <code>stage1</code> from running using <code>return</code> when the build is not success but couldn't find a way where I can just exit the pipeline without running the stages below <code>stage1</code></p>
  
  <p>Here is what I have:</p>
  
  <pre><code>    stage('stage1') {
              steps {
                  script{
                      //somesteps
                      if ("${stdout}" == "1"){
                      currentBuild.result = 'UNSTABLE'
                      return
                      } //if
                      //somesteps
              } //script
          } //steps
      } //stage
  
      // run only when stage1 is success
      stage('stage2'){
          when {
              expression { 
               params.name ==~ /x|y/
              }
          }
          steps {
              script{
                      //stage2 steps
              }
          }
      }
  </code></pre>
  
  <p>If <code>params.name ==~ /z/</code> stage 3 will be executed skippping stage2</p>
  
  <p>Note: I cannot include the steps in stage2/3/.. in stage1. It should be that way. Based on the build paramters stage2/3/4... will be called after stage1</p>