简体   繁体   English

避免在 Jenkins Job DSL API 上重复

[英]Avoid duplicity on Jenkins Job DSL API

I´m configuring a couple Jenkins jobs using Jenkins Job DSL Plugin , and in the steps section for my two jobs it´s almost the same, just has to change one value " builder " by " couchbase ".我正在使用Jenkins Job DSL Plugin配置几个 Jenkins 作业,在我的两个作业的步骤部分几乎相同,只需要通过“ couchbase ”更改一个值“ builder ”。

For now seems like I´m breaking DRY and I´m duplicating a lot of code.现在似乎我正在打破 DRY 并且我正在复制很多代码。 Since I´m pretty newby on DSL I´m not quite sure if the API allow create a sort of generic code to avoid duplicate steps as I´m doing.由于我是 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)')
}

}

If it's as simple as the same steps for two named projects, a loop would suffice:如果它像两个命名项目的相同步骤一样简单,那么循环就足够了:

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)')
    }

  }
}

If you want something more complex, take a look at these examples , specifically the job builder class and usage of it如果您想要更复杂的东西,请查看这些示例,特别是作业生成器类及其用法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM