"Build Periodically" with a Multi-branch Pipeline in Jenkins

JenkinsJenkins PipelineMultibranch Pipeline

Jenkins Problem Overview


I'm running Jenkins 2 with the Pipeline plugin. I have setup a Multi-branch Pipeline project where each branch (master, develop, etc.) has a Jenkinsfile in the root. Setting this up was simple. However, I'm at a loss for how to have each branch run periodically (not the branch indexing), even when the code does not change. What do I need to put in my Jenkinsfile to enable periodic builds?

Jenkins Solutions


Solution 1 - Jenkins

If you use a declarative style Pipeline and only want to trigger the build on a specific branch you can do something like this:

String cron_string = BRANCH_NAME == "master" ? "@hourly" : ""

pipeline {
  agent none
  triggers { cron(cron_string) }
  stages {
    // do something
  }
}

Found on Jenkins Jira

Solution 2 - Jenkins

If you are using a declarative style Jenkinsfile then you use the triggers directive.

pipeline {
    agent any
    triggers {
        cron('H 4/* 0 0 1-5')
    }
    stages {
        stage('Example') {
            steps {
                echo 'Hello World'
            }
        }
    }
}

Solution 3 - Jenkins

I was able to find an example illustrating this an discarding old builds, which is also something I wanted.

Jenkinsfile in jenkins-infra/jenkins.io:

properties(
    [
        [
            $class: 'BuildDiscarderProperty',
            strategy: [$class: 'LogRotator', numToKeepStr: '10']
        ],
        pipelineTriggers([cron('H/30 * * * *')]),
    ]
)

Solution 4 - Jenkins

This is working for me:

  triggers {
    cron(env.BRANCH_NAME == 'development' ? 'H */12 * * *' : '')
  }

See more in this article

Solution 5 - Jenkins

For Paramertized periodic runs or scheduled triggers, one could use as follows.

triggers{
    parameterizedCron env.BRANCH_NAME == "develop" ? '''H 03 * * * % buildSlave=vm1;testSlave=vm2;HYPERVISOR=vbox;VERSION=10.5.0.0
H 03 * * * % buildSlave=vm1;testSlave=vm2;HYPERVISOR=workstation;VERSION=10.5.0.0''' : ""
}

Solution 6 - Jenkins

I hit issues with the above solutions.
I'm not a Jenkins wizard so not sure if I am using an old format/syntax or something, but the following is working for me.

#!/usr/bin/env groovy
properties(
    [
        pipelineTriggers([
                [
                    $class: 'TimerTrigger',
                    spec: 'H 7,19 * * *'
                ]
         ])
    ]
)

Determined from: https://github.com/jenkinsci/jenkins/blob/master/core/src/main/java/hudson/triggers/TimerTrigger.java

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiongeowa4View Question on Stackoverflow
Solution 1 - JenkinsJulian VeerkampView Answer on Stackoverflow
Solution 2 - Jenkinsteeks99View Answer on Stackoverflow
Solution 3 - Jenkinsgeowa4View Answer on Stackoverflow
Solution 4 - JenkinsPaweł IwaneczkoView Answer on Stackoverflow
Solution 5 - JenkinsmerlachandraView Answer on Stackoverflow
Solution 6 - JenkinsAdamView Answer on Stackoverflow