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

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

            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.

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

            People

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

              Dates

                Created:
                Updated: