若是Jenkins使用的Master-Slave多節點架構,刪除Jenkins JOB後,相應JOB的slave節點的workspace不會被刪除python
stackoverflow上有相關的問答:http://stackoverflow.com/questions/20532705/deleting-jenkins-jobs-through-gui-does-not-delete-their-workspaces-in-slavesapi
google groups上有相關的討論:https://groups.google.com/forum/#!topic/jenkinsci-users/SiZ3DL-UJ-8架構
因此須要自行處理,因而用腳本實現該功能ui
具體思路是:google
相關的Python腳本以下: url
# -*- coding: utf-8 -*- import os import shutil import logging from jenkinsapi.jenkins import Jenkins logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__file__) def get_jenkins_instance(): jenkins_url = "http://jenkins.example.com" jenkins_username = "username" jenkins_password = "password" return Jenkins(jenkins_url, username=jenkins_username, password=jenkins_password) def clean_workspace(): jenkins_instance = get_jenkins_instance() jenkins_workspace_path = "/opt/JENKINS_HOME/workspace/" for dirpath, dirnames, filenames in os.walk(jenkins_workspace_path): if dirpath == jenkins_workspace_path: for dirname in dirnames: jenkins_job_name = dirname # 若是job被刪除,則清理相應的workspace if not jenkins_instance.has_job(jenkins_job_name): logger.info("removing workspace dir of job:%s" % dirname) shutil.rmtree(os.path.join(dirpath, dirname)) if __name__ == "__main__": clean_workspace()