Alex Harvey
Alex Harvey

Reputation: 15502

triggeredBy a parameterizedCron in Jenkins pipeline

I have a Jenkins pipeline job like this:

  triggers {
    parameterizedCron(env.BRANCH_NAME == "master" ? "0 12 * * * % RUN_E2E=true;MODE=parallel" : "")
  }

Later I have conditional logic like this:

    stage('Build Release') {
      when {
        allOf {
          branch 'master'
          not {
            triggeredBy 'TimerTrigger'
          }
        }
      }

The triggeredBy is not activated however. That is, "not triggered by timerTrigger" appears to be true even when the parameterizedCron runs.

I got this example from the docs here.

My question is, if I want my build/release stage to execute only on branch == master, and not during the parameterizedCron execution, how can I do that?

Upvotes: 2

Views: 10587

Answers (1)

MaratC
MaratC

Reputation: 6889

Your problem is that parameterizedCron is not a TimerTrigger, only the regular cron is.

The easiest way to go about your requirement is to add a parameter and set it in parameterizedCron:

triggers {
    parameterizedCron(env.BRANCH_NAME == "master" ? "0 12 * * * % RUN_E2E=true;MODE=parallel;SHOULD_BUILD_RELEASE=no" : "")
  }

You can then do:

    stage('Build Release') {
      when {
        allOf {
          branch 'master'
          expression { SHOULD_BUILD_RELEASE == 'yes' }
        }
      }

Otherwise, you may programmatically discover why your build was triggered, and drop out of the build/release part if it turns out that parameterizedCron has triggered it. See here for an example. In the case of parameterizedCron, the relevant part is this:

timerCause = currentBuild.rawBuild.getCause(org.jenkinsci.plugins.parameterizedscheduler.ParameterizedTimerTriggerCause)
if (timerCause) {
    echo "Build reason: Build was started by parameterized timer"
}

Finally, you may try playing around with the exact class, like this (I haven't checked it and it may or may not work):

    stage('Build Release') {
      when {
        allOf {
          branch 'master'
          not {
            triggeredBy 'ParameterizedTimerTriggerCause'
          }
        }
      }

Upvotes: 7

Related Questions