Muthu
Muthu

Reputation: 1550

How to delete an archived artifact in jenkins?

  1. I am using Jenkins with tomcat. I use jenkins cli from java class to create job and to build. I want to delete a archived artifact. How to accomplish this?

  2. Another question is, can we give a specific name to the build in jenkins (e.g) i want the build name like (buildNumber + someName). How to achieve this?

Upvotes: 4

Views: 28073

Answers (3)

zovits
zovits

Reputation: 915

If you only want to delete one specific archived artifact of one build, run the following groovy script at "Manage Jenkins/Nodes//Script Console":

def project = Jenkins.get().getItemByFullName('<your-project-id>')
def jobs = project.getAllJobs()
def job = job[0]
def build = job.getBuildByNumber(42) // the number of your build
def manager = build.getArtifactManager()
def file = manager.root().child('the_file_you_want_to_del.ete')
println file.toURI()
// uncomment the next line if the above has returned the correct URI
//Util.deleteFile(new File(file.toURI()))

Adapted from the beginning of the answer written by @mmonetha.

Upvotes: 0

mmonetha
mmonetha

Reputation: 11

For everyone looking for another approach. If you are using the jenkins web interface and have administrator rights for it. You can run the following groovy script at "Manage Jenkins/Nodes/<master node>/Script Console".

def project = Jenkins.get().getItemByFullName('<your-project-id>')
def jobs = project.getAllJobs()

def total_size = 0

jobs.each{ job -> 
  def builds = job.getBuilds()
  builds.each{ build -> 
    def artifacts = build.artifacts
    artifacts.each{ artifact ->
      total_size += artifact.getFileSize()
      println "$artifact, ${artifact.getFileSize()}"
    }
    build.deleteArtifacts()
  } 
}

println "Total size: $total_size"

Kudos to @perja12 for creating the script.

Upvotes: 1

malenkiy_scot
malenkiy_scot

Reputation: 16615

The artifacts for a build by default are located in: [JENKINS_HOME]/jobs/[job_name]/builds/[$BUILD_ID]/archive/, go there and delete it. It won't delete the link to the artifact from the build page, though. (If you are not sure where your JENKINS_HOME is, go to http://[jenkins_server]/configure, you'll see Home directory near the top of the page).

To change the display name of a build automatically try Build Name Setter Plugin.

To change the display name via CLI:

java -jar jenkins-cli.jar -s http://[server]/ set-build-display-name [job_name] [build#] [new_name]

Upvotes: 8

Related Questions