自动同步上游仓库 #51
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: 自动同步上游仓库 | |
| on: | |
| schedule: | |
| # 每天凌晨2点执行(UTC时间,对应北京时间10点) | |
| - cron: '0 2 * * *' | |
| # 允许手动触发 | |
| workflow_dispatch: | |
| jobs: | |
| sync-upstream: | |
| runs-on: ubuntu-latest | |
| name: 同步上游仓库更新 | |
| # 仅在Fork仓库中运行此工作流 | |
| if: github.repository != 'chiupam/tieba' | |
| steps: | |
| - name: 检出代码 | |
| uses: actions/checkout@v3 | |
| with: | |
| fetch-depth: 0 | |
| # 使用默认的GITHUB_TOKEN,它对当前仓库有写权限 | |
| token: ${{ github.token }} | |
| - name: 配置Git | |
| run: | | |
| git config --global user.name 'github-actions[bot]' | |
| git config --global user.email 'github-actions[bot]@users.noreply.github.com' | |
| - name: 添加上游仓库 | |
| run: | | |
| # 添加chiupam/tieba作为上游仓库 | |
| git remote add upstream https://github.com/chiupam/tieba.git | |
| git remote -v | |
| - name: 获取上游更新 | |
| run: | | |
| git fetch upstream | |
| echo "✅ 成功获取上游仓库的最新代码" | |
| - name: 备份需要保留的文件 | |
| run: | | |
| if [ -f "last_activity.md" ]; then | |
| echo "📦 备份本地的 last_activity.md 文件" | |
| cp last_activity.md last_activity.md.backup | |
| fi | |
| - name: 合并上游变更 | |
| run: | | |
| # 检查当前分支 | |
| CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD) | |
| echo "当前分支: $CURRENT_BRANCH" | |
| # 执行合并 | |
| if git merge upstream/$CURRENT_BRANCH --no-edit; then | |
| echo "✅ 已成功合并上游仓库的变更" | |
| else | |
| echo "⚠️ 合并过程中可能存在冲突,尝试使用策略解决..." | |
| # 尝试使用theirs策略解决冲突(接受上游更改) | |
| git merge --abort | |
| git merge upstream/$CURRENT_BRANCH -X theirs --no-edit | |
| echo "⚠️ 使用theirs合并策略完成,如有异常请手动检查" | |
| fi | |
| - name: 恢复需要保留的文件 | |
| run: | | |
| if [ -f "last_activity.md.backup" ]; then | |
| echo "📦 恢复本地的 last_activity.md 文件" | |
| mv last_activity.md.backup last_activity.md | |
| git add last_activity.md | |
| git commit -m "保留本地 last_activity.md 文件" || echo "没有需要提交的更改" | |
| fi | |
| - name: 推送更新 | |
| run: | | |
| # 推送到当前分支 | |
| CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD) | |
| # 只推送到Fork的仓库,不尝试推送到上游仓库 | |
| if git push origin $CURRENT_BRANCH; then | |
| echo "✅ 已将更新推送到您的仓库" | |
| else | |
| echo "❌ 推送失败,可能需要手动检查" | |
| exit 1 | |
| fi | |
| - name: 同步完成 | |
| run: | | |
| echo "🎉 您的仓库已与上游仓库同步完成!" | |
| echo "📝 最新提交信息:" | |
| git log -1 --pretty=format:"%h - %an: %s" |