diff --git a/.github/workflows/optimize-assets.yml b/.github/workflows/optimize-assets.yml index eb27b6cee2f..3ec3000096f 100644 --- a/.github/workflows/optimize-assets.yml +++ b/.github/workflows/optimize-assets.yml @@ -1,27 +1,44 @@ name: Optimize Assets on: - workflow_dispatch: # 允许手动触发 + workflow_dispatch: # 手动触发 + schedule: + - cron: '0 3 * * *' # 每天凌晨3点执行 + +env: + FFMPEG_VERSION: '6.1' jobs: optimize: runs-on: ubuntu-latest + timeout-minutes: 30 # 设置任务超时时间 steps: - - uses: actions/checkout@v3 + # 添加仓库缓存 + - name: Cache Git Repository + id: cache-repo + uses: actions/cache@v4 with: - fetch-depth: 0 - token: ${{ secrets.FORK_SYNC_TOKEN }} + path: .git + key: git-repo-${{ github.sha }} + restore-keys: | + git-repo- + + - uses: actions/checkout@v4 + with: + fetch-depth: 0 # 获取完整历史以确保正确同步 + token: ${{ github.token }} # 使用默认的 GITHUB_TOKEN - name: Sync with upstream id: sync - continue-on-error: true run: | git config --local user.email "github-actions[bot]@users.noreply.github.com" git config --local user.name "github-actions[bot]" - # 添加上游仓库 - git remote add upstream https://github.com/pagefaultgames/pokerogue.git + # 检查并添加上游仓库 + if ! git remote | grep -q '^upstream$'; then + git remote add upstream https://github.com/pagefaultgames/pokerogue.git + fi # 获取上游更新 git fetch upstream main @@ -30,39 +47,35 @@ jobs: if git rev-list HEAD..upstream/main --count | grep -q "^0$"; then echo "sync_status=uptodate" >> $GITHUB_OUTPUT echo "仓库已是最新,无需同步" - else - # 合并上游更新 - if git merge upstream/main --no-edit; then - echo "sync_status=success" >> $GITHUB_OUTPUT - git push - else - echo "sync_status=failed" >> $GITHUB_OUTPUT - exit 1 - fi + exit 0 fi + + # 重置到上游状态 + git reset --hard upstream/main + + # 强制推送更新 + git push -f + echo "sync_status=updated" >> $GITHUB_OUTPUT - - name: Check sync status - if: steps.sync.outputs.sync_status == 'failed' - run: | - echo "同步上游仓库失败,请手动解决冲突" - exit 1 - + # 只有当仓库不是最新且同步成功时,才继续执行后续步骤 - name: Setup Node.js - uses: actions/setup-node@v3 + if: steps.sync.outputs.sync_status == 'updated' + uses: actions/setup-node@v4 with: node-version: '18' cache: 'npm' # 优化 FFmpeg 缓存 - name: Cache FFmpeg + if: steps.sync.outputs.sync_status == 'updated' id: cache-ffmpeg - uses: actions/cache@v3 + uses: actions/cache@v4 with: path: ~/ffmpeg - key: ${{ runner.os }}-ffmpeg-static-6.1 + key: ${{ runner.os }}-ffmpeg-static-${{ env.FFMPEG_VERSION }} - name: Install FFmpeg - if: steps.cache-ffmpeg.outputs.cache-hit != 'true' + if: steps.sync.outputs.sync_status == 'updated' && steps.cache-ffmpeg.outputs.cache-hit != 'true' run: | mkdir -p ~/ffmpeg wget -q https://johnvansickle.com/ffmpeg/releases/ffmpeg-release-amd64-static.tar.xz @@ -72,12 +85,14 @@ jobs: rm -rf ffmpeg-*-amd64-static* - name: Add FFmpeg to PATH + if: steps.sync.outputs.sync_status == 'updated' run: echo "$HOME/ffmpeg" >> $GITHUB_PATH # 添加 node_modules 缓存 - name: Cache node_modules + if: steps.sync.outputs.sync_status == 'updated' id: cache-node-modules - uses: actions/cache@v3 + uses: actions/cache@v4 with: path: node_modules key: ${{ runner.os }}-node-modules-${{ hashFiles('package-lock.json') }} @@ -85,20 +100,27 @@ jobs: ${{ runner.os }}-node-modules- - name: Install dependencies - if: steps.cache-node-modules.outputs.cache-hit != 'true' + if: steps.sync.outputs.sync_status == 'updated' && steps.cache-node-modules.outputs.cache-hit != 'true' + run: npm ci + + - name: Optimize Assets + if: steps.sync.outputs.sync_status == 'updated' + id: optimize_assets + continue-on-error: true run: | - npm install sharp glob + node scripts/optimize-images.js & + node scripts/optimize-audio.js & + node scripts/optimize-json.js & + wait - - name: Optimize Images - run: node scripts/optimize-images.js - - - name: Optimize Audio - run: node scripts/optimize-audio.js - - - name: Optimize JSON - run: node scripts/optimize-json.js + - name: Check Optimization Status + if: steps.sync.outputs.sync_status == 'updated' && steps.optimize_assets.outcome == 'failure' + run: | + echo "资源优化过程中出现错误,请检查日志" + exit 1 - name: Check for changes + if: steps.sync.outputs.sync_status == 'updated' id: check_changes run: | if [[ -n "$(git status --porcelain)" ]]; then @@ -108,10 +130,23 @@ jobs: fi - name: Commit and push if changed - if: steps.check_changes.outputs.changes == 'true' && steps.sync.outputs.sync_status == 'success' + if: steps.sync.outputs.sync_status == 'updated' && steps.check_changes.outputs.changes == 'true' run: | git config --local user.email "github-actions[bot]@users.noreply.github.com" git config --local user.name "github-actions[bot]" + + # 获取更改的文件统计 + IMAGES_CHANGED=$(git diff --name-only | grep -E "\.(png|jpg|jpeg|gif|webp)$" | wc -l) + AUDIO_CHANGED=$(git diff --name-only | grep -E "\.(mp3|wav|ogg)$" | wc -l) + JSON_CHANGED=$(git diff --name-only | grep -E "\.json$" | wc -l) + + # 构建提交信息 + COMMIT_MSG="🤖 自动优化资源\n\n" + COMMIT_MSG+="优化统计:\n" + COMMIT_MSG+="- 图片文件: ${IMAGES_CHANGED}个\n" + COMMIT_MSG+="- 音频文件: ${AUDIO_CHANGED}个\n" + COMMIT_MSG+="- JSON文件: ${JSON_CHANGED}个" + git add -A - git commit -m "Automatically optimize assets" + git commit -m "$COMMIT_MSG" git push origin HEAD:test -f \ No newline at end of file