【发布时间】:2016-05-06 02:03:13
【问题描述】:
我正在使用 Jenkins Job DSL 插件 配置几个 Jenkins 作业,在我的两个作业的步骤部分中,它几乎相同,只需更改一个值“ builder" 由 "couchbase"。
现在看来我正在打破 DRY 并且我正在复制大量代码。由于我是 DSL 的新手,我不太确定 API 是否允许创建一种通用代码以避免重复步骤。
job("images/builder") {
concurrentBuild()
triggers {
githubPush()
}
scm {
git {
remote {
github("aws", "https", "github.dev.global.com")
credentials('***********')
}
}
}
steps {
shell('export AWS_DEFAULT_REGION=eu-west-1')
shell('$(aws ecr get-login --region eu-west-1)')
shell('docker build -t builder -f ./images/builder/Dockerfile .')
shell('docker tag -f builder:latest **********.dkr.ecr.eu-west-1.amazonaws.com/builder:latest')
shell('docker push **********.dkr.ecr.eu-west-1.amazonaws.com/builder:latest)')
}
}
job("images/couchbase") {
concurrentBuild()
triggers {
githubPush()
}
scm {
git {
remote {
github("aws2", "https", "github.dev.global.com")
credentials('****************')
}
}
}
steps {
shell('export AWS_DEFAULT_REGION=eu-west-1')
shell('$(aws ecr get-login --region eu-west-1)')
shell('docker build -t builder -f ./images/couchbase/Dockerfile .')
shell('docker tag -f builder:latest ********.dkr.ecr.eu-west-1.amazonaws.com/couchbase:latest')
shell('docker push **********.dkr.ecr.eu-west-1.amazonaws.com/couchbase:latest)')
}
}
【问题讨论】:
-
Job DSL 是基于 Groovy 构建的,因此可以将其与 Groovy 代码混合以将更多逻辑添加到种子作业中。结果遵循 DRY 规则。