Jenkins - Using build parameters passed during Trigger of a Job - jenkins

I have a pipeline in Jenkins, where one project sends parameter to the next one. The trigger is set during the Build step and is the last step in the Build process. I have checked the "For each property file, invoke a build" button, where the properties files to be read are in the work space of the current project.
On build, I can see that the Downstream project following the current one, is given the properties of the property file. But when I try accessing them, it just takes the name of the variable and not the value. This means that I am not able to access the parameters in the property file. Can you tell me how will I be able to access the parameters that were passed during trigger from the property file? I have tried all ways, but I can't seem to access them.
Image of Parametrized Trigger--

I solved the issue: in order to use the parameters sent during the trigger, we need to declare them in the triggered project. They should be kept empty. Once we do that, they will take the values sent across by the previous project.

Related

Can there be a few build causes in Jenkins?

The currentBuild global variable in Jenkins has a method of getBuildCauses(). The Reference says this:
getBuildCauses
Returns a JSON array of build causes for the current build
I was wondering, why does this method returns an array? Is there any possibility that a build was caused by several causes?
There are cases where a job was triggered by another job that was e.g. started manually by a user. So this information about a user who triggered the parent job is made available in the array. In another case, this job may be rebuilt manually by a user from a previous run that was triggered by a webhook. Sometimes, people want to know the whole history of the job instance, and so it's made available in this array.

How to set a persistent global variable by scripted pipeline?

I need to pass a variable from a pipeline to another one without triggering the build of the second one.
Basically, the first pipeline should set the value of this variable and terminate. When I run the second pipeline, it should be able to get this value.
So far, I used a properties' file but I'd like to know if there is a way to avoid it. Is it possible?
I think the EnvInject plugin is what you need.
As alternative solution:
You could also write to a file from the first job and source that file from your second job if it is started. The location must be persistent, do not use the workspace dir.

Jenkins: How can I know if an automatic process or a user has triggered a build?

Is there an environment variable in Jenkins which tells me if the build has been ran manually or automatically triggered by polling?
My pipeline works like a charm if automatically triggered, but if manually ran... it always fails, so I think I'm going to edit the pipeline to check how the build has been triggered.
Unfortunately variable env.BUILD_CAUSE is not set in Pipeline builds.
For pipeline jobs see following example
if ( currentBuild.rawBuild.getCauses()[0].toString().contains('UserIdCause') ){
// do steps for manual trigger here
}
Other possible causes to compare can be found here.
The ability to get causes for a workflow run was released in version 2.22 (2018 Nov 02) to the Pipeline Supporting APIs Plugin. The feature was requested in JENKINS-41272.
A couple methods were added to the currentBuild global variable with that release:
getBuildCauses
Returns a JSON array of build causes for the current build
EXPERIMENTAL - MAY CHANGE getBuildCauses(String causeClass)
Takes a string representing the fully qualified Cause class and returns a JSON array of build causes filtered by that type for the current build, or an empty JSON array if no causes of the specified type apply to the current build
See answer https://stackoverflow.com/a/53342374/5955565.
I have copy-pasted it here because this question is shown first in search results (unlike How to differentiate build triggers in Jenkins Pipeline).
See also ${YOUR_JENKINS_URL}/pipeline-syntax/globals for a complete, and up to date, list of properties available on currentBuild.

Not able to access custom parameters passed to the jenkins build script

I want to add some parameters from the web hooks into the mail sent from the Jenkins. I tried solutions provided in StackOverflow and elsewhere. No success yet.
I have the following in place:
Checked This is a parameterized build
The build script looks like http://JENKINS_URL/job/android02/buildWithParameters?token=<My Token>&PARAM=<My Custom Params>
In the mail content, I try to access the custom parameters by ${PARAM}, $PARAM.
In the mail, however, I'm not getting the values I'm setting. If I set default values for PARAM, it is correctly displayed in the mail I receive. I tried http://JENKINS_URL/job/android02/build?token=<My Token>&PARAM=<My Custom Params> URL as well thinking just in case if it works. The mail is sent as Editable email Notification config.
Basically, everything is working except that I'm not able to access the custom parameters I have passed via Trigger builds remotely option.
Edit1:
If I keep a default value, say, test for the parameter, PARAM, in the mail received, I can see test displayed. But I need to get the value I'm passing in the build script.
From the "tool tip" that you get when you click on the question mark next to "":
${ENV,var="VARIABLENAME"}
Expands to an environment variable (specified here as VARIABLENAME) from the build environment. Note that this does not include any variables set by the build scripts themselves, only those set by Jenkins and other plugins.
So, use ${ENV,var="PARAM"}.

How to run parametrized build after commit to repository?

I have parametrized build job in Jenkins. It has configured SCM polling and the build job is started after new commit.
Parameters for this build job are location profiles defined in main pom.xml. Count of these profiles is static and persistent. So after every commit I need to build a project for the same profiles. One profile is started for one build.
It is able for manual triggering when I write profile name and start the build job. But after new commit this build job is started without parameters. So is there any way how to define list of parameters for build job - one parameter per one build.
An SCM change will trigger a related Jenkins job. Once. That's it.
When that job is triggered, and is configured with parameters, it does have default parameters.
For string parameters, it's the default value entered in configuration page (if you haven't entered one, the default is just that: none).
For single choice-style parameters, it's the top-most value.
For multi choice-style parameters, again, unless a default is provided in configuration, it's nothing.
If what you want is to trigger multiple runs of the same build for the same SCM change, then you've configured your jobs wrong.
Either create a matrix job, and configure an axis for every "profile" as you call it.
Or create multiple jobs, and chain them, so that first is triggered by SCM change, and the rest are triggered in sequence
If you only want to specify one default string that should be picked up while building using Poll SCM feature, then you should try using the following method:
Select the following options in Extended Choice Parameter:
In Simple Parameter Types section, go for Single Select in Parameter Type
Instead of Choose Source for Value, go for Choose Source for Default Value. Now enable the radio-button named Default Value. Enter whatever string you want to specify. Build will pick-up the given string as the default one.
Hopefully, it should work. At least, it works when i use Build periodically option. :)

Resources