Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-54227

Safely expose the Cause(s) associated with the current build

XMLWordPrintable

    • 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()
      

            mikecirioli mike cirioli
            mikecirioli mike cirioli
            Votes:
            1 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: