Jenkins declarative pipeline with different triggers per branch
Asked Answered
E

1

7

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

}
Enchanter answered 14/3, 2019 at 8:48 Comment(3)
Hi Jose, the best thing here would be multibranch pipeline with different triggers for each branch (the jenkinsfiles would be slightly different, but that's the point of multibranch pipeline).Airship
Did you ever find a workaround for this?Drusie
We ended up creating a Jenkinsfile per branch and separate jobs for them.Anesthesiology
I
1

This worked for me (using scripted pipelines):

if (BRANCH_NAME == "dev") {
    properties(
        [
            pipelineTriggers([cron('0 8,13,17 * * *')])
        ]
    )
}
Inebriant answered 6/11, 2019 at 13:44 Comment(1)
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.Bluefield

© 2022 - 2024 — McMap. All rights reserved.