name: Sync Assets to Private B Repo on: push: branches: [ main ] paths: - 'common/data/xml/assets/**' workflow_dispatch: jobs: sync: runs-on: ubuntu-latest steps: - name: Checkout A Repo uses: actions/checkout@v4 with: fetch-depth: 0 - name: Set up Python uses: actions/setup-python@v5 with: python-version: '3.x' - name: Prepare files and remove XML comments run: | # 创建临时目录 mkdir -p temp-b-repo/out/assets # 检查源目录是否存在 if [ ! -d "common/data/xml/assets" ]; then echo "Error: common/data/xml/assets directory not found!" && exit 1 fi # 复制源文件到临时目录 cp -rv common/data/xml/assets/* common/data/xml/assets/.* temp-b-repo/out/assets/ 2>/dev/null || true echo "Files copied to temp directory (before processing):" ls -la temp-b-repo/out/assets # 安装lxml库 pip install lxml # 生成Python脚本(修复XML头和注释删除问题) echo "from lxml import etree" > remove_xml_comments.py echo "import os" >> remove_xml_comments.py echo "" >> remove_xml_comments.py echo "def remove_comments_from_xml(file_path):" >> remove_xml_comments.py echo " try:" >> remove_xml_comments.py echo " # 解析XML并保留原始声明和注释" >> remove_xml_comments.py echo " parser = etree.XMLParser(remove_comments=False, strip_cdata=False)" >> remove_xml_comments.py echo " tree = etree.parse(file_path, parser=parser)" >> remove_xml_comments.py echo " root = tree.getroot()" >> remove_xml_comments.py echo "" >> remove_xml_comments.py echo " # 关键修复:删除所有层级的注释(包括非root区域)" >> remove_xml_comments.py echo " # 遍历所有节点,强制检查注释类型" >> remove_xml_comments.py echo " for element in root.iter():" >> remove_xml_comments.py echo " # 遍历子节点的副本,避免修改时索引错乱" >> remove_xml_comments.py echo " for child in list(element):" >> remove_xml_comments.py echo " if child.tag is etree.Comment:" >> remove_xml_comments.py echo " element.remove(child)" >> remove_xml_comments.py echo "" >> remove_xml_comments.py echo " # 保存修改后的文件(修复XML头缺失问题)" >> remove_xml_comments.py echo " # 获取原始XML声明(如果存在)" >> remove_xml_comments.py echo " xml_declaration = tree.docinfo.xml_declaration" >> remove_xml_comments.py echo " encoding = tree.docinfo.encoding or 'utf-8'" >> remove_xml_comments.py echo "" >> remove_xml_comments.py echo " # 写入文件时强制保留XML声明" >> remove_xml_comments.py echo " with open(file_path, 'wb') as f:" >> remove_xml_comments.py echo " # 手动写入XML声明(确保不缺失)" >> remove_xml_comments.py echo " if xml_declaration or xml_declaration is None:" >> remove_xml_comments.py echo " f.write(f'\n'.encode(encoding))" >> remove_xml_comments.py echo " # 写入XML内容,控制自闭合标签格式" >> remove_xml_comments.py echo " tree.write(f," >> remove_xml_comments.py echo " encoding=encoding," >> remove_xml_comments.py echo " pretty_print=False," >> remove_xml_comments.py echo " method='xml'," >> remove_xml_comments.py echo " xml_declaration=False # 已手动处理声明,避免重复" >> remove_xml_comments.py echo " )" >> remove_xml_comments.py echo " print(f'Processed: {file_path}')" >> remove_xml_comments.py echo " except Exception as e:" >> remove_xml_comments.py echo " print(f'Error processing {file_path}: {e}')" >> remove_xml_comments.py echo "" >> remove_xml_comments.py echo "# 遍历所有XML文件并处理" >> remove_xml_comments.py echo "for root_dir, _, files in os.walk('temp-b-repo/out/assets'):" >> remove_xml_comments.py echo " for file in files:" >> remove_xml_comments.py echo " if file.endswith('.xml'):" >> remove_xml_comments.py echo " file_path = os.path.join(root_dir, file)" >> remove_xml_comments.py echo " remove_comments_from_xml(file_path)" >> remove_xml_comments.py # 运行Python脚本处理XML python remove_xml_comments.py echo "Files in temp directory after comment removal:" ls -la temp-b-repo/out/assets - name: Push to Private B Repo env: B_REPO_TOKEN: ${{ secrets.B_REPO_TOKEN }} B_REPO_URL: https://github.com/TO-teams/flash.git B_BRANCH: main run: | git config --global user.name "GitHub Actions" git config --global user.email "actions@github.com" rm -rf b-repo git clone https://$B_REPO_TOKEN@${B_REPO_URL#https://} b-repo || { echo "Failed to clone B repo"; exit 1; } cd b-repo git checkout $B_BRANCH || { echo "Failed to checkout B branch"; exit 1; } git pull origin $B_BRANCH || { echo "Failed to pull B repo"; exit 1; } mkdir -p out/assets cp -rv ../temp-b-repo/out/assets/* ../temp-b-repo/out/assets/.* out/assets/ 2>/dev/null || true echo "Files in B repo target directory after copy:" ls -la out/assets # 检查是否有实际变更 if git diff --quiet -- out/assets; then echo "No changes to sync. Exiting." exit 0 fi git add out/assets git commit -m "Sync assets (no comments): $(date +'%Y-%m-%d %H:%M:%S')" git push origin $B_BRANCH || { echo "Failed to push to B repo"; exit 1; } echo "Sync completed successfully."