11
votes

I have a parameterized Jenkins multibranch pipeline using a GitHub repo as the source for a Jenkinsfile and some scripts. The pipeline is configured to trigger on webhooks for branches and pull requests but I also want a parameterized cron trigger only for the master branch, specifically every 4hours on weekdays.

I'm using declarative pipeline syntax but I'm willing to use scripted pipeline if necessary.

I'm using the parameterized scheduler plugin to achieve cron triggers with parameters.

This pipeline example captures what I am trying to achieve but is not supported:

pipeline {
  triggers {
    when { branch "master" }
    parameterizedCron('H */4 * * 1-5 % ABC=XYZ')
  }
  stages {
  // do something
  }
}

There is an open Jenkins issue for this feature: JENKINS-42643 but it doesn't seem to be in development.

1

1 Answers

12
votes

Using a ternary operator worked for my use case. Builds are only scheduled for the master branch:

pipeline {
  triggers {
    parameterizedCron(env.BRANCH_NAME == 'master' ? '''
# schedule every 4hours only on weekdays
H */4 * * 1-5 % ABC=XYZ''' : '')
  }
  parameters {
    string(name: 'ABC', defaultValue: 'DEF', description: 'test param')
  }
  stages {
    // do something
  }
}