Jenkins: How can I know if an automatic process or a user has triggered a build?

The ability to get causes for a workflow run was released in version 2.22 (2018 Nov 02) to the Pipeline Supporting APIs Plugin. The feature was requested in JENKINS-41272.

A couple methods were added to the currentBuild global variable with that release:

getBuildCauses

    Returns a JSON array of build causes for the current build

EXPERIMENTAL - MAY CHANGE getBuildCauses(String causeClass)

    Takes a string representing the fully qualified Cause class and returns a JSON array of build causes filtered by that type for the current build, or an empty JSON array if no causes of the specified type apply to the current build

See answer https://stackoverflow.com/a/53342374/5955565. I have copy-pasted it here because this question is shown first in search results (unlike How to differentiate build triggers in Jenkins Pipeline).

See also ${YOUR_JENKINS_URL}/pipeline-syntax/globals for a complete, and up to date, list of properties available on currentBuild.


Unfortunately variable env.BUILD_CAUSE is not set in Pipeline builds. For pipeline jobs see following example

if ( currentBuild.rawBuild.getCauses()[0].toString().contains('UserIdCause') ){
    // do steps for manual trigger here
}

Other possible causes to compare can be found here.