ralphcom
ralphcom

Reputation: 333

Separate triggers for different branches in Jenkinsfile

I'm working with a Multi-Branch Jenkins build, of which I want the develop branch to build periodically every two weeks and leave the master branch manual.

Our pipelines are pipelines as code, so I cannot set the config for the desired branches.

I'd like to build our develop branch once every 2 weeks on a sunday, so far I have found some different things.

Right now I've come to this schedule:

triggers {
    cron('00 12 /2 7')
}

But I do not know how to make it branch specific. I'm trying this right now to see if it works, should trigger a develop build every 5 mins or break.

    triggers {
        when (env.BRANCH_NAME == 'develop') {
            cron('H/5 * * * *')
        }
    }

when is not allowed in the triggers block.

I have found a 'solution' on the Jenkins jira which is this:

String cron_string = BRANCH_NAME == "develop" ? "00 12 /2 7" : ""

pipeline {
    agent none
    triggers { cron(cron_string) }
    stages {
    // do something
    }
}

Upvotes: 9

Views: 4871

Answers (1)

ralphcom
ralphcom

Reputation: 333

I have found a 'solution' on the Jenkins jira which is this:

String cron_string = BRANCH_NAME == "develop" ? "00 12 /2 7" : ""

pipeline {
    agent none
    triggers { cron(cron_string) }
    stages {
    // do something
    }
}

Upvotes: 11

Related Questions