Search code examples
jenkinsjenkins-pipeline

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?


Solution

  • 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