如果Jenkins使用的Master-Slave多节点架构,删除Jenkins JOB后,相应JOB的slave节点的workspace不会被删除
stackoverflow上有相关的问答:http://stackoverflow.com/questions/20532705/deleting-jenkins-jobs-through-gui-does-not-delete-their-workspaces-in-slaves
google groups上有相关的讨论:https://groups.google.com/forum/#!topic/jenkinsci-users/SiZ3DL-UJ-8
所以需要自行处理,于是用脚本实现该功能
具体思路是:
相关的Python脚本如下:
# -*- 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()
(adsbygoogle = window.adsbygoogle || []).push({});
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有