"Build Periodically" with a Multi-branch Pipeline in Jenkins
Asked Answered
N

6

117

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?

Nosh answered 26/8, 2016 at 14:38 Comment(0)
W
116

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

Waggon answered 4/7, 2017 at 9:50 Comment(9)
This works, but note that committing and pushing the Jenkinsfile is not enough for the trigger to be picked up; the job has to be run once manually afterwards as well.Riband
Can the trigger be inside of a stage block? I.e. stage('Deploy to production') { triggers { cron(MASTER_TRIGGER) }Crosseye
Will this work in a declarative jenkins file or only a scripted jenkins file?Crosseye
Should work with a declarative Jenkinsfile. The trigger only works inside the options tag iirc. If you want to only deploy the master branch you can put a script{ if (BRANCH_NAME == „Master“) { //deploy}} inside the deploy block. (I’m currently on mobile so the syntax might be wrong)Waggon
@Crosseye "if you use a declarative style Pipeline"Inulin
@Riband you can also add another trigger, such as pollSCM or a webhook.Inulin
Since oct 2020, new git repos use "main" instead of "master" -> Hopefully this comment will help someone else who copy pasted from this answer didn't get their periodically triggered builds to work.Kielty
so - multi branch is a different style of pipeline than declarative, if i understand correctly. this is an answer to a different question if that is the case.Spontoon
a multi-branch pipeline can be either declarative or scripted. This answer refers to a declarative multi-branch pipeline. See the Jenkins Docs: Pipeline for a comparison.Waggon
E
54

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'
            }
        }
    }
}
Enjoin answered 25/4, 2017 at 17:28 Comment(2)
Any way to make cron trigger only on master branch? To give some context: when team mates create a new feature branch and commit there Jenkins file should still trigger (via poll or push) but what I do not want is the cron trigger fire on these feature branches.Muskogee
Use: stage('Stage1') { when { branch "master" } steps { } } OR stage('Stage1 (Not master)') { when { not { branch 'master' } } steps { sh 'do-non-master.sh' }}Gussiegussman
M
39

This is working for me:

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

See more in this article

Milly answered 5/9, 2018 at 11:15 Comment(2)
That just duplicates the answer by @Julian Veerkamp, including the linked Jira issue.Then
Not a duplicate! It's a different means of implementation inline rather than setting a variable outside the pipeline block. The devil is in the details with Jenkinsfile scripts, and the syntax is not always intuitive. SO THIS HELPS!Salesin
N
31

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 * * * *')]),
    ]
)
Nosh answered 26/8, 2016 at 18:20 Comment(3)
BTW for the first property you can use the buildDiscarder symbol to simplify syntax, as Pipeline Syntax should show.Pecan
It does not work in scripted pipelines under jenkins 2.79 (java.lang.UnsupportedOperationException: Undefined symbol ‘pipelineTriggers’)Casiano
Eric, just try with this for scripted pipelines: pipelineTriggers([[$class: "TimerTrigger", spec: "H 1 * * *"]])Ferrochromium
D
2

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''' : ""
}
Deranged answered 20/8, 2019 at 14:37 Comment(0)
A
0

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

Abdulabdulla answered 22/12, 2020 at 13:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.