Thursday, December 31, 2020

Call another job in Jenkins declarative pipeline with parameters

 HOWTO

 

Parameters on the pipeline job

pipeline {
    agent any
    parameters {
        string(name: 'PERSON', defaultValue: 'Mr Jenkins', description: 'Who should I say hello to?')

        text(name: 'BIOGRAPHY', defaultValue: '', description: 'Enter some information about the person')

        booleanParam(name: 'TOGGLE', defaultValue: true, description: 'Toggle this value')

        choice(name: 'CHOICE', choices: ['One', 'Two', 'Three'], description: 'Pick something')

        password(name: 'PASSWORD', defaultValue: 'SECRET', description: 'Enter a password')
    }
    stages {
        stage('Example') {
            steps {
                echo "Hello ${params.PERSON}"

                echo "Biography: ${params.BIOGRAPHY}"

                echo "Toggle: ${params.TOGGLE}"

                echo "Choice: ${params.CHOICE}"

                echo "Password: ${params.PASSWORD}"
            }
        }
    }
}

 

Called job


pipeline {
    agent { 
       label 'dave_node'
    }
    parameters {
        string(name: 'branch', defaultValue: 'main', description: 'Some branch')
    }
    stages {
        stage('CalledJob') {
        
            steps {
            
                 echo "Build CalledJob branch ${params.branch}"  
                 
                 script {         
                     params.each {param ->
                       println "${param.key} -> ${param.value} "
                     }
                 }
                         
            
                git url: 'https://github.com/dveselka/calledjob/', credentialsId: 'daveCredentials', branch: "${params.branch}"     
            }
        }
    }
}


Calling job

   
       stage('BuildOtherJob') {
            steps {   
            
            echo "Call other job job"        
                script {           
                      build job: 'jobtocall', parameters: [
                             string(name: 'branch', value: "some_branch")
                        ]
                }
            }
       }    


Dynamic agent

pipeline {
    agent { 
       label params.agent == null || params.agent.isEmpty() ? "default-agent" : params.agent
    }

No comments:

Post a Comment