How can I create parameterized Jenkins job?

五迷三道 提交于 2019-11-28 01:27:51

问题


I want to use same job in different machine. But I don't want to change the configuration of the job each time. Can I pass the machine name label as parameter and run the job in different machine ? (Not simultaneously).

I want to pass parameters while running a job to the script which I have written in th configuration (batch script). Can we do that ?

Can I get a return value from a job and use it in next job?


回答1:


  1. Yes, you can pass a node label parameter with NodeLabel Parameter Plugin.

  2. Yes, you can define parameters, as described, in Parameterized Builds and then use it in your script as an environment variable:

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

  1. This is not exactly a return value, but you can pass any parameter (with its value) to the downstream job with 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");



回答3:


Use parameterized Build plugin and NodeLabel Parameter Plugin



来源:https://stackoverflow.com/questions/31832419/how-can-i-create-parameterized-jenkins-job

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!