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

"Discard Old Builds" parameters as a environmental variable

      <jenkins.model.BuildDiscarderProperty>
      <strategy class="hudson.tasks.LogRotator">
      <daysToKeep>7</daysToKeep>
      <numToKeep>10</numToKeep>
      <artifactDaysToKeep>-1</artifactDaysToKeep>
      <artifactNumToKeep>-1</artifactNumToKeep>
      </strategy>
      </jenkins.model.BuildDiscarderProperty>

      Is there any way, we can create discard old builds parameters as a environment variable. So that those variables can be applied to all jobs instead of maintaining at the job level.

          [JENKINS-38239] "Discard Old Builds" parameters as a environmental variable

          The behavior can be achieved with https://plugins.jenkins.io/build-discarder/ (not using environment variables but global default settings)

          Christoph Vogtländer added a comment - The behavior can be achieved with https://plugins.jenkins.io/build-discarder/  (not using environment variables but global default settings)

          Elias Balasis added a comment - - edited

          see https://github.com/jenkinsci/build-discarder-plugin/issues/119

          The discarder does not work when assigned environment variables as parameters, in declarative pipelines.

          This would be great for assigning different discarder parameters to groups of build jobs and change as needed without having to edit all the jobs.

          Elias Balasis added a comment - - edited see https://github.com/jenkinsci/build-discarder-plugin/issues/119 The discarder does not work when assigned environment variables as parameters, in declarative pipelines. This would be great for assigning different discarder parameters to groups of build jobs and change as needed without having to edit all the jobs.

          maurice ampt added a comment -

          In issue #119 that you linked you also posted the following snippet:

          options {
                  buildDiscarder \
                      logRotator( \
                          artifactDaysToKeepStr: '', \
                          artifactNumToKeepStr: '', \
                          daysToKeepStr: '${some env var}', \
                          numToKeepStr: '${another env var}' \
                          )
              } 

          Assuming that is a bit of Groovy code, it makes sense that it does not work, as you are providing the literal string

          ${some env var}

           for variable interpolation to work you want to be using Groovy String (GStrings), which allow you to interpolate (environment) variables and require you to use double quotes (") instead of single quotes ('), so for example:

          daysToKeepStr: "${some env var}",
          

          Or, you drop the string part entirely as you do not seem to be doing any string concatenation or anything like that and pass the variable directly:

          daysToKeepStr: some_env_var,

          maurice ampt added a comment - In issue #119 that you linked you also posted the following snippet: options { buildDiscarder \ logRotator( \ artifactDaysToKeepStr: '', \ artifactNumToKeepStr: '', \ daysToKeepStr: '${some env var }' , \ numToKeepStr: '${another env var }' \ ) } Assuming that is a bit of Groovy code, it makes sense that it does not work, as you are providing the literal string ${some env var }  for variable interpolation to work you want to be using Groovy String (GStrings), which allow you to interpolate (environment) variables and require you to use double quotes (") instead of single quotes ('), so for example: daysToKeepStr: "${some env var }" , Or, you drop the string part entirely as you do not seem to be doing any string concatenation or anything like that and pass the variable directly: daysToKeepStr: some_env_var,

          Elias Balasis added a comment - - edited

          Good point xaho.

          I should have remembered that as I have used the same elsewhere.

          It is rather confusing though, as some pipeline actions, "withCredentials" for example, seem to be resolving environment variables while others require double quotes (")

          I have successfully tried with double quotes (") and I can now consider this issue as resolved.

          Elias Balasis added a comment - - edited Good point xaho . I should have remembered that as I have used the same elsewhere. It is rather confusing though, as some pipeline actions, "withCredentials" for example, seem to be resolving environment variables while others require double quotes (") I have successfully tried with double quotes (") and I can now consider this issue as resolved.

          Ken added a comment -

          Hey guys, is anyone aware of proper way to get buildDiscarder to keep infinite amount of builds/artifacts?

          I have a default build discarder configured for JCasC, but want to keep everything for a few pipelines. Does this option support a wildcard, empty string or '-1' as a value to indicate retain everything?

          Ken added a comment - Hey guys, is anyone aware of proper way to get buildDiscarder to keep infinite amount of builds/artifacts? I have a default build discarder configured for JCasC, but want to keep everything for a few pipelines. Does this option support a wildcard, empty string or '-1' as a value to indicate retain everything?

            nkns165 nkns165
            venk8t venkatachalam mani
            Votes:
            1 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated: