You can check the Run instance for a TimerTriggerCause. See: 
https://javadoc.jenkins-ci.org/hudson/model/Run.html#getCause-java.lang.Class-

Since this is in pipeline, you would need to access the Run within a 
@NonCPS method. Also, you would need to whitelist the RunWrapper#getRawBuild 
<https://javadoc.jenkins.io/plugin/workflow-support/org/jenkinsci/plugins/workflow/support/steps/build/RunWrapper.html#getRawBuild-->
 method.

import hudson.triggers.TimerTrigger.TimerTriggerCause
import org.jenkinsci.plugins.workflow.support.steps.build.RunWrapper

@NonCPS
boolean wasTriggeredByTimer(RunWrapper runWrapper) {
  return null != runWrapper.getRawBuild().getCause(TimerTriggerCause.class)
}

// used within a pipeline script
if (wasTriggeredByTimer(currentBuild)) {
    echo("This build was triggered by a timer.")
}


Hope that helps!


On Tuesday, 14 August 2018 02:07:43 UTC-7, Markus "Shorty" Uckelmann wrote:
>
> Hi all, 
>
> I use the following code to trigger a job on a daily basis: 
>
> properties([[$class: 'BuildDiscarderProperty', 
>                  strategy: [$class: 'LogRotator', numToKeepStr: '10']], 
>              pipelineTriggers([cron( env.BRANCH_NAME == 'master' ? 
> '@hourly' : '' )]), 
>                  ]) 
>
> Now I'd like to know, if the job was triggered by the cron trigger. Does 
> anybody know how this can be done? 
>
> I already tried this peace: 
>
> if (manager.logContains("Started by timer")){ 
>    echo "This build was triggered by a timer." 
> } 
>
> But it gave me the error: groovy.lang.MissingPropertyException: No such 
> property: manager for class: groovy.lang.Binding 
>
>
> Cheers, Shorty 
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jenkinsci-users/70a6f039-b21b-4d5f-a6b9-6b67fca05ea0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to