Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins Pipeline Template - Approaches

I came across a blog post for defining pipeline templates here. What is the difference between the below 2 declarations -

vars/myDeliveryPipeline.groovy
def call(Map pipelineParams) {

    pipeline {
        agent any
        stages {
            stage('checkout git') {
                steps {
                    git branch: pipelineParams.branch, credentialsId: 'GitCredentials', url: pipelineParams.scmUrl
                }
            }

            stage('build') {
                steps {
                    sh 'mvn clean package -DskipTests=true'
                }
            }

            stage ('test') {
                steps {
                    parallel (
                        "unit tests": { sh 'mvn test' },
                        "integration tests": { sh 'mvn integration-test' }
                    )
                }
            }

            stage('deploy developmentServer'){
                steps {
                    deploy(pipelineParams.developmentServer, pipelineParams.serverPort)
                }
            }

            stage('deploy staging'){
                steps {
                    deploy(pipelineParams.stagingServer, pipelineParams.serverPort)
                }
            }

            stage('deploy production'){
                steps {
                    deploy(pipelineParams.productionServer, pipelineParams.serverPort)
                }
            }
        }
        post {
            failure {
                mail to: pipelineParams.email, subject: 'Pipeline failed', body: "${env.BUILD_URL}"
            }
        }
    }
}

2nd Approach

vars/myDeliveryPipeline.groovy
def call(body) {
    // evaluate the body block, and collect configuration into the object
    def pipelineParams= [:]
    body.resolveStrategy = Closure.DELEGATE_FIRST
    body.delegate = pipelineParams
    body()

    pipeline {
        // our complete declarative pipeline can go in here
        ...
    }
}

like image 886
Punter Vicky Avatar asked Oct 20 '25 11:10

Punter Vicky


1 Answers

The essential difference here is in the usage for the passing of the Pipeline parameters to the method containing the pipeline during invocation.

For the first example, you will be passing a Map directly via myDeliveryPipeline(params):

myDeliveryPipeline(branch: 'master',
                   scmUrl: 'ssh://[email protected]/repos/myRepo.git',
                   email: '[email protected]', serverPort: '8080',
                   serverPort: '8080',
                   developmentServer: 'dev-myproject.mycompany.com',
                   stagingServer: 'staging-myproject.mycompany.com',
                   productionServer: 'production-myproject.mycompany.com')

For the second example, you will be passing a Map via a closure that resembles a DSL via myDeliveryPipeline { params }:

myDeliveryPipeline {
  branch            = 'master'
  scmUrl            = 'ssh://[email protected]/repos/myRepo.git'
  email             = '[email protected]'
  serverPort        = '8080'
  developmentServer = 'dev-myproject.mycompany.com'
  stagingServer     = 'staging-myproject.mycompany.com'
  productionServer  = 'production-myproject.mycompany.com'
}

Other than argument usage, the methods are identical. It will come down to your preference.

like image 102
Matt Schuchard Avatar answered Oct 23 '25 07:10

Matt Schuchard