首页 > 解决方案 > 直接从 Git Repo 获取 Json 的所有键作为列表

问题描述

对于我的 Jenkins 管道,我想将 GitRepo 中的 Json 中的所有键填充为扩展选择参数。

[$class: 'ChoiceParameter', choiceType: 'PT_SINGLE_SELECT', description: 'Select an option', name: 'Option', randomName: 'choice-parameter-112', script: [$class: 'GroovyScript', fallbackScript: [classpath: [], sandbox: false, script: ''], script: [classpath: [], sandbox: false,
  script: '''
      //What should I write here in this block?
      return keys
  ''']]]

基本上我只需要一个 curl 调用,我可以从上面的块中使用 Git 凭据 ID 从 Git 获取单个文件(我已将我的用户名和密码作为凭据 ID 存储在 Git 中)

标签: shelljenkins-pipeline

解决方案


这是一个常见的情况,归结为:

除非您shnode. 要拥有节点,您需要运行管道。要运行管道,您需要弄清楚参数。要找出这个参数,您需要调用sh. 看到问题了吗?

为了克服这个问题,您可以通过在主管道之前运行另一个(小)管道来欺骗系统,例如

node('master') {
    stage('read file') {
        def file_contents = sh (returnStdout: true, script: "curl myrepo/myfile.json")
        def parsed_data = readJSON text: file_contents
    }
}

接着

properties([
    parameters([
        [$class: 'ChoiceParameter',

等等


推荐阅读