【问题标题】:Avoid duplicity on Jenkins Job DSL API在 Jenkins Job DSL API 上避免重复
【发布时间】: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 规则。

标签: jenkins jenkins-job-dsl


【解决方案1】:

如果两个命名项目的步骤一样简单,那么一个循环就足够了:

String[] names = ["builder", "couchbase"]

names.each {
  job("images/" + it) {
    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/' + it + '/Dockerfile .')
      shell('docker tag -f builder:latest ********.dkr.ecr.eu-west-1.amazonaws.com/' + it + ':latest')
      shell('docker push **********.dkr.ecr.eu-west-1.amazonaws.com/' + it + ':latest)')
    }

  }
}

如果您想要更复杂的东西,请查看these examples,特别是job builder classusage of it

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-23
    • 2015-03-25
    相关资源
    最近更新 更多