Condition in Jenkins pipeline on the triggers directive
You can't use flow control in a pipeline outside of when
and script
, but you can call functions for things like trigger parameters:
pipeline { agent any triggers{ cron( getCronParams() ) } ...}def getCronParams() { if( someCondition ) { return 'H */4 * * 1-5' } else { return 'H/30 */2 * * *' } }
Another way is to generate your pipeline script dynamically using evaluate()
:
evaluate """pipeline { agent any ${getTriggers()} ...}"""String getTriggers() { if( someCondition ) { return "triggers{ cron('H */4 * * 1-5') }" } else { return "triggers{ pollSCM('H */4 * * 1-5') }" } }