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

How can I create parameterized Jenkins job?

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

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

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

使用parameterized Build plugin and NodeLabel Parameter Plugin

  1. 是的,你可以用NodeLabel Parameter Plugin传递一个节点标签参数。

  2. 是的,您可以按照Parameterized Builds中的描述定义参数,然后在您的脚本中将其用作环境变量:

The parameter is available as environment parameters. So e.g. a shell ($FOO, %FOO%) or Ant ( ${env.FOO} ) can access these values.

  1. 这不完全是一个 return 值,但您可以使用 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)

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"

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

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.

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");