|
| 1 | +name: Auto Rebase Develop |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - master |
| 7 | + workflow_dispatch: |
| 8 | + |
| 9 | +permissions: |
| 10 | + contents: write |
| 11 | + pull-requests: write |
| 12 | + issues: write |
| 13 | + |
| 14 | +jobs: |
| 15 | + rebase: |
| 16 | + runs-on: ubuntu-latest |
| 17 | + timeout-minutes: 10 |
| 18 | + |
| 19 | + steps: |
| 20 | + - name: Checkout repository |
| 21 | + uses: actions/checkout@v4 |
| 22 | + with: |
| 23 | + fetch-depth: 0 |
| 24 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 25 | + |
| 26 | + - name: Configure Git |
| 27 | + run: | |
| 28 | + git config --global user.name "github-actions[bot]" |
| 29 | + git config --global user.email "github-actions[bot]@users.noreply.github.com" |
| 30 | +
|
| 31 | + - name: Check if develop branch exists |
| 32 | + id: check-develop |
| 33 | + run: | |
| 34 | + if git ls-remote --heads origin feat/develop | grep -q feat/develop; then |
| 35 | + echo "exists=true" >> $GITHUB_OUTPUT |
| 36 | + echo "Develop branch exists" |
| 37 | + else |
| 38 | + echo "exists=false" >> $GITHUB_OUTPUT |
| 39 | + echo "Develop branch does not exist" |
| 40 | + fi |
| 41 | +
|
| 42 | + - name: Fetch latest changes |
| 43 | + if: steps.check-develop.outputs.exists == 'true' |
| 44 | + run: | |
| 45 | + git fetch origin master |
| 46 | + git fetch origin feat/develop |
| 47 | +
|
| 48 | + - name: Check if rebase is needed |
| 49 | + if: steps.check-develop.outputs.exists == 'true' |
| 50 | + id: check-rebase |
| 51 | + run: | |
| 52 | + # Get the commit hashes |
| 53 | + master_commit=$(git rev-parse origin/master) |
| 54 | + develop_commit=$(git rev-parse origin/feat/develop) |
| 55 | +
|
| 56 | + # Check if develop is already up to date with master |
| 57 | + merge_base=$(git merge-base origin/master origin/feat/develop) |
| 58 | +
|
| 59 | + if [ "$merge_base" = "$master_commit" ]; then |
| 60 | + echo "needed=false" >> $GITHUB_OUTPUT |
| 61 | + echo "Develop branch is already up to date with master" |
| 62 | + else |
| 63 | + echo "needed=true" >> $GITHUB_OUTPUT |
| 64 | + echo "Develop branch needs to be rebased with master" |
| 65 | + echo "master commit: $master_commit" |
| 66 | + echo "Develop commit: $develop_commit" |
| 67 | + echo "Merge base: $merge_base" |
| 68 | + fi |
| 69 | +
|
| 70 | + - name: Checkout develop branch |
| 71 | + if: steps.check-develop.outputs.exists == 'true' && steps.check-rebase.outputs.needed == 'true' |
| 72 | + run: | |
| 73 | + git checkout feat/develop |
| 74 | + git reset --hard origin/feat/develop |
| 75 | +
|
| 76 | + - name: Attempt rebase |
| 77 | + if: steps.check-develop.outputs.exists == 'true' && steps.check-rebase.outputs.needed == 'true' |
| 78 | + id: rebase |
| 79 | + run: | |
| 80 | + echo "Starting rebase of develop with master..." |
| 81 | +
|
| 82 | + # Attempt the rebase |
| 83 | + if git rebase origin/master; then |
| 84 | + echo "success=true" >> $GITHUB_OUTPUT |
| 85 | + echo "Rebase completed successfully" |
| 86 | + else |
| 87 | + echo "success=false" >> $GITHUB_OUTPUT |
| 88 | + echo "Rebase failed due to conflicts" |
| 89 | +
|
| 90 | + # Abort the rebase |
| 91 | + git rebase --abort |
| 92 | +
|
| 93 | + # Get list of conflicting files for the issue |
| 94 | + git checkout feat/develop |
| 95 | + git merge origin/master --no-commit --no-ff || true |
| 96 | + conflicts=$(git diff --name-only --diff-filter=U | tr '\n' ',' | sed 's/,$//') |
| 97 | + echo "conflicts=$conflicts" >> $GITHUB_OUTPUT |
| 98 | +
|
| 99 | + # Reset to clean state |
| 100 | + git merge --abort || true |
| 101 | + git reset --hard origin/feat/develop |
| 102 | + fi |
| 103 | +
|
| 104 | + - name: Push rebased develop branch |
| 105 | + if: steps.check-develop.outputs.exists == 'true' && steps.check-rebase.outputs.needed == 'true' && steps.rebase.outputs.success == 'true' |
| 106 | + run: | |
| 107 | + echo "Pushing rebased develop branch..." |
| 108 | + git push origin feat/develop --force-with-lease |
| 109 | + echo "Successfully pushed rebased develop branch" |
| 110 | +
|
| 111 | + - name: Create issue for manual resolution |
| 112 | + if: steps.check-develop.outputs.exists == 'true' && steps.check-rebase.outputs.needed == 'true' && steps.rebase.outputs.success == 'false' |
| 113 | + uses: actions/github-script@v7 |
| 114 | + with: |
| 115 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 116 | + script: | |
| 117 | + const conflicts = '${{ steps.rebase.outputs.conflicts }}'; |
| 118 | + const conflictsList = conflicts ? conflicts.split(',').map(file => `- \`${file}\``).join('\n') : 'Unknown files'; |
| 119 | +
|
| 120 | + const issueBody = `## Auto Rebase Failed |
| 121 | +
|
| 122 | + The automatic rebase of the \`feat/develop\` branch with \`master\` has failed due to merge conflicts. |
| 123 | +
|
| 124 | + ### Conflicting Files: |
| 125 | + ${conflictsList} |
| 126 | +
|
| 127 | + ### Manual Resolution Required: |
| 128 | +
|
| 129 | + 1. Checkout the develop branch locally: |
| 130 | + \`\`\`bash |
| 131 | + git checkout feat/develop |
| 132 | + git pull origin feat/develop |
| 133 | + \`\`\` |
| 134 | +
|
| 135 | + 2. Rebase with master: |
| 136 | + \`\`\`bash |
| 137 | + git fetch origin master |
| 138 | + git rebase origin/master |
| 139 | + \`\`\` |
| 140 | +
|
| 141 | + 3. Resolve conflicts in the listed files above |
| 142 | +
|
| 143 | + 4. Continue the rebase: |
| 144 | + \`\`\`bash |
| 145 | + git add . |
| 146 | + git rebase --continue |
| 147 | + \`\`\` |
| 148 | +
|
| 149 | + 5. Force push the resolved branch: |
| 150 | + \`\`\`bash |
| 151 | + git push origin feat/develop --force-with-lease |
| 152 | + \`\`\` |
| 153 | +
|
| 154 | + ### Triggered by: |
| 155 | + - **Commit**: ${{ github.sha }} |
| 156 | + - **Author**: ${{ github.actor }} |
| 157 | + - **Workflow**: ${{ github.workflow }} |
| 158 | +
|
| 159 | + This issue will be automatically closed when the next successful rebase occurs. |
| 160 | + `; |
| 161 | +
|
| 162 | + // Check if there's already an open issue for rebase conflicts |
| 163 | + const existingIssues = await github.rest.issues.listForRepo({ |
| 164 | + owner: context.repo.owner, |
| 165 | + repo: context.repo.repo, |
| 166 | + state: 'open', |
| 167 | + labels: 'auto-rebase-conflict' |
| 168 | + }); |
| 169 | +
|
| 170 | + if (existingIssues.data.length === 0) { |
| 171 | + await github.rest.issues.create({ |
| 172 | + owner: context.repo.owner, |
| 173 | + repo: context.repo.repo, |
| 174 | + title: 'Auto Rebase Failed: Manual Resolution Required', |
| 175 | + body: issueBody, |
| 176 | + labels: ['auto-rebase-conflict', 'needs-attention'] |
| 177 | + }); |
| 178 | +
|
| 179 | + console.log('Created issue for manual conflict resolution'); |
| 180 | + } else { |
| 181 | + console.log('Issue for rebase conflicts already exists'); |
| 182 | + } |
| 183 | +
|
| 184 | + - name: Close resolved rebase issues |
| 185 | + if: steps.check-develop.outputs.exists == 'true' && steps.check-rebase.outputs.needed == 'true' && steps.rebase.outputs.success == 'true' |
| 186 | + uses: actions/github-script@v7 |
| 187 | + with: |
| 188 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 189 | + script: | |
| 190 | + // Close any open rebase conflict issues |
| 191 | + const existingIssues = await github.rest.issues.listForRepo({ |
| 192 | + owner: context.repo.owner, |
| 193 | + repo: context.repo.repo, |
| 194 | + state: 'open', |
| 195 | + labels: 'auto-rebase-conflict' |
| 196 | + }); |
| 197 | +
|
| 198 | + for (const issue of existingIssues.data) { |
| 199 | + await github.rest.issues.createComment({ |
| 200 | + owner: context.repo.owner, |
| 201 | + repo: context.repo.repo, |
| 202 | + issue_number: issue.number, |
| 203 | + body: 'Auto rebase has been successfully completed. Closing this issue.' |
| 204 | + }); |
| 205 | +
|
| 206 | + await github.rest.issues.update({ |
| 207 | + owner: context.repo.owner, |
| 208 | + repo: context.repo.repo, |
| 209 | + issue_number: issue.number, |
| 210 | + state: 'closed' |
| 211 | + }); |
| 212 | +
|
| 213 | + console.log(`Closed issue #${issue.number}`); |
| 214 | + } |
| 215 | +
|
| 216 | + - name: Summary |
| 217 | + if: always() |
| 218 | + run: | |
| 219 | + echo "## Auto Rebase Summary" |
| 220 | + echo "- **Develop branch exists**: ${{ steps.check-develop.outputs.exists }}" |
| 221 | + if [ "${{ steps.check-develop.outputs.exists }}" = "true" ]; then |
| 222 | + echo "- **Rebase needed**: ${{ steps.check-rebase.outputs.needed }}" |
| 223 | + if [ "${{ steps.check-rebase.outputs.needed }}" = "true" ]; then |
| 224 | + echo "- **Rebase successful**: ${{ steps.rebase.outputs.success }}" |
| 225 | + fi |
| 226 | + fi |
0 commit comments