增加Jenkins内直接修改压缩包内allure报告名称方法

This commit is contained in:
wangjie 2025-01-08 16:41:08 +08:00
parent 8da0e848ad
commit 248da04b69
1 changed files with 62 additions and 1 deletions

View File

@ -4,8 +4,11 @@
# @Author : wangjie
# @File : jenkins_handle.py
# @project : SensoroApiAutoTest
import json
import os
import shutil
import tempfile
import zipfile
from configs.paths_config import OUT_FILES_DIR
from utils.allure_handle import AllureReportBeautiful
@ -59,5 +62,63 @@ def change_jenkins_allure_report_name():
return None
def modify_jenkins_allure_report_name_in_zip():
"""
直接修改Jenkins构建归档中的allure-report.zip压缩包的报告名称然后重新压缩相比较于上面的change_jenkins_allure_report_name方法的好处是直接在原压缩包内修改
:return:
"""
# 从环境变量中读取报告名称
new_name = os.getenv('ALLURE_REPORT_NAME',
'Allure Report') # 如果环境变量中没有ALLURE_REPORT_NAME并且未传报告名称参数默认使用'Allure Report'
# 找到zip文件路径
zip_path = os.path.join(JENKINS_HOME, 'jobs', ProjectName, 'builds', BUILD_NUMBER, 'archive',
'allure-report.zip')
# 检查allure-report.zip压缩包是否存在
if not os.path.exists(zip_path):
raise FileNotFoundError(f"allure-report.zip压缩包未找到{zip_path}")
# 临时文件夹用于存放解压后的内容
with tempfile.TemporaryDirectory() as temp_dir:
# 打开并提取 zip 文件
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
zip_ref.extractall(temp_dir)
# 定义需要修改的文件路径
summary_file_path = os.path.join(temp_dir, 'allure_report', 'widgets', 'summary.json')
# 检查 summary.json 是否存在
if not os.path.exists(summary_file_path):
raise FileNotFoundError(f"在zip归档文件中找不到summary.json文件{summary_file_path}")
# 读取原始 summary.json
with open(summary_file_path, 'r', encoding='utf-8') as file:
summary_data = json.load(file)
# 修改 summary.json 的内容
summary_data['reportName'] = new_name
# 保存修改后的 summary.json
with open(summary_file_path, 'w', encoding='utf-8') as file:
json.dump(summary_data, file, indent=4, ensure_ascii=False)
# 创建一个新的 zip 文件,并将修改后的文件重新压缩
new_zip_path = zip_path.replace('.zip', '.zip')
with zipfile.ZipFile(new_zip_path, 'w', zipfile.ZIP_DEFLATED) as zip_ref:
# 遍历解压后的文件夹,重新压缩成一个新的 zip 文件
for foldername, subfolders, filenames in os.walk(temp_dir):
for filename in filenames:
file_path = os.path.join(foldername, filename)
# 设置正确的 arcname以保持 zip 文件的原有结构
arcname = os.path.relpath(file_path, temp_dir)
zip_ref.write(file_path, arcname)
print(f"修改后重新压缩zip文件: {new_zip_path}")
return new_zip_path
if __name__ == '__main__':
change_jenkins_allure_report_name()
# change_jenkins_allure_report_name()
modified_zip = modify_jenkins_allure_report_name_in_zip()