Find What Triggered/Caused a Job to Build

Issue

Jobs are being triggered on your instance and you are not sure why they are being built, or you would like to see if jobs are being automatically triggered or manually triggered by a certain user.

Environment

Resolution

Within CloudBees Jenkins the cause of any Build for a particular job should be listed in the UI by navigating to said job and selecting the Build number in question from the left hand column.

For example the UI on the build page will show: Branch indexing, Started by user $USER.

However, if you would like to review all available builds on your instance in one aggregated output it’s possible using the below Groovy Script:

import hudson.model.Fingerprint.RangeSet
import hudson.model.Fingerprint.Range

Jenkins.instance.getAllItems(Job).each {
  it.getBuilds(new RangeSet(new Range(0,2))).each { build ->
    build.getCauses().each {
      println "[${ it instanceof Cause.UserIdCause ? cause.getUserId() : 'AUTOMATION'}] ${build}"
    }
  }
}
return

With this script any build triggered from automation such as polling/webhooks would begin with the tag: [AUTOMATION] otherwise if triggered by a user each job input will begin with [$USER]

Have more questions?

0 Comments

Please sign in to leave a comment.