【问题标题】:How to set Active Choice Parameter's script sandbox via Job DSL如何通过 Job DSL 设置 Active Choice Parameter 脚本沙箱
【发布时间】:2021-10-04 22:37:30
【问题描述】:

我有一个 JobDSL 脚本,它使用 Active Choice Parameters 插件提供的 Active Choice 参数创建 Jenkins 管道作业。不幸的是,JobDSL 不支持在沙箱中运行 Active Choice Groovy 脚本的参数(在 UI 中可用),所以我试图通过配置块启用它。

这是我的 JobDSL 脚本:

pipelineJob("my-pipeline") {
  logRotator(-1, 10)
  parameters {
    activeChoiceParam('Branch') {
      description('Lists branches for integration job')
      filterable()
      choiceType('SINGLE_SELECT')
      groovyScript {
          script("return ['The list of branches']")
          fallbackScript("return ['Unable to list branches']")
      }
    }
    activeChoiceReactiveParam('Build') {
      description('Specifies which build from selected branch will be used for deployment. Only builds that contain Terraform plan are listed.')
      choiceType('SINGLE_SELECT')
      groovyScript {
          script("return ['Selected build']")
          fallbackScript("return ['Unable to list builds']")
      }
      referencedParameter('Branch')
    }
    activeChoiceReactiveReferenceParam('Artifacts') {
      description('Lists artifacts from build specified')
      choiceType('FORMATTED_HTML')
      groovyScript {
          script(scriptGen("return ['Job artifacts']")
          fallbackScript("return ['Unable to list artifacts']")
      }
      referencedParameter('Branch')
      referencedParameter('Build')
    }
  }
  definition {
    cpsScm {
      scm {
        git {
          remote {
            github('mainorg/my-repo', 'https', 'github.com')
            credentials('my-creds')
          }
          branch('*/master')
        }
      }
      scriptPath("ci/Jenkinsfile")
      lightweight(true)
    }
  }
  configure { 
      it / 'properties' / 'hudson.model.ParametersDefinitionPropert' / 'parameterDefinitions' / 'org.biouno.unochoice.ChoiceParameter' / 'script' / 'secureScript' {
        'sandbox'('true')
    }
  }
}

使用configure 块,我试图覆盖<secureFallbackScript><secureScript> under<sandbox> 节点的值,但它不起作用。它会擦除所有其他节点。我不太擅长 Groovy,所以我非常感谢任何帮助。 将所有<sandbox> 节点值覆盖为true 的正确方法是什么?提前致谢。

这里是作业 XML 的链接以供参考:https://gist.github.com/vzabawski/aae51eddd45a51978224e403cc505b5b

【问题讨论】:

    标签: jenkins groovy jenkins-plugins jenkins-groovy jenkins-job-dsl


    【解决方案1】:

    activeChoiceParamactiveChoiceReactiveParamactiveChoiceReactiveReferenceParam 函数是静态 API 的一部分。 Job DSL 作者介绍了dynamic API。只要动态 API 可以用来做某事,就不再支持负责相同逻辑的静态 API。

    您应该阅读这些页面:

    如何从静态 API 切换到动态 API 的示例:

    activeChoiceParam('Branch') {
      description('Lists branches for integration job')
      filterable()
      choiceType('SINGLE_SELECT')
      groovyScript {
        script("return ['The list of branches']")
        fallbackScript("return ['Unable to list branches']")
      }
    }
    

    choiceParameter {
      name('Branch')
      description('Lists branches for integration job')
      filterable(true)
      choiceType('PT_SINGLE_SELECT')
      script {
        groovyScript {
          script {
            script("return ['The list of branches']")
            sandbox(true)
          }
          fallbackScript {
            script("return ['Unable to list branches']")
            sandbox(true)
          }
        }
      }
      randomName('')
      filterLength(0)
    }
    

    【讨论】:

    • 我收到了ERROR: (infrastructure_jobs_ccloud.groovy, line 100) No signature of method: java.lang.String.call() is applicable for argument types: (java.lang.String) values: [Branch] Possible solutions: wait(), chars(), any(), wait(long), split(java.lang.String), any(groovy.lang.Closure),这很奇怪。试图找出原因。
    • 我很确定你做了类似的事情:def name = 'Job Name'; choiceParameter { name(name)。在这种情况下,脚本无法解析name 方法(它被name 变量隐藏)。您必须使用delegate 对象来指向方法:def name = 'Job Name'; choiceParameter { delegate.name(name)
    • 是的,你是对的。我在代码的开头有一个变量,我忘记了它的存在。非常感谢。
    • 值得一提:SINGLE_SELECT 应替换为PT_SINGLE_SELECT
    猜你喜欢
    • 1970-01-01
    • 2021-06-03
    • 2016-01-11
    • 1970-01-01
    • 2022-08-11
    • 2016-07-06
    • 1970-01-01
    • 2012-08-09
    • 1970-01-01
    相关资源
    最近更新 更多