Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Artifactory discardOldBuilds to true in a Jenkinsfile?

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.

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']]])

How can I set discardOldBuilds to true in my Jenkinsfile so I can also clean-up the builds stored in Artifactory?

like image 493
rasebo Avatar asked Jun 22 '16 16:06

rasebo


People also ask

How do I link Jfrog Artifactory to Jenkins?

To install the Jenkins Artifactory Plugin, go to Manage Jenkins > Manage Plugins, click on the Available tab and search for Artifactory. Select the Artifactory plugin and click Download Now and Install After Restart.

How does Artifactory work with Jenkins?

The popular Jenkins Artifactory Plugin brings Artifactory's Build Integration support to Jenkins. This integration allows your build jobs to deploy artifacts and resolve dependencies to and from Artifactory, and then have them linked to the build job that created them.

How do you push artifacts to Artifactory from Jenkins?

This is made via passing Jenkins BUILD_NUMBER variable to Maven. In the post-build actions section, add deploy artifacts to the Artifactory step, specify that you would like to add your artifacts, and check the 'Deploy Maven Artifacts' checkbox. You will also need to specify a project name and path to your repository.

How do I get rid of old builds in Jenkins?

The global build discarder settings can be accessed from Manage Jenkins -> Configure System -> Global Build Discarders . The default "Project Build Discarder" requires a project to have the "Discard Old Builds" setting configured.


3 Answers

Into official JenkinsFile https://github.com/jenkinsci/jenkins/blob/master/Jenkinsfile has:

properties([[$class: 'jenkins.model.BuildDiscarderProperty', strategy: 
            [$class: 'LogRotator', numToKeepStr: '50', artifactNumToKeepStr: '20']
           ]])

A example of own use with github plugin and jenkins multibranch pipeline:

#!groovy

node {

  try {
    properties([ 
      [
        $class: 'jenkins.model.BuildDiscarderProperty',
        strategy: [
          $class: 'LogRotator',
          numToKeepStr: '10'
          ]
      ],
      [
        $class: 'GithubProjectProperty',
        displayName: '',
        projectUrlStr: 'https://github.com/xxxxxxx/xxxxx-xxxx/'
      ]
    ])

    env.JAVA_HOME = tool 'JDK8'
    def mvnHome = tool 'Maven'

    stage 'Clean Workspace'
      deleteDir()


    stage 'Git Checkout Source'
      checkout scm


    stage 'Build Maven Module'
      sh "${mvnHome}/bin/mvn clean install -DskipTests=true -DskipITs=true -U"


    stage 'Unit Test Maven Module'
      sh "${mvnHome}/bin/mvn test -DskipTests=false -DskipITs=true"


    stage 'Integration Test Maven Module'
      sh "${mvnHome}/bin/mvn verify -DskipTests=true -DskipITs=false"


    stage 'Nexus Deploy Artifact'
      sh "${mvnHome}/bin/mvn deploy:deploy -Pnexus-deploy"


    stage 'Trigger Job xxxxxx-xxxxxx /master'
      build job: 'xxxxxx-xxxxxx/master', propagate: false, wait: false



    stage 'Send Success Email'
      mail from: '[email protected]',
           to: '[email protected]',
           subject: "[JENKINS] ${env.JOB_NAME} - Build # ${env.BUILD_NUMBER} - [SUCCESS]!",
           body: "${env.JOB_NAME} - Build # ${env.BUILD_NUMBER} - SUCCESS!"

  }
  catch (Exception ex) {
    mail from: '[email protected]',
         to: '[email protected]',
         subject: "FAILURE - [JENKINS] ${env.JOB_NAME} - Build # ${env.BUILD_NUMBER} - [FAILURE]!",
         body: "${env.JOB_NAME} - Build # ${env.BUILD_NUMBER} - FAILURE (${ex.message})!"
    throw ex
  }

}

In my Sonatype Nexus, I created a task to clean up the artifacts. enter image description here

I do not use 'Artifactory', but I believe you can create an internal task to clear it.

You can also manually remove, in the case of maven use, you can follow this example:

How do you deal with maven-3 timestamped snapshots efficiently?

I hope I helped you.

like image 64
frekele Avatar answered Sep 28 '22 04:09

frekele


This has been fixed in the latest Artifactory Jenkins plugin.

As per the documentation, to trigger build retention when publishing build-info to Artifactory, use the following method:

buildInfo retention maxBuilds: 10
buildInfo retention maxDays: 7
// deleteBuildArtifacts is false by default.

buildInfo retention maxBuilds: 10, maxDays: 7, doNotDiscardBuilds: ["3", "4"], deleteBuildArtifacts: true
like image 28
rasebo Avatar answered Sep 28 '22 04:09

rasebo


Triggering Artifactory Build retention from a Pipeline script is planned to be supported in the coming release of the Jenkins Artifactory Plugin. See this issue: https://www.jfrog.com/jira/browse/HAP-796

like image 34
Eyal Ben Moshe Avatar answered Sep 28 '22 03:09

Eyal Ben Moshe