6

I am looking to implement different cron triggers per branch in a declarative pipeline jenkins job. At the minute, I am only triggering hourly builds on our dev branch:

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

pipeline {

   triggers {
        cron(cron_string)
   }

   //stages, options and more code here...

}

My aim would be to have two separate cron strings that would trigger builds at different times in separate branches (eg: hourly builds in dev, every three hours builds in master), however the execution would be identical. My question is, can I do something like the code block below or should I take a different approach?

String cron_string_1 = BRANCH_NAME == "dev"     ? "0 8/20 ? * MON-FRY" : ""
String cron_string_2 = BRANCH_NAME == "master"  ? "0 8/20/3 ? * MON-FRY" : ""


pipeline {

   triggers {
        cron(cron_string)
   }

   //stages, options and more code here...

}

1 Answers1

0

This worked for me (using scripted pipelines):

if (BRANCH_NAME == "dev") {
    properties(
        [
            pipelineTriggers([cron('0 8,13,17 * * *')])
        ]
    )
}
Simon Oualid
  • 365
  • 2
  • 9
  • This won’t work for multiple branches. Assuming that the dev branch builds the first job, the pipeline will run and schedule the next build as per the cron. Now, no matter which branch builds the next job, it will run as per the dev branch cron. The same goes for the master branch cron too. – Dibakar Aditya Nov 06 '19 at 20:08