13

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.

Zach Weg
  • 1,553
  • 11
  • 13
  • 2
    https://stackoverflow.com/questions/39168861/build-periodically-with-a-multi-branch-pipeline-in-jenkins/44902622#44902622 – rohit thomas Jun 16 '18 at 06:41

1 Answers1

18

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
  }
} 
Zach Weg
  • 1,553
  • 11
  • 13