Declarative Jenkinsfile: different cron trigger per branch

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Declarative Jenkinsfile: different cron trigger per branch

Amedee Van Gasse
I have a *declarative* Jenkinsfile.
I want to have a daily build of the develop branch.
I currently have the following trigger:

    triggers {
        cron(env.BRANCH_NAME == 'develop' ? '@midnight' : '')
    }

Now I also want to build the master branch. Not daily, but weekly.
I already know that I can have only one cron expression in triggers.

I know of one solution that works, and that I do not want to use: have a different cron expression committed to each branch.
I don't want that, because master branches from develop and then gets merged back into develop. Doing it that way, will cause merge conflicts that I don't want to deal with.

The other solution is to have a more complex cron expression in the trigger.
In pseudo code:

IF (env.BRANCH_NAME = 'develop')
THEN cron = '@midnight'
ELSE IF (env.BRANCH_NAME = 'master')
THEN cron = '@weekly'

I don't know how to construct the correct syntax for the cron expression. Is that Groovy? Linux shell? Something else?

If possible, then I would like to avoid "polluting" my declarative pipeline with script blocks (I don't even know if they are allowed inside triggers).

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/1e0c46fd-6031-41b9-9cac-1fbb622d9fc6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Declarative Jenkinsfile: different cron trigger per branch

Amedee Van Gasse
To answer my own question:

#!/usr/bin/env groovy

def schedule = env.BRANCH_NAME.contains('master') ? '@weekly' : env.BRANCH_NAME == 'develop' ? '@midnight' : ''

pipeline {

triggers {
cron(schedule)
}

Worked for me.

On Tuesday, July 9, 2019 at 10:34:06 AM UTC+2, Amedee Van Gasse wrote:
I have a *declarative* Jenkinsfile.
I want to have a daily build of the develop branch.
I currently have the following trigger:

    triggers {
        cron(env.BRANCH_NAME == 'develop' ? '@midnight' : '')
    }

Now I also want to build the master branch. Not daily, but weekly.
I already know that I can have only one cron expression in triggers.

I know of one solution that works, and that I do not want to use: have a different cron expression committed to each branch.
I don't want that, because master branches from develop and then gets merged back into develop. Doing it that way, will cause merge conflicts that I don't want to deal with.

The other solution is to have a more complex cron expression in the trigger.
In pseudo code:

IF (env.BRANCH_NAME = 'develop')
THEN cron = '@midnight'
ELSE IF (env.BRANCH_NAME = 'master')
THEN cron = '@weekly'

I don't know how to construct the correct syntax for the cron expression. Is that Groovy? Linux shell? Something else?

If possible, then I would like to avoid "polluting" my declarative pipeline with script blocks (I don't even know if they are allowed inside triggers).

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/ed96d9c4-e549-41d7-bd5a-2372a1ecfc22%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Declarative Jenkinsfile: different cron trigger per branch

Ivan Fernandez Calvo
Hi,

Every branch has its own Jenkinsfile, so you can have different configuration on every Jenkinsfile

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/aea7ae3d-13ed-4fe7-8de2-5b25fae9e6cf%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Declarative Jenkinsfile: different cron trigger per branch

Amedee Van Gasse


On Saturday, July 13, 2019 at 1:07:16 PM UTC+2, Ivan Fernandez Calvo wrote:
Hi,

Every branch has its own Jenkinsfile, so you can have different configuration on every Jenkinsfile

Hi Ivan,

Merge conflicts shall be avoided at all possible cost, therefor my previous solution shall be used, and your solution shall not be used. 

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/48296db8-3e0b-4ca9-a6a4-dc5782fc68d3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.