Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 258
geowa4 Avatar asked Aug 26 '16 14:08

geowa4


People also ask

How do you run Jenkins pipeline periodically?

Jenkins Cron Syntax From the Jenkins code documentation: To allow periodically scheduled tasks to produce even load on the system, the symbol H (for “hash”) should be used wherever possible. For example, using 0 0 * * * for a dozen daily jobs will cause a large spike at midnight.

How do I create a multi branch pipeline in Jenkins?

Step 1: From the Jenkins home page create a “new item”. Step 2: Select the “Multibranch pipeline” from the option and click ok. Step 3: Click “Add a Source” and select Github. Step 4: Under the credentials field, select Jenkins, and create a credential with your Github username and password.

How do you trigger the pipeline for periodically option?

If we want to trigger our build periodically we need check the "Build periodically" option in the build section. However In the Multilbranch Pipeline the we do not have the build trigger section available. If We want to "Build periodically" for a multibranch pipeline then we need to change our Jenkinsfile to do that.


1 Answers

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

like image 169
Julian Veerkamp Avatar answered Oct 16 '22 08:10

Julian Veerkamp