在扩展选择参数中访问构建参数 - 在促销步骤中?

时间:2014-12-12 18:46:34

标签: jenkins jenkins-plugins

在宣传构建时,我想揭示一些如何/促进什么的选择。为了正确地做到这一点,我需要一些关于我正在推广的构建的信息,我真的想使用扩展选择参数来做到这一点。

如何在构建升级步骤中访问扩展选择参数块中的构建参数?例如,假设我想要推广的构建是使用参数myvar="1,2,5"构建的,我想让构建启动器选择1,2,5或某种组合。如何在促销流程中的扩展选项参数中访问特定版本的myvar值?

我可以使用可用的Git分支自动填充这些具有全局环境变量的特定仓库,但是我还没有看到我需要访问有关我即将(即将)推广的构建的信息的groovy魔法。 / p>

2 个答案:

答案 0 :(得分:2)

这对我来说并不明显,但我发现在促销步骤进行之前,PROMOTED_URLPROMOTED_JOB_NAME等环境变量无法使用。当通过扩展选择参数的Groovy脚本字段填充促销参数时,我需要公开此信息。我通过窥视线程名称来完成它:

import jenkins.*
import jenkins.model.*
import hudson.*
import hudson.model.*

def jenkins = Jenkins.instance
def thread = Thread.currentThread().toString()
def (jobName,buildNumber) = thread.replaceAll(/.*GET \/job\/(.*)\/promotion\/.*/, '$1').split('/')
def job = jenkins.getJob(jobName)
def build = job.getBuild(buildNumber)

return build.getBuildVariables()["SOME_BUILD_PARAM"]

我该怎么用?假设我可以在构建时选择要构建的某些目标,而不是其他目标。我希望能够仅提升那些实际构建的的某些子集,因此参数不能只保留默认值。相当狭隘的边缘情况,我会批准,但有时看起来就像詹金斯所关注的那样,我的整个生活正在处理一些边缘情况或其他情况。如果有更优雅的方式,我全都是耳朵!

答案 1 :(得分:0)

构建@courtlandj解决方案(使用最新的hudson API):

import hudson.model.*

def paramToLookup = "SOME_BUILD_VARIABLE_NAME"

// you can of course parse the jobName from the currentThread
// but that is a case-by-case basis depending on how you name them
// build numbers are strictly numeric - and more predictable to parse with regex
def jobName = "some-jenkins-full-job-name"
int buildNumber = 0

// Similar approach to courtlandj to get THIS build number (of page being viewed):
def thisPromotionInfo = Thread.currentThread().toString()

// groovy regex that gets the promotion digits (\d)
def buildRegex = /.*\/(\d+)\/promotion\/.*/
def matcher = ( thisPromotionInfo =~ buildRegex )

if (matcher.matches()) {
  buildNumber = matcher[0][1].toInteger()
} else {
  return "unable to identify buildNumber in Extended Choice Parameter (see Jenkins Config)"
}

// borrowed slightly from here: https://wiki.jenkins-ci.org/display/JENKINS/Display+job+parameters
try {
  def job = Hudson.instance.getItemByFullName(jobName)
  def build = job.getBuildByNumber(buildNumber)
  return build.getProperties()["buildVariableResolver"].resolve(paramToLookup).toString()
} catch(Exception ex) {
    return ex.toString()
}

我借来的相关链接:

https://groups.google.com/forum/#!topic/jenkinsci-users/cHClVbjuSN4 http://groovy.jmiguel.eu/groovy.codehaus.org/Tutorial+5+-+Capturing+regex+groups.html http://hudson-ci.org/javadoc/hudson/model/Hudson.html