如何创建参数化的Jenkins作业?

问题描述 投票:2回答:3

我想在不同的机器上使用相同的工作。但我不想每次都改变工作的配置。我可以将机器名称标签作为参数传递并在不同的机器上运行作业吗? (不是同时)。

我想在运行作业时将参数传递给我在配置中编写的脚本(批处理脚本)。我们可以这样做吗?

我可以从工作中获得返回值并在下一份工作中使用它吗?

jenkins jenkins-plugins jenkins-cli jenkins-scriptler
3个回答
6
投票
  1. 是的,您可以使用NodeLabel Parameter Plugin传递节点标签参数。
  2. 是的,您可以在Parameterized Builds中定义参数,然后在脚本中将其用作环境变量:

该参数可用作环境参数。所以例如shell($ FOO,%FOO%)或Ant($ {env.FOO})可以访问这些值。

  1. 这不是一个返回值,但您可以使用Parameterized Trigger Plugin将任何参数(及其值)传递给下游作业。

2
投票
Example criteria:
User needs to use different environments for executions.

1. Select jenkins project
2. Job -> Configure -> Select this project is parameterized check box.
3. Select Add parameter drop down - > String parameter
4. Define a string parameter (In my example I use environment variable as : BaseURL)

enter image description here

5.Now under build section initialize the mvn command user needs to execute.Here in this case I want to execute my jenkins build on a environment where I specify. So I should be able to execute my build in different environment each time with out changing the code.

My mvn command: I pass my environment url as a parameter (Using $ sign). This is based on user requirement. 
clean test -Dcustomproperty="$BaseURL"

enter image description here

6. Apply and save your Jenkins project.
7. Now your project is parameterized.  Then your project should have Build with parameters option.

enter image description here

8.Click Build with parameters link and enter your parameter (In this example my environment variable) and click build. Your jenkins job should run with the parameter.

enter image description here

NOTE: This build won't succeed unless the passing parameter is not utilized in the automation script. So script has to be modified to retrieve the passing variable.

String BaseURL= System.getProperty("customproperty");

© www.soinside.com 2019 - 2024. All rights reserved.