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

Artifactory DSL set discardOldBuilds to true in multibranch pipeline Jenkinsfile

    • Icon: Improvement Improvement
    • Resolution: Fixed
    • Icon: Major Major
    • artifactory-plugin
    • None

      I am working on implementing a build job using Jenkins Multibranch Pipelines. The final stage is uploading the build output to Artifactory.

      When configuring a standalone job via the interface, there is an option to "Discard old builds from Artifactory" which allows me to only keep the same number of builds as specified in "Max # of builds to keep" setting.

      A "normal" DSL job can be configured like this https://issues.jenkins-ci.org/browse/JENKINS-29856, but I haven't been able to figure out how to set discardOldBuilds to true in my multibranch pipeline Jenkinsfile.

      The upload stage in my Jenkinsfile is configured like this: https://wiki.jenkins-ci.org/display/JENKINS/Artifactory+-+Working+With+the+Pipeline+Jenkins+Plugin

      ... and I also have the following in my Jenkinsfile which does clean-up the builds in the Jenkins workspace:

      properties([[$class: 'BuildDiscarderProperty', strategy: [$class: 'LogRotator', numToKeepStr: '10']]])

      Assuming (maybe wrongfully) that this is a limitation of the current pipeline artifactory DSL and it's currently not possible to set discardOldBuilds to true, would it be possible to have this feature added to the roadmap? If not, I would be very open to any and all enlightening initiatives.

          [JENKINS-36364] Artifactory DSL set discardOldBuilds to true in multibranch pipeline Jenkinsfile

          Razvan Botez created issue -
          R. Tyler Croy made changes -
          Workflow Original: JNJira [ 173067 ] New: JNJira + In-Review [ 184915 ]
          Vasili Gulevich made changes -
          Attachment New: Screenshot 2024-10-05 at 10.42.10.png [ 63374 ]
          Vasili Gulevich made changes -
          Link New: This issue is blocked by JENKINS-73864 [ JENKINS-73864 ]
          Vasili Gulevich made changes -
          Comment [ I've tried
          {code:java}
          options {
                 buildDiscarder(logRotator(numToKeepStr: '2', daysToKeepStr: '7'))
                 disableConcurrentBuilds()
              }
          {code}
          Based on an answer from [Stackoverflow|https://stackoverflow.com/a/59668598/125562] but this does not remove builds 13 days old.

          !Screenshot 2024-10-05 at 10.42.10.png|width=357,height=286!

          I'm using [GitLab Branch Source Version 704.vc7f1202d7e14|https://plugins.jenkins.io/gitlab-branch-source]

           

          Likely [removeLastBuild|https://github.com/jenkinsci/jenkins/blob/77c3d7ac843d3d5756eca3139a65f450a38b7a48/core/src/main/java/hudson/tasks/LogRotator.java#L116C21-L116C36] property of LogRotator is never set.

          I've tried
          {code:java}
          buildDiscarder(logRotator(numToKeepStr: '2', daysToKeepStr: '7', removeLastBuild: true)){code}
          But this fails with:
          {code:java}
          org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
          WorkflowScript: 30: Invalid parameter "removeLastBuild", did you mean "numToKeepStr"? @ line 30, column 73.
             Str: '2', daysToKeepStr: '7', removeLast
                                           ^

          WorkflowScript: 30: Invalid parameter "removeLastBuild", did you mean "numToKeepStr"? @ line 30, column 73.
             Str: '2', daysToKeepStr: '7', removeLast
                                           ^

          2 errors

          at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:309)
          at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1107)
          at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:624)
          at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:602)
          at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:579)
          at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:323)
          at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:293)
          at PluginClassLoader for script-security//org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.GroovySandbox$Scope.parse(GroovySandbox.java:163)
          at PluginClassLoader for workflow-cps//org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.doParse(CpsGroovyShell.java:190)
          at PluginClassLoader for workflow-cps//org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.reparse(CpsGroovyShell.java:175)
          at PluginClassLoader for workflow-cps//org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.parseScript(CpsFlowExecution.java:635)
          at PluginClassLoader for workflow-cps//org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.start(CpsFlowExecution.java:581)
          at PluginClassLoader for workflow-job//org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:335)
          at hudson.model.ResourceController.execute(ResourceController.java:101)
          at hudson.model.Executor.run(Executor.java:446) {code}
          Created JENKINS-73864. ]
          Vasili Gulevich made changes -
          Link New: This issue duplicates JENKINS-68822 [ JENKINS-68822 ]
          Vasili Gulevich made changes -
          Resolution New: Fixed [ 1 ]
          Status Original: Open [ 1 ] New: Resolved [ 5 ]

            eyalbe Eyal Ben Moshe
            razvan_botez Razvan Botez
            Votes:
            2 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: