Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-42643

Conditional application of triggers in Declarative

      I want to be able to use trigger with cron but only for the master branch.

      if and when both don't work in the trigger context.

      Here is what I expected to work:

       

      if (env.BRANCH == 'master') {
        trigger { cron('@daily') }
      }
      

          [JENKINS-42643] Conditional application of triggers in Declarative

          Christian Höltje created issue -

          Andrew Bayer added a comment -

          Hmm, so yeah, if definitely wouldn't work there, by design, but I do think you have something worth considering in re: conditional triggering. I'm gonna rename this ticket and keep thinking about it.

          Andrew Bayer added a comment - Hmm, so yeah, if definitely wouldn't work there, by design, but I do think you have something worth considering in re: conditional triggering. I'm gonna rename this ticket and keep thinking about it.
          Andrew Bayer made changes -
          Summary Original: when/if doesn't work in trigger New: Conditional application of triggers in Declarative

          ❤️

          Maybe allow with to work anyplace? Or at least most places? e.g. the properties section (or options or whatever it is called) as well as inside a stage?

          Christian Höltje added a comment - ❤️ Maybe allow with to work anyplace? Or at least most places? e.g. the properties section (or options or whatever it is called) as well as inside a stage?

          Bruce Adams added a comment -

          An approach that works and seems legible:

          String cron_string = BRANCH_NAME == "master" ? "@hourly" : ""
          
          pipeline {
            agent none
            triggers { cron(cron_string) }
            stages {
              // do something
            }
          }
          

           

          Bruce Adams added a comment - An approach that works and seems legible: String cron_string = BRANCH_NAME == "master" ? "@hourly" : "" pipeline {   agent none triggers { cron(cron_string) }   stages {     // do something } }  

          Mike Ginou added a comment -

          My understanding is that this is possible using a Groovy Scripted pipeline (using something like what bruce_adams is suggesting)....perhaps we can allow a script block directly under pipeline that could be executed prior to the triggers block (or, frankly, any of the other blocks).

           

          Mike Ginou added a comment - My understanding is that this  is possible using a Groovy Scripted pipeline (using something like what bruce_adams  is suggesting)....perhaps we can allow a script block directly under pipeline that could be executed prior to the triggers block (or, frankly, any of the other blocks).  

          Andrew Bayer added a comment -

          So I'm considering adding something to options that would allow you to say "The triggers defined here should only apply if we're on this branch (or we're not a pull request)". It wouldn't be granular - you couldn't have just some of the triggers be applied but not others. Thoughts?

          Andrew Bayer added a comment - So I'm considering adding something to options that would allow you to say "The triggers defined here should only apply if we're on this branch (or we're not a pull request)". It wouldn't be granular - you couldn't have just some of the triggers be applied but not others. Thoughts?
          Andrew Bayer made changes -
          Issue Type Original: Bug [ 1 ] New: Improvement [ 4 ]
          Andrew Bayer made changes -
          Link New: This issue is duplicated by JENKINS-48898 [ JENKINS-48898 ]

          Łukasz Dudek added a comment -

          abayer - your implementation idea seems all right to me.

          However, I'd like to ask if my proposition from the duplicate issue is viable?
          It states that when closures could be used inside triggers:

          pipeline {
              triggers {
                  when { branch "master" }
                  cron('H */4 * * 1-5')
              }
              stages {
              // do something
              }
          }
          

          Łukasz Dudek added a comment - abayer - your implementation idea seems all right to me. However, I'd like to ask if my proposition from the duplicate issue is viable? It states that when closures could be used inside triggers : pipeline { triggers { when { branch "master" } cron( 'H */4 * * 1-5' ) } stages { // do something } }

            Unassigned Unassigned
            docwhat Christian Höltje
            Votes:
            48 Vote for this issue
            Watchers:
            61 Start watching this issue

              Created:
              Updated: