简体   繁体   English

Jenkins 参数化构建以使用键/值对

[英]Jenkins Parameterized build to use key/value pairs

I have a Jenkins parameterized build.我有一个 Jenkins 参数化构建。 I tick the "this build is parameterized" and I set a "Choice" environment name to be "ENVIRONMENT" and then as choices I define human readable names such as "Test env1", "Test env2", etc. However I want these keys to actually contain different values, for example "Test env1" key would container a file path as its value.我勾选“此构建已参数化”,并将“选择”环境名称设置为“环境”,然后作为选择,我定义了人类可读的名称,例如“测试 env1”、“测试 env2”等。但是我想要这些实际包含不同值的键,例如“Test env1”键将包含一个文件路径作为其值。 How can this be done?如何才能做到这一点?

I have managed to get the keys/values with a dropdown select parameter working with the Active Choices Plugin , the answer was actually buried in the comments on the plugin page itself.我设法通过使用Active Choices Plugin的下拉选择参数获取键/值,答案实际上隐藏在插件页面本身的评论中。

You simply need to use a map rather than a list when writing your groovy script to define the dropdown options.在编写 groovy 脚本来定义下拉选项时,您只需要使用地图而不是列表。 The map key is what the parameter will be set to if the user selects this option.如果用户选择此选项,则映射键是参数将设置的内容。 The map value is what will be actually displayed to the user (ie something human readable) in the dropdown list.地图值是将在下拉列表中实际显示给用户的内容(即人类可读的内容)。

Here are the steps.以下是步骤。

  1. Ensure you have the Active Choices Plugin installed.确保您已安装Active Choices 插件
  2. Open the configuration of your Jenkins job, select This project is parameterised .打开 Jenkins 作业的配置,选择This project is parameterised
  3. Click Add Parameter and select Active Choices Parameter .单击添加参数并选择活动选择参数
  4. Name your parameter ENVIRONMENT and click the Groovy Script check box.将参数命名为ENVIRONMENT并单击Groovy Script复选框。
  5. In Groovy Script enter content: return ['env1 file path value':'Test env1', 'env2 file path value':'Test env2'] For this example the user will see a dropdown with 2 options: Test env1 and Test env2 .Groovy 脚本中输入内容: return ['env1 file path value':'Test env1', 'env2 file path value':'Test env2']对于这个例子,用户将看到一个下拉菜单,有 2 个选项: Test env1Test env2 . The keys: env1 file path value and env2 file path value are what the Jenkins build parameter will be set to if the option is selected.密钥: env1 文件路径值env2 文件路径值是 Jenkins 构建参数将在选择该选项时设置的内容。 Modify these as necessary.根据需要修改这些。

In this case ENVIRONMENT is the key and "Test env1", "Test env2", etc. are the possible values.在这种情况下, ENVIRONMENT是键, "Test env1", "Test env2", etc.是可能的值。 Choice parameter is to restrict the possible inputs.选择参数是为了限制可能的输入。

Based on the value of %ENVIRONMENT% you can execute multiple pathways in your batch scripts or whichever scripts you are executing根据%ENVIRONMENT%的值,您可以在批处理脚本或正在执行的任何脚本中执行多个路径

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM