如何在 jenkinsfile 的属性步骤中访问 git url?

How to access git url in properties step of jenkinsfile?

我正在使用 Active Choices 插件,您如何访问 jenkinsfile 属性部分的脚本部分中的 git url?

例如,在下面标记为 "script" 的部分中,我将如何访问 env.GIT_URL???

明确地说,我可以在管道的各个阶段访问“${env.GIT_URL}”,但是属性脚本中的这个 returns null...

也尝试在脚本中查看 jenkinsProject 但 returns:"No such property: jenkinsProject for class: Script1"

也试过scm.getUserRemoteConfigs()[0].getUrl(),但是returns:"No such property: scm for class: Script1"

也试过build.getBuildVariables().get('GIT_URL'),但是returns"No such property: build for class: Script1"

也试过System.getenv('GIT_URL'),但是returns无效

也尝试过: def thr = Thread.currentThread() def build = thr?.executable def envVarsMap = build.parent.builds[0].properties.get("envVars") 但是这个 returns "No such property: executable for class: java.lang.Thread"

也试过: def build = this.getProperty('binding').getVariable('build') def listener = this.getProperty('binding').getVariable('listener') def env = build.getEnvironment(listener) ,但是returns"No such property: build for class: groovy.lang.Binding"

node{
  properties([
      parameters([
          [$class: 'ChoiceParameter',
              choiceType: 'PT_SINGLE_SELECT',
              description: 'The names',
              filterable: false,
              name: 'Name',
              randomName: 'choice-parameter-5631314439613978',
              script: [
                  $class: 'GroovyScript',
                  script: [
                      classpath: [],
                      sandbox: false,
                      // note, changes to this script need script approval in Jenkins (see jenkins/scriptApproval)
                      script: """
                                 // how to get env.git_url at this point?
                                 return "anything"
                              """
                  ]
              ]
          ],
      ])
  ])
}
pipeline {
  ...
}

我正在使用 Jenkins v2.121.2,带有 Active Choices 插件 v2.1

我想我已经找到答案了……逃避这一切似乎可行……

\"${scm.userRemoteConfigs[0].url}\"

所以我的 Jenkinsfile 中的属性看起来像这样...注意,这也需要包装在一个节点块中。

node{
properties([
    parameters([
        [$class: 'ChoiceParameter',
            choiceType: 'PT_SINGLE_SELECT',
            description: 'All tags',
            filterable: false,
            name: 'Tag',
            randomName: 'choice-parameter-5631314439613999',
            script: [
                $class: 'GroovyScript',
                script: [
                    classpath: [],
                    sandbox: false,
                    script: """
                              def sout = new StringBuilder(), serr = new StringBuilder()
                              def proc = String.format(\"git ls-remote --tags %s\", \"${scm.userRemoteConfigs[0].url}\").execute()
                              proc.consumeProcessOutput(sout, serr)
                              proc.waitForOrKill(30000)

                              def tagList = sout.tokenize().findAll { it.endsWith(\"{}\") }
                              return tagList.sort()
                            """
                ]
            ]
        ],
        [$class: 'ChoiceParameter',
            choiceType: 'PT_SINGLE_SELECT',
            description: 'Build Configuration - whether to build the solution/project as either a Debug or Release build.',
            filterable: false,
            name: 'Config',
            randomName: 'choice-parameter-5631314439615999',
            script: [
                $class: 'GroovyScript',
                script: [
                    classpath: [],
                    sandbox: false,
                    // note, changes to this script need script approval in Jenkins (see jenkins/scriptApproval)
                    script: """
                              return ['Debug:selected', 'Release']
                            """
                ]
            ]
        ],
    ])
])
}