Either a bug in the docs or a missing feature but....

      Currently it appears as though the options section in a declarative pipeline is global to all uses of the pipeline.

      However a very common use case is to have a different artifact retention strategy for PRs vs mainline branches.

      According to the docs it does not seem like this is possible to do, so this

      This RFE is to update the documentation to show how it is done, or add support for this kind of use case (different options per something[1])

      [1] may be branch vs PR, could be branch names (master and stable may be considered special) etc...

          [JENKINS-44679] need to support script inside options

          James Nord added a comment -

          The workaround (way to do this?) is to put a code block before the pipeline step to calculate and store variables and then reference them later.

          def keepBuilds = 20
          if (env.CHANGE_ID != null) {
                def keepBuilds = 1
          }
          pipeline {
                options {
                     buildDiscarder(logRotator(numToKeepStr: "${keepBuilds}"))
                }
          ...
          }
          

          James Nord added a comment - The workaround (way to do this?) is to put a code block before the pipeline step to calculate and store variables and then reference them later. def keepBuilds = 20 if (env.CHANGE_ID != null ) { def keepBuilds = 1 } pipeline { options { buildDiscarder(logRotator(numToKeepStr: "${keepBuilds}" )) } ... }

          James Nord added a comment -

          abayer has confirmed that the following works now and is now supported.

          pipeline {
              options {
                  buildDiscarder(logRotator(numToKeepStr: "${env.CHANGE_ID == null ? '100' : '5'}"))
              }
          }
          

          James Nord added a comment - abayer has confirmed that the following works now and is now supported. pipeline { options { buildDiscarder(logRotator(numToKeepStr: "${env.CHANGE_ID == null ? '100' : '5' }" )) } }

          Andrew Widdersheim added a comment - - edited

          I have a similar need here but a bit more complex than the original reporter. I see that this issue is closed but hopefully we can re-examine the use case. I can also make a new issue.

          In my case I'd much rather omit buildDiscarder(). Also the original solution doesn't really work with things like disableConcurrentBuilds(). For example, it is not possible AFAIK to do something like:

          pipeline {
              options { 
                  disableConcurrentBuilds(env.FOO ? true : false) 
              }
          }

          Either way I'd much prefer to do something like this:

          pipeline { 
              options {
                  script {
                      if (env.FOO) { 
                          buildDiscarder logRotator(daysToKeepStr: '30')
                          disableConcurrentBuilds()
                      } 
                  }
              } 
          }

          Andrew Widdersheim added a comment - - edited I have a similar need here but a bit more complex than the original reporter. I see that this issue is closed but hopefully we can re-examine the use case. I can also make a new issue. In my case I'd much rather omit buildDiscarder() . Also the original solution doesn't really work with things like disableConcurrentBuilds() . For example, it is not possible AFAIK to do something like: pipeline { options { disableConcurrentBuilds(env.FOO ? true : false ) } } Either way I'd much prefer to do something like this: pipeline { options { script { if (env.FOO) { buildDiscarder logRotator(daysToKeepStr: '30' ) disableConcurrentBuilds() } } } }

            abayer Andrew Bayer
            teilo James Nord
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: