Search code examples
jenkinsjenkins-pipelinecron-task

Trigger hourly build from scripted Jenkinsfile


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'
    }

   ...
}

Solution

  • 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.