以编程方式创建 jobdsl 参数时出错

Error creating jobdsl parameters programatically

我正在创建用于 Terraform 的作业。有多种环境,并且数量一直在增长。我没有在参数更改时同时更新管道文件和 jobdsl 文件,而是从扫描存储库中的环境文件并根据需要更新管道和 jobdsl 文件的角度开始工作。

我的 jobdsl 脚本:

@Library('mylib') _

params = [
  "serviceName": "infrastructure-${repo}",
  "repoUrl": "${repoUrl}",
  "sshCredentials": 'git-readonly',
  "environment": "${env.Environment}",
  "configParams": getTFConfigs(
      repoUrl, 
      "env/${env.AccountName}/${env.AWSRegion}/${env.Environment}")
  ]
template = libraryResource('dslTemplates/infra.groovy')
jobDsl scriptText: helpers.renderTemplate(template, params)

共享库方法:getTFConfigs

#!/usr/bin/env groovy

@NonCPS
import java.util.zip.ZipEntry
import java.util.zip.ZipInputStream

def call(String repoUrl, String filter=""){
  def gitProc = new ProcessBuilder(
    "git",
    "archive",
    "--format=zip",
    "--remote=${repoUrl}",
    "main").start()
  def zipIn = new ZipInputStream(gitProc.inputStream)
  def zipMembers = []
  while (true) {
    def ZipEntry entry = zipIn.getNextEntry()
    if (entry == null) break
    if ( (entry.getName()).contains(filter) ) {
      entryName = entry.getName()
      zipMembers.push("${entryName}")
    }
  }
  println zipMembers
  return zipMembers
}

dslTemplates/infra.groovy 模板

pipelineJob("${serviceName}") {
  description("Apply TF for ${serviceName} to all environment configurations")
  definition {
    parameters {
      <% configParams.each { %>
      booleanParam(name: "<%= "${it}" %>", defaultValue: true, description: "<%= "${it}" %>" )
      <% } %>
    }
    logRotator {
      numToKeep(20)
    }
    cpsScm {
      scm {
        git{
          remote{
            url("${repoUrl}")
            credentials("${sshCredentials}")
            branch('*/main')
          }
        }
      }
      scriptPath('infra.groovy')
    }
  }
}

模板结果

...  
definition {
    parameters {

      booleanParam(name: env1.tfvars, defaultValue: true, description: env1.tfvars )

      booleanParam(name: env2.tfvars, defaultValue: true, description: env2.tfvars )

    }
...

当种子作业运行并执行代码时,应使用每个环境的复选框更新参数。然而,jobdsl 失败了:

ERROR: (script, line 6) No signature of method: javaposse.jobdsl.dsl.helpers.BuildParametersContext.booleanParam() is applicable for argument types: (java.util.LinkedHashMap) values: [[name:env1.tfvars, defaultValue:true, ...]]
Possible solutions: booleanParam(java.lang.String), booleanParam(java.lang.String, boolean), booleanParam(java.lang.String, boolean, java.lang.String)
Finished: FAILURE

我已经尝试在各个步骤中应用“toString()”,但似乎找不到任何解决方案。 我试图将整个 jobdsl 脚本写入一个文件,然后使用“jobDsl 目标:文件名”将其读回并得到相同的结果!

敲我的头!原来如此!

谢谢

你好像用过 Pipeline Syntax for the parameters in DSL script. If you want to define a parameter in a DSL script do not use name, defaultValue and description. (See Job DSL Plugin)
booleanParam('BOOL_PARAM', true, 'This is a boolean param')