sto*_*el2 5 key-value-observing jenkins jenkins-plugins keyvaluepair
这是我在 Jenkins 中有参数的要求:
1. User selects 3 Values from Dropdown: DEV, QA, PROD
2. Upon selection I need to return single value as parameter as like this:
If DEV selected, return "Development http://dev.com 1"
If QA selected, return "QA http://qa.com 2"
If PROD selected, return "Production http://prod.com 3"
3. Once the value is returned in a variable, I will use that variable value in next step of 'Windows batch command'.
Run Code Online (Sandbox Code Playgroud)
在哪里以及如何定义键/值。我尝试使用扩展选择参数插件,但不确定如何执行此操作。
我已经设法通过使用Active Choices Plugin的下拉选择参数获取键/值,它不像这里的其他答案那么复杂,它实际上隐藏在插件页面本身的评论中。
要获取键/值对,请在 Jenkins 中选择下拉列表参数(即显示人类可读的值,但将不同的键传递给构建。在编写 groovy 脚本时,您只需要使用映射而不是列表。映射键如果用户选择此选项,参数将设置为什么。地图值是将在下拉列表中实际显示给用户的内容。
例如脚本:return ['Key1':'Display 1', 'Key2':'Display 2', 'Key3':'Display 3']将向用户显示包含:Display1、Display2 和 Display3 的下拉列表。然而,根据选择的内容,构建参数实际上会设置为 Key1、Key2 或 Key3。
对于这个特定问题,以下是步骤。
return ['Development http://dev.com 1':'DEV', 'QA http://qa.com 2':'QA', 'Production http://prod.com 3':'PROD']对于此示例,用户将看到一个包含 3 个选项的下拉列表:DEV、QA 和 PROD。为该参数传递的值将是Development http://dev.com 1等。现在有一个带有空格和 URL 的参数可能会导致问题,这取决于您稍后在构建中如何使用它,但这个概念确实是我想要说明的。您可以在 groovy 脚本中进行映射。如果您有一个名为 InputParam 的参数,则可以将其映射到 System Groovy 脚本中名为 OutParam 的新参数,如下所示:
import hudson.model.*
def parameterMap=[:]
parameterMap.put('DEV','Development http://dev.com 1')
parameterMap.put('QA','QA http://qa.com 2')
parameterMap.put('PROD','Production http://prod.com 3')
def buildMap = build.getBuildVariables()
def inputValue=buildMap['InputParam']
buildMap['OutParam']=parameterMap[inputValue]
setBuildParameters(buildMap)
def setBuildParameters(map) {
def npl = new ArrayList<StringParameterValue>()
for (e in map) {
npl.add(new StringParameterValue(e.key.toString(), e.value.toString()))
}
def newPa = null
def oldPa = build.getAction(ParametersAction.class)
if (oldPa != null) {
build.actions.remove(oldPa)
newPa = oldPa.createUpdated(npl)
} else {
newPa = new ParametersAction(npl)
}
build.actions.add(newPa)
}
Run Code Online (Sandbox Code Playgroud)
选择执行系统 Groovy 脚本作为第一个构建操作。然后,您可以在 Windows shell 中将输出参数作为环境变量进行访问,例如。
ECHO %OUTPARAM%
| 归档时间: |
|
| 查看次数: |
14264 次 |
| 最近记录: |