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.
Related
I know about the rebuild and replay functionality, but both of them are manually triggers. So here is our problem:
We have multiple servers which can be deployed with any branch that exists. But this deploy is manually. But we want to ensure, that at least once a day, the latest version of that branch is deployed to avoid having servers being outdated.
So what I want to do it, create a scheduler job that runs once a day and triggers a Jenkins job to rebuild the last job using the exact same parameters.
Would be great if someone has some input here :-)
You can try out the Persistent Parameter plugin and use it to define the relevant parameters inside the deploy job that you want to reuse.
This plugin enables you to set you input parameters (string, text, boolean and choice) with default values taken from the previous build - so every time you run the build with parameters manually or trigger it from another job the values that will be used are the values from the last execution.
Your caller job can still pass parameters to the deploy job during the daily execution - but for all parameters that are not passed their latest value will be used.
You can also override parameters defined as persistent as it just affects the default value.
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.
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.
I am using a Pipeline job to trigger several freestyle jobs during its execution, using the native 'build' step.
Later, after all the runs are finalized, I'd like to read info about those runs (name, number, duration, timestamp...) to collect metrics/stats regarding the overall runs.
I'm using the Groovy Event Listener plugin for that task.
Now, I was thinking to read the WorkflowRun object's actions (looking for BuildTriggerAction instances) to get the downstream builds, but they are not available.
run.getActions(BuildTriggerAction.class)
returns an empty list.
I've seen indeed that action is removed every time the triggered build is completed, as described in this ticket https://issues.jenkins-ci.org/browse/JENKINS-28673
My questions are:
do you know how to retrieve the handle to the triggered builds in another way? Not at runtime (I don't have the RunWrapper object, but the WorkflowRun instead). Maybe using the flow objects?
is there another way to fix the issues seen in the ticket, instead of removing the BuildTriggerAction actions?
Thanks in advance!
I am new to Jenkins plugin development. M trying to write a plugin that should be executed before any Multi configuration type job runs in Jenkins.
In this plugin I want to write rules that will check what configuration parameters user has selected while submitting the job, based on selected parameters, I want to decide whether to allow the job to run or to restrict it.
User should be shown reason as to why that job cannot be run in the Console Output.
Does anyone have any ideas which class I need to extend or which interface I need to implement in order to get a hook into Jenkins job run?
You could look at the Matrix Execution Strategy which allows for a groovy script to select which matrix combinations to run. I would think if your script threw an exception it would stop the build.
For background, the multi configuration projects run a control job (or flyweight) which runs the SCM phase then starts all the actual combinations. This plugin runs after the flyweight SCM checkout.
If nothing else, this will give you a working plugin to start from
Disclaimer: I wrote this plugin
Blocked queue job plugin was what I needed
Out of the box that plugin supports two ways to block the jobs -
Based on the result of last run of another project.
Based on result of last run of the current project
In that plugin the BlockQueueItemTaskDispatcher.java extends Jenkin's QueueTaskDispatcher providing us a hook into Jenkins logic to allow or block the jobs present in the queue from running.
I used this plugin as a starting point for developing a new plugin that allows us to restrict project based on parameters selected and the current time. Ultimate goal is to restrict production migrations from running during the day.
Overriding the isBlocked() method of QueueTaskDispatcher gave access to hudson.model.Queue.Item instance as an argument to me. Then I used the Item instance's getParams method to get access to build parameters selected by the user at runtime. Parsed the lifecyle value from it. Checked the current time. If lifecycle was Production and current time was day time then restricted the job by returning non null CauseOfBlockage from isBlocked() method. If that condition was false, then returnedCauseOfBlockage as null allowing the queued job to run.