Declarative Pipeline When Conditions

Viewed 2814

I'm in need of help with the when condition on Jenkins. I want to take the words FULL_BUILD from the merge request title and then execute different stages based on whether it is a FULL_BUILD or someone just submitting a merge request to master that doesn't need to go through Veracode, SonarQube, etc etc (these stages are not pasted in as they are just repeats of the when conditions below). However, I have to repeat this crazy when condition on EVERY stage, as well as sometimes creating a special stage that only executes on FULL_BUILD or "regular" builds.

Has anyone created a @NonCPS script to set a true/false variable? Or is there a crafty way to execute the script on startup to set a reusable variable?

I want to have the users execute everything from their Gitlab MR and not have to go to Jenkins to hit a button (hence I do not use a parameter of boolean).

pipeline {
    agent {
        node {
            label 'master'
        }
    }

    parameters{
        //I am trying to pull the information for a full build from the Merge Request
    }

    environment {
        //Assume random variables all work fine
    }

    options {
        skipDefaultCheckout() 
        gitLabConnection('GitLab_Generic')  
        timeout(time: 60, unit: 'MINUTES')
    }
    triggers {
        gitlab(triggerOnPush: true, triggerOnMergeRequest: true, branchFilterType: 'All')
    }

    stages {
        stage('Checkout') {
            steps {
                checkout scm
            }
        }


        stage('Build') {

            steps {

                dir("${env.WORKSPACE}\\${params.PROJECT_NAME}") {
                    bat 'nuget.exe restore %PROJECT_NAME%.sln'
                }

                dir("${env.WORKSPACE}\\${params.PROJECT_NAME}\\build") {
                    bat "\"${tool 'msbuild'}\"  %COMPONENT%.XML  /p:Configuration=Debug "

                }

                dir("${env.WORKSPACE}") {
                    echo "Creating a Build status file"
                    writeFile file: "output/MR_Title.txt", text: "BUILD STATUS:"
                }
            }
        }

        stage('Check MR FULL_BUILD' ){
            when { 
                    branch 'master'      
            }
            steps{
                dir("${env.WORKSPACE}") {
                    //writeFile file: "MR_Title.txt", text: "BUILD STATUS:"
                   powershell '& "./build/scripts/MergeRequestAPI.ps1" -GIT_CREDENTIALS $env:GIT_API_TOKEN -PROJECT_ID $env:GIT_PROJECT_ID | Out-File output/MR_Title.txt -Encoding utf8"'   
                }
            }
        }


        stage('Package Snapshot') {
            when {     
                    allOf  {
                        branch 'master'      
                        not {
                            expression {
                                return readFile('output/MR_Title.txt').contains("FULL BUILD")
                            }
                        }
                    }   
                }    
                steps {
                    dir("${env.WORKSPACE}\\${params.PROJECT_NAME}\\build") {
                        bat "\"${tool 'msbuild'}\"  %COMPONENT%.XML /t:Publish /p:version=${env.SnapshotComponentVersion} "
                   }
                }
            }


        stage('Package Full Build') {
            when {     
                allOf  {
                    branch 'master'      
                    expression {
                        return readFile('output/MR_Title.txt').contains("FULL BUILD")
                    }                  
                }   
            }

            steps {
                    dir("${env.WORKSPACE}\\${params.PROJECT_NAME}\\build") {
                          bat "\"${tool 'msbuild'}\"  %COMPONENT%.XML /t:Publish /p:version=${env.ComponentVersion} "
                }
            }
        }
    }
}
0 Answers
Related