|
1 |
| -// There is no direct access to the build Causes from the Pipeline, but you can |
2 |
| -// get this by using the `currentBuild.rawBuild` variable, as shown below. |
| 1 | +// As of Pipeline Supporting APIs v2.22, there is a whitelisted API to access |
| 2 | +// build causes as JSON that is available inside of the Pipeline Sandbox. |
3 | 3 |
|
4 | 4 | // Get all Causes for the current build
|
5 |
| -def causes = currentBuild.rawBuild.getCauses() |
| 5 | +def causes = currentBuild.getBuildCauses() |
6 | 6 |
|
7 | 7 | // Get a specific Cause type (in this case the user who kicked off the build),
|
8 | 8 | // if present.
|
9 |
| -def specificCause = currentBuild.rawBuild.getCause(hudson.model.Cause$UserIdCause) |
| 9 | +def specificCause = currentBuild.getBuildCauses('hudson.model.Cause$UserIdCause') |
10 | 10 |
|
11 |
| -// If you see errors regarding 'Scripts not permitted to use method...' approve |
12 |
| -// these scripts at JENKINS_URL/scriptApproval/ - the UI shows the blocked methods |
13 |
| - |
14 |
| -// See the Javadoc for Cause for more information on what's in Causes, etc at: |
15 |
| -// http://javadoc.jenkins-ci.org/hudson/model/class-use/Cause.html |
| 11 | +// The JSON data is created by calling methods annotated with `@Exported` for |
| 12 | +// each Cause type. See the Javadoc for specific Cause types to check exactly |
| 13 | +// what data will be available. |
| 14 | +// For example, for a build triggered manually by a specific user, the resulting |
| 15 | +// JSON would be something like the following: |
| 16 | +// |
| 17 | +// [ |
| 18 | +// { |
| 19 | +// "_class\": "hudson.model.Cause$UserIdCause", |
| 20 | +// "shortDescription": "Started by user anonymous", |
| 21 | +// "userId": "tester", |
| 22 | +// "userName": "anonymous" |
| 23 | +// } |
| 24 | +// ] |
| 25 | +// cf. https://javadoc.jenkins-ci.org/hudson/model/Cause.UserIdCause.html |
0 commit comments