How can I create parameterized Jenkins job? - jenkins

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?

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

Yes, you can pass a node label parameter with NodeLabel Parameter Plugin.
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.
This is not exactly a return value, but you can pass any parameter (with its value) to the downstream job with Parameterized Trigger Plugin.

Use parameterized Build plugin and NodeLabel Parameter Plugin

Related

How to set a variable inside Jenkins Shell Command

I have a Jenkins build job. It has section for shell command where I read "version" of the current application that i am building.
Now, i want to set the Jenkins custom variable "VERSION" with the "version" in the same shell command section.
I need to pass this value as parameter to some other job being triggered after successful build of this job.
Now, I would pass the Jenkins variable VERSION to some other Job.
Please suggest how can I do this.
Use EnvInject Plugin to inject runtime variables into Jenkins build process so that you can use in other build steps or you can pass it on to other Job's as input parameter.
I could get some clue from Suresh's answer. I did the following steps and it worked well.
Step 1: Install Plugin “EnvInject Plugin”.
Step 2: Set the custom env variable and store that in env.properties
Step 3: Add POST Build Action, “Trigger parameterized build on other projects”
Step 4: Add the variables that the Post Build Job is expecting:
Here the BUILD_NUMBER and FILE_VERSION are two parameterized variables
that the next Job is expecting. FILE_VERSION is passed as parameters
from properties file which was stored in STEP 2.

How to use the output of shell script in jenkins

I am trying to execute the following shell command
timeStamp=$(date +%s)
echo "###### TIMESTAMP IS #$timeStamp #######"
I wanted to pass this variable timeStamp to my second job that I am trying to trigger using "Trigger parameterized build on other projects"
timeStamp=${timeStamp}
I am not getting the value of timestamp in the variable timeStamp.
I have restrictions that EnvInjectPlugin cannot be used. Is there any other option available to pass the shell script output to another parameter.
Thanks,
Ramya.
I suggest you use the EnvInjectPlugin plugin. But since you have restrictions that EnvInjectPlugin cannot be used, you can choose to use property file and here is how.
Step 1:
Step 2:
Add Post-build action ==> Trigger parameterized build on other projects ==> Add Parameters ==> Parameters from preperties file
After that, you can use TIMESTAMP parameter in the downstreamJob.

Jenkins Promoted Build Parameter Value

I have a jenkins job that runs some tests and promotes the build if all tests pass.
I then have a second job that has a 'Promoted build parameter' which is used for deployments.
THe idea is that the deply job should let you pick one of the prompted builds for deploying. The issue I'm having is that I can pick a promoted build, but I have not idea how I access information about the build.
I've named the build parameter
SELECTED_BUILD
And according to the docs this should then be available via the environment.
The problem is that it doesn't seem to be bound to anything.
If I run a build step to exectute this sheel script:
echo $SELECTED_BUILD
echo ${SELECTED_BUILD}
The values are not interpolated / set.
Any idea how I can access the values of this param?
Many Thanks,
Vackar
Inject the information as variable user jenkins plugin (eject evn variable).
while triggering parameterized build on other job, pass above variable as parameter to next job.

How to change the configuration's by using the parametrized build based on the parameter?

choice parameter
Name
a
b
c
I have a parametrized build with choice parameters like a,b,c,d. When I select a parameter, it has to do fresh checkout, and, when I select b it has to update the work space of Jenkins.
Right now whatever the parameter choose either a,b,c, it is checkout policy is fresh checkout only.
Can any one let me know how to set different properties based on the selected parameter.
You would have to forego the Jenkins SCM checkout configuration, and maintain the SCM checkout through a script (either Execute Shell or Execute Batch Command). Your script would then handle the logic of doing a certain type of checkout/update based on passed parameters
Late edit:
You could configure a Pre-scm Build Step, and there run either a Conditional Build Step or a plain shell execution (bash or batch). In that shell, test the param, and if it matched b, then wipe the local workspace/checkout folder from the shell script.
When the rest of the job runs, it will do a fresh checkout (since the workspace/checkout is empty). With other param options, it will run the job normally, doing an update.
I haven't tried this. Your biggest issue may be if pre-scm build step does not have access to environment variables at that time.

Updating jenkins job variable

Parameterized variable are not getting updated in jenkins
I m using the conditional build-step plugin to update the jenkins job parameter by executing shell script its showing me the new value of variable as well but its not get reflected.
You can try the EnvInject Plugin. One of the features is a build step that allows you to "inject" parameters into the build job from a settings file.
Create a property for the email list in the env.properties file:
echo "variable=`value`"> env.properties
It will create the properties file in the job workspace directory.
env.properties
In shell script:
"$variable"
If I understand correctly, you are trying to change the value of a pre-defined parameter
from within a script that is run by the job.
This will not work, because of "scope" (or "call-stack"),
as a process (your script) cannot change the environment of a parent process (your Jenkins job).

Resources