Search code examples
jenkinscronjenkins-pipelinejenkins-plugins

Jenkinsfile on multibranch pipeline cron


I have a declarative pipeline for multibranch pipeline job, and I am interested in creating a cron trigger to it. Currently I already have cron trigger on it for master branch using the following cron string:

String cron_string = BRANCH_NAME == "master" ? '30 23 * * *' : ""

I am interested in modifying the cron string so the cron would be triggered when branch name is master or if branch name contains the string release

I Wondered how can I achieve this would appreciate your help. Thanks in advance, Alon


Solution

  • def no_cron = ""
    def some_cron = "30 23 * * *" 
    
    CRON_DATA = no_cron
    
    if (BRANCH_NAME == "master" || BRANCH_NAME.contains("release")) {
        CRON_DATA = some_cron 
    }
    
    pipeline {
        agent any 
    
        triggers {
            cron (CRON_DATA)
        }
        ...
    }