31

Is there a way to trigger a Jenkins job to run every hour using the Jenkinsfile scripted pipeline syntax?

I have seen examples using the declarative syntax, but none using the pipeline syntax.

Declarative Syntax Example

pipeline {
    agent any

    triggers {
        cron '@daily'
    }

   ...
}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Alex
  • 5,364
  • 9
  • 54
  • 69
  • 2
    Possible duplicate of [Jenkins Build Pipeline Scheduled Trigger](https://stackoverflow.com/questions/32028761/jenkins-build-pipeline-scheduled-trigger) – StephenKing May 22 '17 at 13:18

2 Answers2

43

You could use this snippet for Scripted pipeline syntax:

properties(
    [
        ...  , // other properties that you have
        pipelineTriggers([cron('0 * * * *')]),
    ]
)

Reference for properties is here. You can search for "pipelineTriggers" string and find out that triggers for build can be for example artifactory or something else from this list (extracted 2019-03-23 from linked doc page):

$class: 'ArtifactoryTrigger'
$class: 'AssemblaBuildTrigger'
bitBucketTrigger
bitbucketPush
$class: 'BuildResultTrigger'
$class: 'CIBuildTrigger'
$class: 'CodingPushTrigger'
$class: 'CronFolderTrigger'
$class: 'DeployDbTrigger'
$class: 'DockerHubTrigger'
$class: 'DosTrigger'
$class: 'ElOyente'
$class: 'FanInReverseBuildTrigger'
$class: 'FeatureBranchAwareTrigger'
$class: 'FilesFoundTrigger'
$class: 'FogbugzStatePoller'
$class: 'FolderContentTrigger'
GenericTrigger
gerrit
$class: 'GhprbTrigger'
$class: 'GitBucketPushTrigger'
githubBranches
githubPullRequests
githubPush
gitee
$class: 'GogsTrigger'
issueCommentTrigger
$class: 'IvyTrigger'
$class: 'JiraChangelogTrigger'
$class: 'JiraCommentTrigger'
$class: 'KanboardQueryTrigger'
$class: 'MailCommandTrigger'
$class: 'MavenDependencyUpdateTrigger'
$class: 'NugetTrigger'
p4Trigger
$class: 'PeriodicFolderTrigger'
$class: 'PollMailboxTrigger'
$class: 'PullRequestBuildTrigger'
$class: 'QuayIoTrigger'
$class: 'RemoteBuildTrigger'
upstream
$class: 'RundeckTrigger'
<code>scm</code>
$class: 'SelfieTrigger'
$class: 'SpoonTrigger'
$class: 'SqsBuildTrigger'
$class: 'TeamPRPushTrigger'
$class: 'TeamPushTrigger'
cron
$class: 'URLTrigger'
snapshotDependencies
$class: 'io.relution.jenkins.awssqs.SQSTrigger'
$class: 'io.relution.jenkins.scmsqs.SQSTrigger'
$class: 'org.cloudbees.literate.jenkins.promotions.PromotionTrigger'
$class: 'org.jenkinsci.plugins.deploy.weblogic.trigger.DeploymentTrigger'
$class: 'org.jenkinsci.plugins.deployment.DeploymentTrigger'

More info about scripted way here (sample from another question). Documentation that covers declarative pipeline is here.

Lubo
  • 1,621
  • 14
  • 33
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • 5
    It does not work in scripted pipelines under jenkins 2.79 (java.lang.UnsupportedOperationException: Undefined symbol ‘pipelineTriggers’) – gileri Sep 22 '17 at 15:13
  • For anyone in the future that had the same issue that I had, where its failing syntax-wise, I needed some statement after the `@Library` in my Jenkinsfile. Since I was using the idea from @Clément MATHIEU of `string cron_string = BRANCH_NAME == "master" ? "@hourly" : ""` it works for me. – CeePlusPlus May 21 '20 at 23:59
-18

The correct version is in a Jenkinsfile "Declarative Pipeline":

pipeline {
    agent any
    triggers {
        cron('H */4 * * 1-5')
    }
...
}
xXxLMxXx
  • 7
  • 3
  • 12
    The question is specifically about scripted pipeline, not the declarative one. – cji Mar 22 '18 at 09:54