title: Jenkinsfile - Conditional stage execution in the Script Pipeline syntax
body: <p>We are using the <strong>Script Pipeline</strong> syntax for our <code>Jenkinsfile</code> which has a lot of stage defined to build and deploy our code. We have a use case where I want to run all my stages if I am doing a <strong><em>Full Build</em></strong> but only run one specific stage if I need to perform some AWS routing. I know I can use the <code>if(&lt;expression&gt;)</code> to skip a stage or run a stage. Problem is I don't want to apply that <code>if</code> condition to every stage in my <code>Jenkinsfile</code>. </p>
  
  <p>In the new <strong>Declarative Pipeline</strong> syntax this is easily possible using the <code>stage..when</code> option. We have a lot of custom Groovy helper function used in our infrastructure so it is not possible at this point for me to switch from the <strong>Script Pipeline</strong> syntax to the new <strong>Declarative Pipeline</strong> syntax. What I ended up doing on my existing <code>Jenkinsfile</code> is something like this..</p>
  
  <p>Original Jenkinsfile</p>
  
  <pre><code>  stage('Checkout Code') {}
    stage('Build') {}
    parallel(
      stage('Sonar Analysis') {}
      stage('CLM Analysis') {}
      stage('Security Analysis') {}
    )
    stage('Build Docker Image') {}
    ...
    ...
    stage('QA Deploy') {}
    stage('QA Routing') {}
    ...
    ...
    stage('Prod Deploy') {}
    stage('Prod Routing') {}
  </code></pre>
  
  <p>Changed to</p>
  
  <pre><code>  if (fullDeploy){
      stage('Full Build') {
          stage('Checkout Code') {}
          stage('Build') {}
          parallel(
            stage('Sonar Analysis') {}
            stage('CLM Analysis') {}
            stage('Security Analysis') {}
          )
          stage('Build Docker Image') {}
          ...
          ...
          stage('QA Deploy') {}
          stage('QA Routing') {}
          ...
          ...
          stage('Prod Deploy') {}
          stage('Prod Routing') {}          
      }
    }
  
    if (routeOnly){
      stage('Prod Routing') {}    
    } 
  </code></pre>
  
  <p>This feels a little hacky to me and I couldn't find any things on the Jenkins docs that provides a good way to do this.</p>
  
  <p>Has anyone got a better way in which I can incorporate this?</p>