Jenkins:如何访问参数化作业中的参数 [英] Jenkins: How to access parameters in a parameterized job

查看:471
本文介绍了Jenkins:如何访问参数化作业中的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像下面这样的参数化版本:



然后我创建了一个groovy脚本来创建一个变量URL_TOMCAT,其值取决于TARGET_TOMCAT参数:

即使在更新我得到了同样的错误

  import hudson.model。* 
def target = build.buildVariableResolver.resolve( TARGET_TOMCAT)
def URL_TOMCAT =
switch(target){
casetomcat1:URL_TOMCAT =http:// localhost:8080 / manager / text
break
casetomcat2:URL_TOMCAT =http:// localhost:8089 / manager / text
break
}

然后,我想获得URL_TOMCAT值并调整maven构建步骤,如图所示:



但是我得到这个错误:



您可以像groovy一样将其写入文件中:

<$ p $ ($ {workspace} \\Environment.Variables)。write(URL_TOMCAT = $ {code workspace} = build.buildVariableResolver.resolve(WORKSPACE)
新文件URL_TOMCAT})

如果您不想将它写入作业工作区,您可以跳过抓取



在您的groovy构建步骤之后,添加一个Envinject构建步骤,并在属性文件中输入包含键值对的文件的路径路径字段。您应该能够像构建的其他部分中的任何其他环境变量一样引用URL_TOMCAT。继续使用about路径,我会使用 $ {WORKSPACE} \Environment.Variables 作为路径。


I have a parameterized build like below:

then i've create a groovy script to create a variable URL_TOMCAT where its value depends on the TARGET_TOMCAT parameter:

Even after this update i got the same error

import hudson.model.*
def target = build.buildVariableResolver.resolve("TARGET_TOMCAT")
def URL_TOMCAT = ""
switch(target ) {
  case "tomcat1": URL_TOMCAT= "http://localhost:8080/manager/text"
   break
  case "tomcat2": URL_TOMCAT = "http://localhost:8089/manager/text"
   break  
}

Then i want to get The URL_TOMCAT value and adjust the maven build step as shown:

But i got this error :

Has any on an idea how to fix this error?

解决方案

In your groovy script you need to make an API call to get the value of the parameter from Jenkins into your workspace.

Import hudson.model

def targetTomcat = build.buildVariableResolver.resolve("TARGET_TOMCAT")

def URL_TOMCAT = ""
switch(targetTomcat) {
  case "tomcat1": URL_TOMCAT = "http://localhost:8080/manager/text"
    break
  case "tomcat2": URL_TOMCAT = "http://localhost:8089/manager/text"
    break  
}

I want to point out that the URL_TOMCAT variable won't be available to any other buildsteps, it's scoped to just the groovy build step. If you want to expose your URL_TOMCAT variable to the rest of the build you'll need to expose it to the build environment somehow. I normally do this by writing the value to a file as a key value pair and using the EnvInject Plugin

You can write it to a file in groovy like so:

def workspace = build.buildVariableResolver.resolve("WORKSPACE")
new File("${workspace}\\Environment.Variables").write("URL_TOMCAT=${URL_TOMCAT}")

If you don't want to write it to the jobs workspace you can skip grabbing that value and just code a specific path.

After your groovy build step add an Envinject build step and enter the path to the file containing the key value pair in the Properties File Path field. You should be able to reference URL_TOMCAT like any other environment variable in the rest of the build. Continuing with the about path I would use ${WORKSPACE}\Environment.Variables as the path.

这篇关于Jenkins:如何访问参数化作业中的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆