|
| 1 | +name: Sync master to nicehash-v3 |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - master |
| 7 | + workflow_dispatch: |
| 8 | + |
| 9 | +jobs: |
| 10 | + sync-branch: |
| 11 | + runs-on: ubuntu-latest |
| 12 | + permissions: |
| 13 | + contents: write |
| 14 | + pull-requests: write |
| 15 | + |
| 16 | + steps: |
| 17 | + - name: Checkout repository |
| 18 | + uses: actions/checkout@v4 |
| 19 | + with: |
| 20 | + fetch-depth: 0 |
| 21 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 22 | + submodules: false |
| 23 | + |
| 24 | + - name: Configure Git |
| 25 | + run: | |
| 26 | + git config user.name "GitHub Actions Bot" |
| 27 | + git config user.email "actions@github.com" |
| 28 | +
|
| 29 | + - name: Attempt merge master -> nicehash-v3 |
| 30 | + id: merge |
| 31 | + run: | |
| 32 | + git fetch origin |
| 33 | +
|
| 34 | + if \! git ls-remote --exit-code --heads origin nicehash-v3; then |
| 35 | + echo "Branch nicehash-v3 does not exist, skipping sync" |
| 36 | + echo "merge_success=skipped" >> $GITHUB_OUTPUT |
| 37 | + exit 0 |
| 38 | + fi |
| 39 | +
|
| 40 | + git checkout nicehash-v3 |
| 41 | + git pull origin nicehash-v3 |
| 42 | +
|
| 43 | + if git merge origin/master --no-edit; then |
| 44 | + echo "merge_success=true" >> $GITHUB_OUTPUT |
| 45 | + git push origin nicehash-v3 |
| 46 | + echo "Successfully merged master into nicehash-v3" |
| 47 | + else |
| 48 | + echo "merge_success=false" >> $GITHUB_OUTPUT |
| 49 | + git merge --abort |
| 50 | + echo "Merge conflict detected" |
| 51 | + fi |
| 52 | +
|
| 53 | + - name: Create PR on conflict |
| 54 | + if: steps.merge.outputs.merge_success == 'false' |
| 55 | + uses: actions/github-script@v7 |
| 56 | + with: |
| 57 | + script: | |
| 58 | + const { owner, repo } = context.repo; |
| 59 | +
|
| 60 | + const existingPRs = await github.rest.pulls.list({ |
| 61 | + owner, |
| 62 | + repo, |
| 63 | + head: `${owner}:master`, |
| 64 | + base: 'nicehash-v3', |
| 65 | + state: 'open' |
| 66 | + }); |
| 67 | +
|
| 68 | + if (existingPRs.data.length > 0) { |
| 69 | + console.log('PR already exists:', existingPRs.data[0].html_url); |
| 70 | + await github.rest.issues.createComment({ |
| 71 | + owner, |
| 72 | + repo, |
| 73 | + issue_number: existingPRs.data[0].number, |
| 74 | + body: `New commits pushed to master. Resolve conflicts and merge. |
| 75 | +
|
| 76 | +Commit: ${context.sha}` |
| 77 | + }); |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + const pr = await github.rest.pulls.create({ |
| 82 | + owner, |
| 83 | + repo, |
| 84 | + title: 'Sync master to nicehash-v3 (conflicts)', |
| 85 | + head: 'master', |
| 86 | + base: 'nicehash-v3', |
| 87 | + body: `Automatic merge from master to nicehash-v3 has conflicts. |
| 88 | +
|
| 89 | +Resolve and merge manually. |
| 90 | +
|
| 91 | +Triggered by commit: ${context.sha}` |
| 92 | + }); |
| 93 | + |
| 94 | + console.log('Created PR:', pr.data.html_url); |
| 95 | + |
| 96 | + - name: Summary |
| 97 | + run: | |
| 98 | + echo "### Sync Result" >> $GITHUB_STEP_SUMMARY |
| 99 | + echo "- Branch: master -> nicehash-v3" >> $GITHUB_STEP_SUMMARY |
| 100 | + echo "- Status: ${{ steps.merge.outputs.merge_success }}" >> $GITHUB_STEP_SUMMARY |
0 commit comments