name: Update README Table on: push: branches: - main # 确认你的主分支名是 main 而不是 master paths: - '**/*.js' - '**/*.py' jobs: update-readme: runs-on: ubuntu-latest permissions: contents: write steps: - name: 检出代码 uses: actions/checkout@v4 with: fetch-depth: 2 - name: 配置 Python 环境 uses: actions/setup-python@v5 with: python-version: '3.x' - name: 运行更新脚本 env: TZ: Asia/Shanghai run: | cat << 'EOF' > update_readme.py import os import re import datetime import subprocess # 1. 获取更改的文件列表(增加对 .py 的支持) try: diff_cmd = ['git', 'diff', '--name-only', 'HEAD~1', 'HEAD'] result = subprocess.run(diff_cmd, capture_output=True, text=True, check=True) changed_files = result.stdout.splitlines() except Exception: changed_files = [] # 同时匹配 js 和 py 文件 target_files = [f for f in changed_files if (f.endswith('.js') or f.endswith('.py')) and os.path.exists(f)] if not target_files: print("没有符合条件的脚本文件发生更改。") exit(0) # 2. 提取文件中的脚本名称 now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') updates = {} for f in target_files: with open(f, 'r', encoding='utf-8') as file: content = file.read() # 匹配 new Env("XXX") 或 Env('XXX') match = re.search(r'Env\s*\(\s*["\']([^"\']+)["\']\s*\)', content) if match: script_name = match.group(1) updates[script_name] = now if not updates: print("修改的文件中未找到 Env 声明。") exit(0) # 3. 修改 README.md readme_path = 'README.md' if not os.path.exists(readme_path): print("未找到 README.md") exit(1) with open(readme_path, 'r', encoding='utf-8') as file: lines = file.readlines() new_lines = [] updated_scripts_set = set() # 处理表格 for line in lines: processed = False # 匹配表格行,并提取脚本名 if line.strip().startswith('|'): parts = [p.strip() for p in line.split('|')] if len(parts) >= 4: script_name = parts[1] if script_name in updates: parts[2] = updates[script_name] # 更新时间 parts[3] = "✅" # 状态 line = f"| {parts[1]} | {parts[2]} | {parts[3]} |\n" updated_scripts_set.add(script_name) processed = True new_lines.append(line) # 如果是新脚本,追加到末尾 for script, time in updates.items(): if script not in updated_scripts_set: new_lines.append(f"| {script} | {time} | ✅ |\n") with open(readme_path, 'w', encoding='utf-8') as file: file.writelines(new_lines) print(f"成功更新: {list(updates.keys())}") EOF python update_readme.py - name: 提交并推送 run: | git config --local user.email "github-actions[bot]@users.noreply.github.com" git config --local user.name "github-actions[bot]" git add README.md if ! git diff --staged --quiet; then git commit -m "docs: 自动更新脚本状态 [skip ci]" git push else echo "没有变化需要提交" fi