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

Conditional application of triggers in Declarative

    XMLWordPrintable

Details

    Description

      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') }
      }
      

      Attachments

        Issue Links

          Activity

            abayer 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.

            abayer 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.

            ❤️

            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?

            docwhat 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 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 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 } }  
            mginou 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).

             

            mginou 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).  
            abayer 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?

            abayer 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?
            ljbd Ł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
                }
            }
            
            ljbd Ł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 } }
            zachweg Zach Weg added a comment - - edited

            We could really use a way to conditionally trigger scheduled multibranchpipeline builds by branch name with declarative dsl. ljbd's suggestion to use a "when" directive within the "trigger" block seems preferable. Any status update on this issue?

            zachweg Zach Weg added a comment - - edited We could really use a way to conditionally trigger scheduled multibranchpipeline builds by branch name with declarative dsl. ljbd 's suggestion to use a "when" directive within the "trigger" block seems preferable. Any status update on this issue?

            This has been working for me:

              triggers {
                cron(env.BRANCH_NAME == 'master' ? '@daily' : '')
              }
            
            docwhat Christian Höltje added a comment - This has been working for me: triggers { cron(env.BRANCH_NAME == 'master' ? '@daily' : '') }
            kaibutsux 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.

            kaibutsux 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.

            kompastver 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 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 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...

            dwatroustrinet 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?

            webrat Andreas Sieferlinger added a comment - Has there been any progress on this?
            tridnguyen 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 * * *' : '')
              }
            } 
            tridnguyen 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)

            tulip 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 * * *' : '')
              }
            
            ericrdgz 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 * * *' : '')   }

            People

              Unassigned Unassigned
              docwhat Christian Höltje
              Votes:
              47 Vote for this issue
              Watchers:
              59 Start watching this issue

              Dates

                Created:
                Updated: