|
| 1 | +name: Upstream Sync |
| 2 | + |
| 3 | +permissions: |
| 4 | + contents: write |
| 5 | + pull-requests: write # Required to create the PR |
| 6 | + |
| 7 | +env: |
| 8 | + UPSTREAM_REPO: KuekHaoYang/KVideo |
| 9 | + UPSTREAM_BRANCH: main |
| 10 | + TARGET_BRANCH: main |
| 11 | + SYNC_BRANCH: upstream-sync-conflict # The branch name used if a conflict occurs |
| 12 | + |
| 13 | +on: |
| 14 | + schedule: |
| 15 | + - cron: "0 */6 * * *" |
| 16 | + workflow_dispatch: |
| 17 | + |
| 18 | +jobs: |
| 19 | + sync_latest_from_upstream: |
| 20 | + name: Sync latest commits from upstream repo |
| 21 | + runs-on: ubuntu-latest |
| 22 | + if: ${{ github.event.repository.fork }} |
| 23 | + |
| 24 | + steps: |
| 25 | + - name: Checkout target repo |
| 26 | + uses: actions/checkout@v4 |
| 27 | + with: |
| 28 | + fetch-depth: 0 # Required to access full history for merging |
| 29 | + |
| 30 | + - name: Sync and Merge (with PR Fallback) |
| 31 | + env: |
| 32 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 33 | + run: | |
| 34 | + # 1. Configure Git |
| 35 | + git config user.name "github-actions[bot]" |
| 36 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 37 | +
|
| 38 | + # 2. Add Upstream Remote |
| 39 | + git remote add upstream "https://github.com/$UPSTREAM_REPO.git" |
| 40 | + git fetch upstream |
| 41 | +
|
| 42 | + # 3. Attempt Merge |
| 43 | + echo "Attempting to merge upstream/$UPSTREAM_BRANCH into $TARGET_BRANCH..." |
| 44 | + |
| 45 | + if git merge upstream/$UPSTREAM_BRANCH; then |
| 46 | + echo "✅ Merge successful. Pushing changes..." |
| 47 | + git push origin $TARGET_BRANCH |
| 48 | + else |
| 49 | + echo "⚠️ Merge conflict detected. Aborting merge and creating PR..." |
| 50 | + git merge --abort |
| 51 | +
|
| 52 | + # 4. Create a clean branch from upstream content |
| 53 | + git checkout -B $SYNC_BRANCH upstream/$UPSTREAM_BRANCH |
| 54 | + git push -f origin $SYNC_BRANCH |
| 55 | +
|
| 56 | + # 5. Create Pull Request using GitHub CLI |
| 57 | + # The '|| true' prevents failure if the PR already exists |
| 58 | + gh pr create \ |
| 59 | + --title "🚨 Manual Sync Required: Conflicts with Upstream" \ |
| 60 | + --body "The automatic sync failed due to merge conflicts. This PR contains the latest upstream changes. Please resolve conflicts and merge manually." \ |
| 61 | + --head $SYNC_BRANCH \ |
| 62 | + --base $TARGET_BRANCH \ |
| 63 | + || echo "PR already exists or could not be created." |
| 64 | + fi |
| 65 | +
|
| 66 | + - name: Delete workflow runs |
| 67 | + uses: Mattraks/delete-workflow-runs@v2 |
| 68 | + with: |
| 69 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 70 | + repository: ${{ github.repository }} |
| 71 | + retain_days: 1 |
| 72 | + keep_minimum_runs: 3 |
0 commit comments