Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to differentiate build triggers in Jenkins Pipeline

I'm hoping to add a conditional stage to my Jenkinsfile that runs depending on how the build was triggered. Currently we are set up such that builds are either triggered by:

  1. changes to our git repo that are picked up on branch indexing
  2. a user manually triggering the build using the 'build now' button in the UI.

Is there any way to run different pipeline steps depending on which of these actions triggered the build?

like image 781
old_qwerty_bastard Avatar asked Apr 24 '17 21:04

old_qwerty_bastard


People also ask

What are the different ways the build can be triggered?

Builds can be triggered by repository events, such as source code changes; triggered on a schedule; or triggered by post-processing events, such as completing builds in related projects.

How do you know who triggered Jenkins job?

I wanted to show the user who triggered a Jenkins job in the post job email. This is possible by using the plugin Build User Vars Plugin and the env variable BUILD_USER .


2 Answers

The following code should works to determine if a user has started the pipeline or a timer/other trigger:

def isStartedByUser = currentBuild.rawBuild.getCause(hudson.model.Cause$UserIdCause) != null 
like image 149
Philip Avatar answered Sep 24 '22 19:09

Philip


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

And an example from me submitting:

echo "${currentBuild.buildCauses}" // same as currentBuild.getBuildCauses() echo "${currentBuild.getBuildCauses('hudson.model.Cause$UserCause')}" echo "${currentBuild.getBuildCauses('hudson.triggers.TimerTrigger$TimerTriggerCause')}" 

And the output:

[Pipeline] echo [[_class:hudson.model.Cause$UserIdCause, shortDescription:Started by user anonymous, userId:null, userName:anonymous], [_class:org.jenkinsci.plugins.workflow.cps.replay.ReplayCause, shortDescription:Replayed #12]] [Pipeline] echo [] [Pipeline] echo [] [Pipeline] End of Pipeline Finished: SUCCESS 

NOTE

There appears to be an issue with the currentBuild.getBuildCauses(type) when the type is a type of Cause contributed by a plugin. For example, currentBuild.getBuildCauses('org.jenkinsci.plugins.workflow.cps.replay.ReplayCause') fails with a java.lang.ClassNotFoundException. This was reported in JENKINS-54673 for the 2.22 version of the Pipeline: Supporting APIs (workflow-support) plugin. It is reportedly fixed in the 2.24 version.

like image 23
mkobit Avatar answered Sep 24 '22 19:09

mkobit