How can I create parameterized Jenkins job?

35,445

Solution 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.

Solution 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");
Share:
35,445
Sreevalsa E
Author by

Sreevalsa E

...

Updated on July 09, 2022

Comments

  • Sreevalsa E
    Sreevalsa E almost 2 years

    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?

  • Sreevalsa E
    Sreevalsa E almost 9 years
    Can't I use windows batch script and pass parameters ? It talks about shell script and ant
  • Sreevalsa E
    Sreevalsa E almost 9 years
    Please help me with instruction to us parameter to a windows batch script .
  • Sreevalsa E
    Sreevalsa E almost 9 years
    I got it!! Using %param% in the script!! :)