python--將jenkins配置的任務導出到Excel


jenkins任務全部導出到Excel,完整代碼

from jenkins import Jenkins
import xlwt,re

jen = Jenkins(url="jenkins路由地址", username="用戶名", password="密碼")
all_jobs = jen.get_all_jobs()

def export_excel():
    wb = xlwt.Workbook(encoding='utf-8')
    ws = wb.add_sheet('test')
    style = xlwt.XFStyle()
    # 設置字體
    font = xlwt.Font()
    style.font = font
    # 單元格對齊
    alignment = xlwt.Alignment()
    # 水平對齊方式和垂直對齊方式
    alignment.horz = xlwt.Alignment.HORZ_CENTER
    alignment.vert = xlwt.Alignment.VERT_CENTER
    # 自動換行
    alignment.wrap = 1
    style.alignment = alignment
    ws.write(0, 0, '_class')
    ws.write(0, 1, 'name')
    ws.write(0, 2, 'url')
    ws.write(0, 3, 'color')
    ws.write(0, 4, 'fullname')
    ws.write(0, 5, 'gitUrl')
    ws.write(0, 6, 'shell')
    try:
        for i in range(len(all_jobs)):
            ws.write(i+1,0,all_jobs[i].get('_class'))
            ws.write(i+1,1,all_jobs[i].get('name'))
            ws.write(i+1,2,all_jobs[i].get('url'))
            ws.write(i+1,3,all_jobs[i].get('color'))
            ws.write(i+1,4,all_jobs[i].get('fullname'))
            print(all_jobs[i].get('name'))
            name_list = ['compile',               # name_list是我拉取到所有應用里請求git地址和shell腳本時報錯的應用名'knowledge',
                         'database',
                         'docker-images','python-runtime-build']
            if all_jobs[i].get('name') in name_list:
                pass
            else:
                conf = jen.get_job_config(name=all_jobs[i].get('name'))
                url = re.finditer(r"<url>.*?</url>", conf)
                for match in url:
                    gitUrl = match.group()
                    ws.write(i+1, 5, gitUrl.lstrip('<url>').rstrip('</url>'))
                comm = re.finditer(r"<command>[\s\S]*?</command>", conf)
                for match in comm:
                    shellComm = match.group()
                    shellStr = shellComm.lstrip('<command>').rstrip('</command>')   # 去除標簽
                    shellstr = re.sub(r'&quot;', '"', shellStr)        # 雙引號亂碼替換
                    shellstr = re.sub(r'&apos;',"'",shellstr)          # 單引號亂碼替換
            shellstr = re.sub(r'&amp;',"&",shellstr)       # &亂碼替換
ws.write(i + 1, 6, shellstr) except Exception as e: print(e) # 保存excel文件 wb.save('./test.xls') if __name__ == '__main__': export_excel()


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM