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

时间:2019-07-01 08:30:52

标签: jenkins jenkins-pipeline jenkins-plugins jenkins-groovy

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

例如,在下面标记为“脚本”的部分中,我将如何访问env.GIT_URL?

要清楚,我可以在管道的各个阶段访问“ $ {env.GIT_URL}”,但这在属性的脚本中返回null。

也尝试在脚本中查看jenkinsProject,但返回:“无此类属性:jenkinsProject类:Script1”

也尝试了scm.getUserRemoteConfigs()[0].getUrl(),但返回:“没有这样的属性:scm,用于类:Script1”

也尝试了build.getBuildVariables().get('GIT_URL'),但返回“没有这样的属性:为类Script1构建”

也尝试过System.getenv('GIT_URL'),但返回null

也尝试过: def thr = Thread.currentThread() def build = thr?.executable def envVarsMap = build.parent.builds[0].properties.get("envVars") 但这会返回“无此类属性:类的可执行文件:java.lang.Thread”

也尝试过: def build = this.getProperty('binding').getVariable('build') def listener = this.getProperty('binding').getVariable('listener') def env = build.getEnvironment(listener) ,但返回“没有这样的属性:为类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

1 个答案:

答案 0 :(得分:0)

我想我已经找到了答案...逃避这一切似乎都有效...

\“ $ {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']
                            """
                ]
            ]
        ],
    ])
])
}