feat: 更新文件处理逻辑,确保使用绝对路径并添加 CI_EMAIL 环境变量检查

This commit is contained in:
2025-04-11 22:49:51 +08:00
parent 31957e51a4
commit 6a5fec87e3

View File

@@ -1,11 +1,11 @@
#!/usr/bin/env python
import os
import sys
import subprocess
import threading
from concurrent.futures import ThreadPoolExecutor, as_completed
import yaml
import requests
import subprocess
import logging
# 初始化日志记录器
@@ -34,6 +34,7 @@ def process_item(item):
global modified
file_path = item.get("file")
source = item.get("source")
if not file_path or not source:
logging.warning(f"⚠️ 条目缺少 file 或 source 字段: {item}")
return
@@ -43,13 +44,13 @@ def process_item(item):
if new_content is None:
return
# 确保文件路径是相对路径且安全
rel_path = os.path.relpath(file_path)
os.makedirs(os.path.dirname(rel_path), exist_ok=True)
# 构建最终文件路径
abs_file_path = os.path.join(os.getcwd(), file_path)
os.makedirs(os.path.dirname(abs_file_path), exist_ok=True)
updated = False
if os.path.exists(rel_path):
with open(rel_path, "r", encoding="utf-8") as f:
if os.path.exists(abs_file_path):
with open(abs_file_path, "r", encoding="utf-8") as f:
old_content = f.read()
if old_content != new_content:
updated = True
@@ -57,16 +58,16 @@ def process_item(item):
updated = True
if updated:
with open(rel_path, "w", encoding="utf-8") as f:
with open(abs_file_path, "w", encoding="utf-8") as f:
f.write(new_content)
logging.info(f"🚀 更新了 {rel_path}")
logging.info(f"🚀 更新了 {abs_file_path}")
with LOCK:
modified = True
def check_env_vars():
"""检查必要的环境变量是否存在"""
required_vars = ["THE_REPO", "REPO_TOKEN"]
required_vars = ["THE_REPO", "REPO_TOKEN", "CI_EMAIL"]
missing_vars = [var for var in required_vars if not os.environ.get(var)]
if missing_vars:
logging.error(f"❌ 缺少必要的环境变量: {', '.join(missing_vars)}")
@@ -81,7 +82,8 @@ def main():
# 加载 list.yaml 文件
try:
with open("list.yaml", "r", encoding="utf-8") as f:
list_yaml_path = os.path.join(os.getcwd(), "list.yaml")
with open(list_yaml_path, "r", encoding="utf-8") as f:
items = yaml.safe_load(f) or []
except FileNotFoundError:
logging.error("❌ list.yaml 文件不存在。")
@@ -105,7 +107,8 @@ def main():
if modified:
try:
# 配置 Git 用户信息
subprocess.run(["git", "config", "--global", "user.email", "cicd@sugarscat.cn"], check=True)
ci_email = os.environ["CI_EMAIL"]
subprocess.run(["git", "config", "--global", "user.email", ci_email], check=True)
subprocess.run(["git", "config", "--global", "user.name", "CI/CD"], check=True)
# 设置远程仓库 URL隐藏敏感信息