0

A "log rotation" strategy should be implemented in the Jenkins pipeline to only store the last 5 builds

This change should be made in the pipeline script itself (not through the Jenkins UI) to ensure the configuration is checked in.

yinon
  • 1,418
  • 11
  • 14
VSK
  • 21
  • 2
  • Have you checked [Override the settings with a job specific discarder](https://plugins.jenkins.io/build-discarder/#plugin-content-override-the-settings-with-a-job-specific-discarder)? – vijay v Dec 27 '21 at 11:48
  • 1
    Does this answer your question? [How to write Pipeline to discard old builds?](https://stackoverflow.com/questions/39542485/how-to-write-pipeline-to-discard-old-builds) – Noam Helmer Dec 27 '21 at 12:45

1 Answers1

0

I'm assuming you're using the Jenkins Artifactory plugin.

You can configure build retention before the publish build info step:

stage ('Set build retention') {
    steps {
        rtBuildInfo (
            maxBuilds: 5
        )
    }
}

You can find a good example here.

buildInfo.retention maxBuilds: 5

You can find a good example here.

yahavi
  • 5,149
  • 4
  • 23
  • 39
  • Thanks but will this syntax remove old build from artifactory as well? – VSK Jan 03 '22 at 05:31
  • @Vishal yes, it should remove also old builds with the same build name – yahavi Jan 03 '22 at 08:27
  • ,i tried to add the code but seems to not deleting old builds from artifactory, below is the code snippet stage("Artifactory Upload") { def directory = "generic-local/XYZ/ABC/${params['version']}/${currDate}/${buildNo}/" def server = Artifactory.server 'art-int' def buildInfo = Artifactory.newBuildInfo() buildInfo.retention maxBuilds: 10,deleteBuildArtifacts: true def upload_spec_bin = """{ "files": [ { "pattern": "*", "target": "${directory}", "exclusions": ["*.txt"] } ] }""" server.upload spec: upload_spec_bin } – VSK Jan 03 '22 at 10:24
  • if you have any update please let me know . – VSK Jan 03 '22 at 11:11
  • Looks like you forgot to publish the build info: server.publishBuildInfo buildInfo – yahavi Jan 03 '22 at 12:29
  • that also not working – VSK Jan 03 '22 at 16:31
  • stage("Artifactory Upload") { def directory = "generic-local/ABC/XYZ/${params['version']}/${currDate}/${buildNo}/" def server = Artifactory.server 'art-int' def buildInfo = Artifactory.newBuildInfo() buildInfo.retention maxBuilds: 10, deleteBuildArtifacts: true server.publishBuildInfo buildInfo def upload_spec_bin = """{ "files": [ { "pattern": "*", "target": "${directory}", "exclusions": ["*.txt"] } ] }""" server.upload spec: upload_spec_bin } @yahavi – VSK Jan 03 '22 at 16:32
  • i am not sure what i am missing here ================= stage("Artifactory Upload") { def directory = "generic-local/ABC/XYZ/${params['version']}/${currDate}/${buildNo}/" def server = Artifactory.server 'art-int' def buildInfo = Artifactory.newBuildInfo() buildInfo.retention maxBuilds: 10, deleteBuildArtifacts: true server.publishBuildInfo buildInfo def upload_spec_bin = """{ "files": [ { "pattern": "*", "target": "${directory}", "exclusions": ["*.txt"] } ] }""" server.upload spec: upload_spec_bin } – VSK Jan 03 '22 at 16:37
  • @Vishal you should publish the build info after the upload and change the maxBuilds to 5 – yahavi Jan 03 '22 at 17:57
  • yes i did that as well pls see below snip stage("Artifactory: Upload") { def server = Artifactory.server 'art-int' def buildInfo = Artifactory.newBuildInfo() def upload_spec_bin = """{ "files": [ { "pattern": "${syncapp_binary_name}", "target": "generic-local/XYZ/ABC/${env.BUILD_NUMBER}/" } ] }""" server.upload spec: upload_spec_bin buildInfo.retention maxBuilds: 5, deleteBuildArtifacts: true server.publishBuildInfo buildInfo } – VSK Jan 03 '22 at 18:06
  • `stage("Artifactory Upload") { def directory = "generic-local/ABC/XYZ/${params['version']}/${currDate}/${buildNo}/" def server = Artifactory.server 'art-int' def buildInfo = Artifactory.newBuildInfo() buildInfo.retention maxBuilds: 10, deleteBuildArtifacts: true def upload_spec_bin = """{ "files": [ { "pattern": "*", "target": "${directory}", "exclusions": ["*.txt"] } ] }""" server.upload spec: upload_spec_bin server.publishBuildInfo buildInfo } ` – VSK Jan 04 '22 at 15:30
  • @VSK looks like you published an empty build info. Please attach the buildinfo object to the upload command to make sure the uploaded artifact will be attached to the build: `server.upload spec: upload_spec_bin, buildInfo: buildInfo` – yahavi Jan 08 '22 at 12:56
  • does the build retention feature work for artifactory OSS version , or it work only on artifactory pro?, – VSK Jan 09 '22 at 12:49
  • [link] https://www.jfrog.com/confluence/display/JFROG/Jenkins+Artifactory+Plug-in as in this it says artifactory pro is required – VSK Jan 09 '22 at 13:40