104 lines
3.7 KiB
Go
104 lines
3.7 KiB
Go
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' # 使用最新稳定版Python
|
||
|
||
- name: Prepare files for B Repo (remove comments via Python)
|
||
run: |
|
||
mkdir -p temp-b-repo/out/assets
|
||
|
||
if [ -d "common/data/xml/assets" ]; then
|
||
# 复制源文件到临时目录
|
||
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
|
||
|
||
# 关键步骤:创建并运行Python脚本删除XML注释
|
||
cat > remove_xml_comments.py << 'EOF'
|
||
import xml.etree.ElementTree as ET
|
||
import os
|
||
|
||
def remove_comments_from_xml(file_path):
|
||
try:
|
||
# 解析XML文件,保留注释(需自定义解析器)
|
||
parser = ET.XMLParser(target=ET.TreeBuilder(insert_comments=True))
|
||
tree = ET.parse(file_path, parser=parser)
|
||
root = tree.getroot()
|
||
|
||
# 递归查找并删除所有注释节点
|
||
def remove_comments(element):
|
||
for child in list(element):
|
||
# 注释节点的tag为 'comment'(ElementTree内部标记)
|
||
if child.tag is ET.Comment:
|
||
element.remove(child)
|
||
else:
|
||
remove_comments(child)
|
||
|
||
remove_comments(root)
|
||
|
||
# 保存修改后的文件(覆盖原文件)
|
||
tree.write(file_path, encoding='utf-8', xml_declaration=True)
|
||
print(f"Processed: {file_path}")
|
||
except Exception as e:
|
||
print(f"Error processing {file_path}: {e}")
|
||
|
||
# 遍历临时目录下所有XML文件并处理
|
||
for root_dir, _, files in os.walk("temp-b-repo/out/assets"):
|
||
for file in files:
|
||
if file.endswith(".xml"):
|
||
file_path = os.path.join(root_dir, file)
|
||
remove_comments_from_xml(file_path)
|
||
EOF
|
||
|
||
# 运行Python脚本处理所有XML文件
|
||
python remove_xml_comments.py
|
||
|
||
echo "Files in temp directory after comment removal:"
|
||
ls -la temp-b-repo/out/assets
|
||
else
|
||
echo "Error: common/data/xml/assets directory not found!" && exit 1
|
||
fi
|
||
|
||
- 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
|
||
|
||
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."
|