Dynamically defining variables (process control statements) in jenkins pipeline

Keywords: git jenkins

Scenario: define different code warehouse addresses and branches based on job names

Solution: use script to include the whole code download process

The conventional way of writing is as follows:

pipeline
{
    agent { label 'test' } 

    stages
    {
        stage('DownloadCode')
        {
            steps
            {
                
                checkout([
                    $class: 'GitSCM', 
                    branches: [[name: 'master']], 
                    doGenerateSubmoduleConfigurations: false, 
                    extensions: [
                        [$class: 'CleanBeforeCheckout'], 
                        [$class: 'RelativeTargetDirectory', relativeTargetDir: 'code'], 
                        [$class: 'SubmoduleOption', disableSubmodules: false, parentCredentials: true, recursiveSubmodules: true, reference: '', timeout: 3, trackingSubmodules: false]], 
                    submoduleCfg: [], 
                    userRemoteConfigs: [[credentialsId: 'xxx', 
                    url: 'git@xxx.git']]])
            }
        }
        
        
    }
}

The disadvantage of this writing method is to write the code warehouse address and branch. If there are multiple similar job s, but the code warehouse is different, multiple jenkinsfiles are needed

 

Imagine that for such multiple pipelines, the steps are the same or similar, but the code warehouse is different. Use a Jenkinsfile uniformly. In Jenkinsfile, define git url address, branch and other variables respectively by judging the name of the pipeline

When using switch and other statements in Jenkins file, it needs to be in script {}, so the defined variables cannot be obtained outside script

When trying multiple methods, there will always be an error or the previously defined variables cannot be obtained when downloading the code below

Final solution:

pipeline
{
    agent { label 'test' } 

    stages
    {
        stage('DownloadCode')
        {
            steps
            {
                script
                {
                    //according jenkins job name to set git url and branch to download
                    switch(env.JOB_NAME)
                    {
                        case "pipeline1":
                            url = 'git@url1.git'
                            branch = 'release'
                            break
                        case "pipeline2":
                            url = 'git@url2.git'
                            branch = 'master'
                            break
                        case "pipeline3":
                            url = 'git@code.url3.git'
                            branch = 'develop'
                            break
                        default:
                            echo "############ wrong pipeline name ############"
                            break
                    }
                checkout([
                    $class: 'GitSCM', 
                    branches: [[name: "$branch"]], //You have to use "$branch" here
                    doGenerateSubmoduleConfigurations: false, 
                    extensions: [
                        [$class: 'CleanBeforeCheckout'], 
                        [$class: 'RelativeTargetDirectory', relativeTargetDir: 'code'], 
                        [$class: 'SubmoduleOption', disableSubmodules: false, parentCredentials: true, recursiveSubmodules: true, reference: '', timeout: 3, trackingSubmodules: false]], 
                    submoduleCfg: [], 
                    userRemoteConfigs: [[credentialsId: 'xxx', 
                    url: "$url"]]]) //You must use "$url" here
                }
            }
        }
        
        
    }
}

 

Posted by developer on Mon, 16 Dec 2019 12:01:52 -0800