Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
    }
}
like image 978
ralphcom Avatar asked Feb 12 '18 12:02

ralphcom


People also ask

Which one enables you to implement different Jenkinsfile for different branches of the same project?

The Multibranch Pipeline project type enables you to implement different Jenkinsfiles for different branches of the same project. In a Multibranch Pipeline project, Jenkins automatically discovers, manages and executes Pipelines for branches which contain a Jenkinsfile in source control.

How do I add a trigger in Jenkinsfile?

Adding a Jenkins triggerSelect a Jenkins master from the Master drop-down menu, then select a job from the Job drop-down. Add a property file, if desired. See the property files section of the Pipeline Expression Guide for more information about how to specify and use property files.

Can a Jenkinsfile have multiple pipelines?

A multi-branch pipeline project always includes a Jenkinsfile in its repository root. Jenkins automatically creates a sub-project for each branch that it finds in a repository with a Jenkinsfile . Multi-branch pipelines use the same version control as the rest of your software development process.

How do I create multiple branches in Jenkins?

Head over to your Jenkins instance and create a new item. Enter a name for the job, and select the “Multibranch Pipeline” option at the end of the screen. Then, click on the OK button. In the next screen, go to the “Branch sources” tab, click on the “Add source” button, and choose “Git” from the dropdown menu.


1 Answers

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
    }
}
like image 134
ralphcom Avatar answered Sep 22 '22 13:09

ralphcom