name: Sync Assets to Private B Repo on: push: branches: [ main ] workflow_dispatch: # 支持手动触发 jobs: sync: runs-on: ubuntu-latest steps: # 步骤1:拉取 A 仓库代码(完整历史,确保所有文件可访问) - name: Checkout A Repo uses: actions/checkout@v4 with: fetch-depth: 0 # 拉取完整历史,避免浅克隆导致文件缺失 # 步骤2:准备同步文件(校验源目录并复制) - name: Prepare files for B Repo run: | # 创建临时目录存放待同步文件 mkdir -p temp-b-repo/out/assets # 校验源资产目录是否存在 if [ -d "common/data/xml/assets" ]; then echo "Found assets directory, starting copy..." # 复制所有文件(包括隐藏文件)到临时目录 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:" ls -la temp-b-repo/out/assets # 输出文件列表用于调试 else echo "Error: common/data/xml/assets directory not found!" && exit 1 fi # 步骤3:推送到私有 B 仓库(优化克隆逻辑和错误处理) - 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 身份 git config --global user.name "GitHub Actions" git config --global user.email "actions@github.com" # 清理旧目录(若存在),确保环境干净 rm -rf b-repo # 克隆 B 仓库(带错误捕获) echo "Cloning B repo..." git clone https://$B_REPO_TOKEN@${B_REPO_URL#https://} b-repo || { echo "Failed to clone B repository!"; exit 1; } # 进入 B 仓库目录并同步最新代码 cd b-repo echo "Checking out branch $B_BRANCH..." git checkout $B_BRANCH || { echo "Failed to checkout branch $B_BRANCH!"; exit 1; } echo "Pulling latest changes from $B_BRANCH..." git pull origin $B_BRANCH || { echo "Failed to pull latest changes!"; exit 1; } # 确保目标目录存在 mkdir -p out/assets # 复制临时目录的文件到 B 仓库(包括隐藏文件) echo "Copying assets to B repo..." 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 # 输出目标目录文件列表用于调试 # 检查是否有变更(仅关注 out/assets 目录) if git diff --quiet -- out/assets; then echo "No changes to sync to B repo. Exiting." else # 提交并推送变更 echo "Detected changes, pushing to B repo..." git add out/assets git commit -m "Sync assets: $(date +'%Y-%m-%d %H:%M:%S')" git push origin $B_BRANCH || { echo "Failed to push changes to B repo!"; exit 1; } echo "Sync completed successfully." fi