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

          This has been working for me:

            triggers {
              cron(env.BRANCH_NAME == 'master' ? '@daily' : '')
            }
          

          Christian Höltje added a comment - This has been working for me: triggers { cron(env.BRANCH_NAME == 'master' ? '@daily' : '') }

          Sean Grider added a comment -

          I would also love to see a feature that allowed the triggers to also specify the parameters to triggered job.

           

          pipeline {
            triggers {
               cron('0 0 * * *', params { BUILD=TRUE })
               cron('0 3 * * *', params { DEPLOY=TRUE})
            }
          }

          This would alleviate the dependency on external jobs scheduled to trigger this pipeline to get the parameters at different times.

          Sean Grider added a comment - I would also love to see a feature that allowed the triggers to also specify the parameters to triggered job.   pipeline { triggers { cron('0 0 * * *', params { BUILD=TRUE }) cron('0 3 * * *', params { DEPLOY=TRUE}) } } This would alleviate the dependency on external jobs scheduled to trigger this pipeline to get the parameters at different times.

          In our case, we'd like to be able to enable or disable githubPush trigger based on a flag from a variable.

          Something like this:

          pipeline {
              triggers {
                  when {
                      expression { enableTrigger = true }
                  }
                  githubPush()
              }
          
              stages {
              // do something
              }
          }
          

          The workaround for cron trigger does not applicable for githubPush and others.

          Pavel Znamensky added a comment - In our case, we'd like to be able to enable or disable githubPush trigger based on a flag from a variable. Something like this: pipeline { triggers { when { expression { enableTrigger = true } } githubPush() } stages { // do something } } The workaround for cron trigger does not applicable for githubPush and others.

          Igwe Kalu added a comment -

          These discussions are quite are useful and I can relate with many of the ideas/suggestions in my own use cases.

           

          abayer, is there any plan to start progressing this issue soon?

          Igwe Kalu added a comment - These discussions are quite are useful and I can relate with many of the ideas/suggestions in my own use cases.   abayer , is there any plan to start progressing this issue soon?

          kaibutsux, I found https://plugins.jenkins.io/parameterized-scheduler while looking for this same feature.

          My use case is a little different. I have a build that deploys software, but I want that software to get cleaned up some fixed number of hours after the last build. What I really want is to schedule a one time build, but I realized I might be able to use triggers for this. When the initial build runs, I dynamically generate the cron to run at a time that would be the number of hours later that I want. When the clean up build runs, I would set the cron trigger to an empty string, so it effectively on runs one time...

          Daniel Watrous added a comment - kaibutsux , I found https://plugins.jenkins.io/parameterized-scheduler  while looking for this same feature. My use case is a little different. I have a build that deploys software, but I want that software to get cleaned up some fixed number of hours after the last build. What I really want is to schedule a one time build, but I realized I might be able to use triggers for this. When the initial build runs, I dynamically generate the cron to run at a time that would be the number of hours later that I want. When the clean up build runs, I would set the cron trigger to an empty string, so it effectively on runs one time...

          Has there been any progress on this?

          Andreas Sieferlinger added a comment - Has there been any progress on this?

          Tri Nguyen added a comment - - edited

          I imagine the cron string could be set conditionally (only has a value on `master` branch, and  an empty string if not)? In the case that you call cron with an empty string, would it have any side effect?

           I tried this out, and while I can just set the cron job to an empty string, the problem is that I can't have an expression evaluated for the triggers. Here's how I tried it:

          pipeline {
            triggers {
              cron( env.BRANCH_NAME.equals('master') ? '* 1 * * *' : '')
            }
          } 

          Tri Nguyen added a comment - - edited I imagine the cron string could be set conditionally (only has a value on `master` branch, and  an empty string if not)? In the case that you call cron with an empty string, would it have any side effect?  I tried this out, and while I can just set the cron job to an empty string, the problem is that I can't have an expression evaluated for the triggers. Here's how I tried it: pipeline { triggers { cron( env.BRANCH_NAME.equals( 'master' ) ? '* 1 * * *' : '') } }

          It would be useful if we can have conditional triggers while using Parameterized cron trigger as well (https://github.com/jenkinsci/parameterized-scheduler-plugin)

          Illakkiya Ravichandran added a comment - It would be useful if we can have conditional triggers while using Parameterized cron trigger as well ( https://github.com/jenkinsci/parameterized-scheduler-plugin )

          I can confirm this workaround works for me 

          pipeline {
            triggers {
                cron( env.BRANCH_NAME.equals('${env.BRANCH_NAME}') ? '0 2 * * *' : '')
            }
          

          Eric Rodriguez added a comment - I can confirm this workaround works for me  pipeline {   triggers {       cron( env.BRANCH_NAME.equals( '${env.BRANCH_NAME}' ) ? '0 2 * * *' : '')   }

          Looking for something similar. I am creating a standard Pipeline that is generic enough to be used by anyone via shared library. Taking triggers as args fails with `Expected a trigger`
          So something like below does not work
          ```
          triggers

          { args.triggers ?: cron('') }

          ```
          The `when` would be helpful here.

          Sayali Gaikawad added a comment - Looking for something similar. I am creating a standard Pipeline that is generic enough to be used by anyone via shared library. Taking triggers as args fails with `Expected a trigger` So something like below does not work ``` triggers { args.triggers ?: cron('') } ``` The `when` would be helpful here.

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

              Created:
              Updated: