-
New Feature
-
Resolution: Fixed
-
Minor
-
-
workflow-support 2.22
It is currently not possible to access the Cause}}s of a build without using the non-whitelisted {{currentBuild.getRawBuild().getCauses() function. An additional drawback to that approach is that is allows direct access to the actual Cause objects, which could potentially allow a malicious pipeline script to manipulate the Cause associated with an upstream build.
This jira and its associated PR propose a different approach based on the use of stapler's ModelBuilder to create a JSON representation of a Cause}}s {{@Exported fields:
For example, a build with a hudson.model.Cause$UserId cause produce the following output:
[{ "_class":"hudson.model.Cause$UserIdCause", "shortDescription":"Started by user anonymous", "userId":"tester", "userName":"anonymous" }]
The JSON objects in the resulting array can be used directly in a pipeline:
assert currentBuild.getBuildCauses().size() == 1 assert currentBuild.getBuildCauses()[0].userId == 'tester' echo currentBuild.getBuildCauses()[0].shortDescription
Additionally, you can filter the result of currentBuild.getBuildCauses() by passing a class name (or superclass name) of the type you would like to filter by. For example, to get a list of build Cause objects that only contains Cause objects of type hudson.model.Cause$UserIdCause, call the method like this:
echo currentBuild.getBuildCauses('hudson.model.Cause$UserIdCause').size()
- duplicates
-
JENKINS-41272 Pipeline "currentBuild" should expose build causes
- Resolved
- is duplicated by
-
JENKINS-55758 Add Whitelisted method to get the trigger cause
- Resolved
- relates to
-
JENKINS-60340 BitBucket payload information is not injected in the Jenkins build
- Open
- links to