setting different values of crons in same pipeline

Viewed 18

Is it possible to set different values of parametrized crons on a single pipeline on a condition in groovy. So that first execution will set a diff cron and another execution will set a different cron depends upon a condition in groovy. I cant use a branch variable as I want to execute the pipeline with same branch but with different crons in different builds.

1 Answers

You can do something like the below.

def flag = false
def cron1 = 'H/15 * * * *'
def cron2 = 'H/30 * * * *'

pipeline {
    agent any
    triggers{ cron(flag ? cron1 : cron2) }
    stages {
    stage("A"){
        steps{
            script {
                echo "Test"
                }
            }
        }
    }
}

or this

def flag = false

def cronExp = ''
if(flag) {
    cronExp = 'H/15 * * * *'
} else {
    cronExp = 'H/30 * * * *'
}

pipeline {
    agent any
    triggers{ cron(cronExp) }
    stages {
    stage("A"){
        steps{
            script {
                echo "Test"
                }
            }
        }
    }
}
Related