Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create parameterized Jenkins job?

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?

like image 727
Sreevalsa E Avatar asked Aug 05 '15 12:08

Sreevalsa E


People also ask

What Jenkins option we used to parameterize our Jenkins build?

To execute the Jenkins job from Jenkins web interface first go to respective project workspace and then click on “Build with Parameters” option from left panel. After that, you will be asked to choose/set parameter value (In my project I have set BROWSER parameter.)

What is parameterized job?

Hey @Hannah, Parameterized jobs/builds are the ones that take in parameters to execute their builds. This is the easiest way to make your build flexible. Let me explain the basic steps: Create a freestyle project.

How many types of parameters are available in Jenkins job?

There are three types of active choice parameters.

How can the parameters be defined in Jenkins?

To define parameters for your job, select the This project is parameterized check box. The Add Parameter drop-down is enabled with the list of parameter types. Use the drop-down button to add as many parameters as you need. There are different parameter types available.


2 Answers

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");
like image 115
Neyomal Avatar answered Oct 01 '22 02:10

Neyomal


  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.
like image 20
Vitalii Elenhaupt Avatar answered Oct 01 '22 01:10

Vitalii Elenhaupt