Jenkins管道模板-方法

Jenkins管道模板-方法,jenkins,groovy,continuous-integration,jenkins-pipeline,continuous-deployment,Jenkins,Groovy,Continuous Integration,Jenkins Pipeline,Continuous Deployment,我看到一篇关于定义管道模板的博客文章。以下两种声明之间有什么区别- vars/myDeliveryPipeline.groovy def call(Map pipelineParams) { pipeline { agent any stages { stage('checkout git') { steps { git branch: pipelinePar

我看到一篇关于定义管道模板的博客文章。以下两种声明之间有什么区别-

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}"
            }
        }
    }
}
第二种方法

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
        ...
    }
}


这里的本质区别在于调用期间将管道参数传递给包含管道的方法的用法

对于第一个示例,您将通过
myDeliveryPipeline(params)
直接传递
Map

对于第二个示例,您将通过类似DSL的闭包通过
myDeliveryPipeline{params}
传递
Map

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

除了参数用法之外,这些方法是相同的。这将取决于您的喜好。

谢谢@Matt Schuchard。除了Matt的答案,我在以下内容中找到了这一段:“还有一个‘构建器模式’技巧,首先使用Groovy的Closure.DELEGATE_,它允许Jenkinsfile看起来更像一个配置文件而不是一个程序,但这更复杂,更容易出错,因此不推荐使用。“我不知道为什么第二种方法可能更复杂,但我认为这可能值得分享。@RobertStrauch第二种方法是以前推荐的用法,它确实存在问题。现在推荐使用第一种方法的修改版本。例如,第一种用法允许将
env
params
映射直接作为参数传递,而第二种用法不允许这样做(未解决的错误)。@MattSchuchard感谢您提供的信息。我不知道。
myDeliveryPipeline {
  branch            = 'master'
  scmUrl            = 'ssh://git@myScmServer.com/repos/myRepo.git'
  email             = 'team@example.com'
  serverPort        = '8080'
  developmentServer = 'dev-myproject.mycompany.com'
  stagingServer     = 'staging-myproject.mycompany.com'
  productionServer  = 'production-myproject.mycompany.com'
}