【问题标题】:Jenkins Pipeline Template - ApproachesJenkins 流水线模板 - 方法
【发布时间】:2019-08-06 00:12:24
【问题描述】:

我看到了一篇关于定义管道模板的博文here。以下2个声明有什么区别-

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

【问题讨论】:

    标签: jenkins groovy continuous-integration jenkins-pipeline continuous-deployment


    【解决方案1】:

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

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

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

    对于第二个示例,您将通过类似于 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。
    • 除了马特的回答,我在Jenkins documentation 中找到了这一段:“还有一个使用 Groovy 的 Closure.DELEGATE_FIRST 的‘构建器模式’技巧,它允许 Jenkinsfile 看起来更像一个配置文件比程序,但这更复杂且容易出错,不推荐使用。”我不确定为什么第二种方法可能更复杂,但我认为这可能值得分享。
    • @RobertStrauch 第二种方法是以前推荐的用法,但确实存在问题。现在推荐使用第一种方法的修改版本。例如,第一种用法允许将envparams 映射直接作为参数传递,而第二种用法不允许这样做(未解决的错误)。
    • @MattSchuchard 感谢您提供的信息。不知道。
    猜你喜欢
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 2021-09-29
    • 2022-09-26
    • 2017-12-21
    • 1970-01-01
    • 1970-01-01
    • 2021-10-07
    相关资源
    最近更新 更多