Search This Blog

Breaking

Wednesday, 8 December 2021

[Jenkins] Conditional statement in pipeline job

If we have a pipeline job and if we want to trigger build based on some conditions, we can use if -else statement in the pipeline. I faced this situation for my master parallel job which was used for calling other jobs. Now, I need to call those jobs based on the choices or environment, so I try to put an if- else condition there.

Below is one sample pipeline, which executes code based on your choice parameter.







    



 


pipeline {

    agent any

    tools {

        maven 'Maven3'

        jdk 'java8'

    }

    

     parameters {

        choice(choices: ['QA', 'stage', 'dev'], description: 'Which Env?', name: 'PickYourEnv')

    }

    

    stages {

        stage('Build') {

            steps {

                 script {

                     echo "This is environment name : " + String.valueOf(params.PickYourEnv)

                       currentBuild.description = "${params.PickYourEnv}"; 

                    if (String.valueOf(params.PickYourEnv) == 'QA') {

                        echo 'I only execute on the master branch'

                        git 'https://github.com/qamatters/KarateDemo.git'

                        bat "mvn clean test -DargLine='-Dkarate.env=e2e' -Dtest=CucumberReport -DfailIfNoTests=false"

                    } 

                    else {

                        echo 'I execute elsewhere'

                    }

            }

    }

            post {

                // If Maven was able to run the tests, even if some of the test

                // failed, record the test results and archive the jar file.

                success {

                    cucumber buildStatus: 'null', customCssFiles: '', customJsFiles: '', failedFeaturesNumber: -1, failedScenariosNumber: -1, failedStepsNumber: -1, fileIncludePattern: '**/target/karate-reports/*json', pendingStepsNumber: -1, skippedStepsNumber: -1, sortingMethod: 'ALPHABETICAL', undefinedStepsNumber: -1

                }

            }

        }

    }

}


No comments:

Post a Comment