trigger

trigger

poll SCM

properties([
  // every 6 hours
  pipelineTriggers([
    pollSCM( ignorePostCommitHooks: true, scmpoll_spec: 'H H/6 * * *' )
  ])
])
properties([
  pipelineTriggers([
    [ $class: "SCMTrigger", scmpoll_spec: 'H/5 * * * *' ],
  ])
])
  • declarative:

    triggers {
      pollSCM ignorePostCommitHooks: true, scmpoll_spec: 'H H * * *'
    }

properties([
  parameters([
    choice(choices: ['', 'a', 'b', 'c'], description: '', name: 'var1')
    choice(choices: ['', 'a', 'b', 'c'], description: '', name: 'var2')
  ]),
  pipelineTriggers([
    parameterizedCron( '''
      H/3 * * * * % var1=a; var2=b
      H/6 * * * * % var1=b; var2=a
    ''' )
  ])
])

triggered by

  • get build cause

    import hudson.model.Cause.UserIdCause
    import org.jenkinsci.plugins.workflow.job.WorkflowRun
    
    workflowRun build = currentBuild.rawBuild
    
    println build.getCauses().collect { it.getClass().getCanonicalName() }
    println build.getCauses().collect { it.getClass().getCanonicalName() }.collect { it.tokenize('.').last() }
    
    println """
      cause                               : ${build.getCauses().toString()}
      cause.getClass()                    : ${build.getCauses().getClass()} : ${build.getCauses().getClass().getCanonicalName()}
      build.getCause( UserIdCause.class ) : ${build.getCause( UserIdCause.class )}
    """
    • console output

      [Pipeline] echo
      [hudson.model.Cause.UserIdCause]
      [Pipeline] echo
      [UserIdCause]
      [Pipeline] echo
      
          cause                               : [hudson.model.Cause$UserIdCause@bf8cb337]
          cause.getClass()                    : class java.util.Collections$UnmodifiableRandomAccessList : java.util.Collections.UnmodifiableRandomAccessList
          build.getCause( UserIdCause.class ) : hudson.model.Cause$UserIdCause@bf8cb337
  • get user id if triggered by manually

    import hudson.model.Cause.UserIdCause
    import org.jenkinsci.plugins.workflow.job.WorkflowRun
    
    workflowRun build = currentBuild.rawBuild
    if ( build.getCause(UserIdCause.class) ) {
      println """
        username : ${build.getCause(hudson.model.Cause.UserIdCause.class).getUserName()}
        id       : ${build.getCause(hudson.model.Cause.UserIdCause.class).getUserId()}
        mail     : ${build.getCause(hudson.model.Cause.UserIdCause.class).getUserId()}@domain.com
      """
    }
    • or

      import hudson.model.Cause.UserIdCause
      
      if ( build.getCause(UserIdCause.class) ) {
        println """
          username : ${build.getCause(UserIdCause.class).getUserName()}
          id       : ${build.getCause(UserIdCause.class).getUserId()}
          mail     : ${build.getCause(UserIdCause.class).getUserId()}@domain.com
        """
      }
  • get causedby

    import org.jenkinsci.plugins.workflow.job.WorkflowRun
    import hudson.model.Cause.*
    import hudson.triggers.TimerTrigger.TimerTriggerCause
    import org.jenkinsci.plugins.workflow.cps.replay.ReplayCause
    import org.jenkinsci.plugins.parameterizedscheduler.ParameterizedTimerTriggerCause
    import org.jenkinsci.plugins.workflow.cps.replay.ReplayCause
    import com.sonyericsson.rebuild.RebuildCause
    
    def getCasuedBy( workflowRun build = currentBuild.rawBuild ) {
        CauseAction causeAction = currentBuild.rawBuild.getAction(CauseAction.class)
        causeAction.getCauses().each { Cause cause ->
          if ( cause instanceof Cause.UpstreamCause            ) println ( 'by upstream'                  )
          if ( cause instanceof Cause.UserIdCause              ) println ( 'by user'                      )
          if ( cause instanceof ReplayCause                    ) println ( 'by reply'                     )
          if ( cause instanceof RebuildCause                   ) println ( 'by rebuild'                   )
          if ( cause instanceof TimerTriggerCause              ) println ( 'by timer'                     )
          if ( cause instanceof ParameterizedTimerTriggerCause ) println ( 'by ParameterizedTimerTrigger' )
        }
    }
  • gitlab

    currentBuild.rawBuild.getCause(com.dabsquared.gitlabjenkins.cause.GitLabWebHookCause).getData()
    // or
    commit = currentBuild.rawBuild.getCause(com.dabsquared.gitlabjenkins.cause.GitLabWebHookCause).getData().getLastCommit()

libs

import hudson.model.Cause.*
import hudson.triggers.TimerTrigger.TimerTriggerCause
import org.jenkinsci.plugins.parameterizedscheduler.ParameterizedTimerTriggerCause
import org.jenkinsci.plugins.workflow.cps.replay.ReplayCause
import com.sonyericsson.rebuild.RebuildCause

Boolean byCron( WorkflowRun build = currentBuild.rawBuild ) {
  build.getCause( TimerTriggerCause.class ) && true
}
Boolean byParameterizedCron( WorkflowRun build = currentBuild.rawBuild ) {
  build.getCause( ParameterizedTimerTriggerCause.class ) && true
}
Boolean byTimer( WorkflowRun build = currentBuild.rawBuild ) {
  byCron( build ) || byParameterizedCron( build )
}
Boolean byUpstream( WorkflowRun build = currentBuild.rawBuild ) {
  build.getCause( Cause.UpstreamCause.class ) && true
}
Boolean byUserId( WorkflowRun build = currentBuild.rawBuild ) {
  build.getCause( Cause.UserIdCause.class ) && true
}
Boolean byReplay( WorkflowRun build = currentBuild.rawBuild ) {
  byUserId( build ) && build.getCause( ReplayCause.class )
}
Boolean byRebuild( WorkflowRun build = currentBuild.rawBuild ) {
  byUserId( build ) && build.getCause( RebuildCause.class )
}

GerritCause

Last updated