Search code examples
javajenkinsgroovycontinuous-integrationjenkins-pipeline

Implementing conditional execution of Jenkins pipeline stages based on the 'choice' parameter


 pipeline {
  parameters {
choice choices: ['ME', 'FC', 'ME_and_FC'], description: 'Pick Something', name: 'Build_type'
 }
  agent {
    node{
        label 'Slave_APP_Train_32'
        customWorkspace 'App_WS'
    }
 }
stages {
    stage('FC') { steps {echo "D6-EPS EXECUTED"}}
    stage('FC') { steps {echo "D6-EPS EXECUTED"}}
    stage('FC') { steps {echo "D6-EPS EXECUTED"}}
      stage('parallel'){
          parallel {
            
            stage('ME') {
                stages {
                    stage('D6 SRA') { steps {echo "D6-EPS EXECUTED"}}
                   
                }
            }
            stage('ME') {
                stages {
                    stage('D6 EPS') { steps {echo "D6-EPS EXECUTED"}}
                   
                }
            }
            stage('ME') {
                stages {
                    stage('C3 EPS') { steps {echo "D6-EPS EXECUTED"}}
                   
                }
            }
        }
      }
    
    }
  }`

Need some conditional statement to call the ME and FC stages ,so that it runs based on the choices given in the choice parameter

This my script, i am not sure how to call the choice parameter with if condition Say for FC all the FC stages should run , for ME all the ME stages should run

I tried by calling with this command if(${params.Build_type=='ME'}||${params.Build_type=='ME_and_FC'})
 [ERROR](https://i.sstatic.net/x0BWA.png) it shows like not a valid syntax 

Can someone help me to figure it out`

Solution

  • you can use the when block for this see Pipeline Syntax.

    example:

    stage('FC') {
       when { expression { return params.Build_type == 'FC' } }
       steps {
          echo "D6-EPS EXECUTED"}
       }
    }