Jenkins 管道模板 - 方法
Jenkins Pipeline Template - Approaches
我发现了一个博客 post,用于定义管道模板 here。以下两个声明有什么区别 -
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
:
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 via 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'
}
除了参数用法外,方法是相同的。这将取决于您的喜好。
我发现了一个博客 post,用于定义管道模板 here。以下两个声明有什么区别 -
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
:
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 via 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'
}
除了参数用法外,方法是相同的。这将取决于您的喜好。