如何通过 Job DSL 设置 Active Choice Parameter 的脚本沙箱

How to set Active Choice Parameter's script sandbox via Job DSL

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

这里有一份link工作XML供参考:https://gist.github.com/vzabawski/aae51eddd45a51978224e403cc505b5b

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