How do you find out what triggered a build?
IntermediateConceptScripted pipeline, Groovy & CPS
Answer
script {
def userCause = currentBuild.getBuildCauses('hudson.model.Cause$UserIdCause')
if (userCause) { echo "Started by ${userCause[0].userId}" }
def timer = currentBuild.getBuildCauses('hudson.triggers.TimerTrigger$TimerTriggerCause')
}
In declarative, when { triggeredBy 'UserIdCause' } or triggeredBy cause: 'UserIdCause', detail: 'alice' handles the common cases without code.
More on Scripted pipeline, Groovy & CPS
What does a scripted pipeline look like and when would you still write one?IntermediateWhat is CPS and why does Jenkins transform pipeline Groovy?AdvancedWhy does `java.io.NotSerializableException` happen in pipelines?AdvancedWhat does `@NonCPS` do and what are its rules?AdvancedWhy do some Groovy closures behave unexpectedly in pipelines, for example `list.each { }` or `sort`?AdvancedWhat is the `script` block in declarative, and what are its limits?Beginner