Jenkins: Is there any way to cleanup Jenkins workspace?

后端 未结 11 1824
既然无缘
既然无缘 2020-11-29 03:18

How can I cleanup the workspace in Jenkins? I am using AccuRev as version control tool.

I created freestyle projects in Jenkins.

相关标签:
11条回答
  • 2020-11-29 03:44

    There is an option to do it. Configure -> Post-build Actions
    The option comes by default at least in Jenkins version 2.236

    0 讨论(0)
  • 2020-11-29 03:48

    Beside above solutions, there is a more "COMMON" way - directly delete the largest space consumer from Linux machine. You can follow the below steps:

    1. Login to Jenkins machine (Putty)
    2. cd to the Jenkins installation path

    Using ls -lart to list out hidden folder also, normally jenkin installation is placed in .jenkins/ folder

    [xxxxx ~]$ ls -lart
    
    drwxrwxr-x 12 xxxx  4096 Feb  8 02:08 .jenkins/
    
    1. list out the folders spaces

    Use df -h to show Disk space in high level

    du -sh ./*/ to list out total memory for each subfolder in current path.

    du -a /etc/ | sort -n -r | head -n 10 will list top 10 directories eating disk space in /etc/

    1. Delete old build or other large size folder

    Normally ./job/ folder or ./workspace/ folder can be the largest folder. Please go inside and delete base on you need (DO NOT delete entire folder).

    rm -rf theFolderToDelete

    0 讨论(0)
  • 2020-11-29 03:48

    IMPORTANT: It is safe to remove the workspace for a given Jenkins job as long as the job is not currently running!

    NOTE: I am assuming your $JENKINS_HOME is set to the default: /var/jenkins_home.

    Clean up one workspace

    rm -rf /var/jenkins_home/workspaces/<workspace>

    Clean up all workspaces

    rm -rf /var/jenkins_home/workspaces/*

    Clean up all workspaces with a few exceptions

    This one uses grep to create a whitelist:

    ls /var/jenkins_home/workspace \ 
      | grep -v -E '(job-to-skip|another-job-to-skip)$' \
      | xargs -I {} rm -rf /var/jenkins_home/workspace/{}
    

    Clean up 10 largest workspaces

    This one uses du and sort to list workspaces in order of largest to smallest. Then, it uses head to grab the first 10:

    du -d 1 /var/jenkins_home/workspace \
      | sort -n -r \
      | head -n 10 \
      | xargs -I {} rm -rf /var/jenkins_home/workspace/{}
    
    0 讨论(0)
  • 2020-11-29 03:49

    You can run the below script in the Manage JenkinsScripts Console for deleting the workspaces of all the jobs at one shot. We did this to clean up space on the file system.

    import hudson.model.*
    // For each project
    for(item in Hudson.instance.items) {
      // check that job is not building
      if(!item.isBuilding()) {
        println("Wiping out workspace of job "+item.name)
        item.doDoWipeOutWorkspace()
      }
      else {
        println("Skipping job "+item.name+", currently building")
      }
    }
    
    0 讨论(0)
  • 2020-11-29 03:49

    not allowed to comment, therefore:

    The answer from Upen works just fine, but not if you have Jenkins Pipeline Jobs mixed with Freestyle Jobs. There is no such Method as DoWipeWorkspace on Pipeline Jobs. So I modified the Script in order to skip those:

    import hudson.model.*
    import org.jenkinsci.plugins.workflow.job.WorkflowJob
    
    // For each project
    for(item in Hudson.instance.items) {
      // check that job is not building
      if(!item.isBuilding() && !(item instanceof WorkflowJob))
      {
        println("Wiping out workspace of job "+item.name)
        item.doDoWipeOutWorkspace()
      }
      else {
        println("Skipping job "+item.name+", currently building")
      }
    }
    

    you could also filter by Job Names if required: item.getDisplayName().toLowerCase().contains("release")

    0 讨论(0)
  • 2020-11-29 03:51

    Assuming the question is about cleaning the workspace of the current job, you can run:

    test -n "$WORKSPACE" && rm -rf "$WORKSPACE"/*
    
    0 讨论(0)
提交回复
热议问题