PR → Merge → Revert (demo) #5
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: PR → Merge → Revert (demo) | |
| on: | |
| workflow_dispatch: | |
| inputs: | |
| base: | |
| description: Base branch to target | |
| required: false | |
| default: main | |
| schedule: | |
| - cron: '30 1 * * 0' # Every Sunday at 07:00 IST | |
| permissions: | |
| contents: write | |
| pull-requests: write | |
| jobs: | |
| demo: | |
| runs-on: ubuntu-latest | |
| env: | |
| GH_TOKEN: ${{ secrets.PAT_TOKEN }} # PAT with repo:write | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| token: ${{ env.GH_TOKEN }} | |
| - name: Configure git user | |
| run: | | |
| git config user.name "github-actions[bot]" | |
| git config user.email "41898282+github-actions[bot]@users.noreply.github.com" | |
| - name: Create a feature branch and commit | |
| id: make_change | |
| run: | | |
| BASE="${{ github.event.inputs.base }}" | |
| : "${BASE:=main}" # fallback to main if empty | |
| BRANCH="feat-auto-${{ github.run_id }}" | |
| git checkout -B "$BRANCH" "origin/${BASE}" | |
| echo "Hello from run ${{ github.run_id }} at $(date -u +"%Y-%m-%dT%H:%M:%SZ")" >> demo.txt | |
| git add demo.txt | |
| git commit -m "demo: automated change" | |
| git push -u origin "$BRANCH" | |
| echo "branch=$BRANCH" >> $GITHUB_OUTPUT | |
| - name: Create PR (REST) | |
| id: create_pr | |
| uses: actions/github-script@v7 | |
| with: | |
| github-token: ${{ env.GH_TOKEN }} | |
| script: | | |
| const base = core.getInput('base') || 'main'; | |
| const head = "${{ steps.make_change.outputs.branch }}"; | |
| const { owner, repo } = context.repo; | |
| const pr = await github.rest.pulls.create({ | |
| owner, repo, | |
| title: `Auto PR for run #${context.runNumber}`, | |
| head, base, | |
| body: "Automated demo PR created by workflow." | |
| }); | |
| core.setOutput('number', pr.data.number); | |
| core.setOutput('html_url', pr.data.html_url); | |
| - name: Merge PR (REST, merge commit) | |
| id: merge_pr | |
| uses: actions/github-script@v7 | |
| with: | |
| github-token: ${{ env.GH_TOKEN }} | |
| script: | | |
| const { owner, repo } = context.repo; | |
| const pull_number = Number("${{ steps.create_pr.outputs.number }}"); | |
| const res = await github.rest.pulls.merge({ | |
| owner, repo, pull_number, | |
| merge_method: "merge", | |
| commit_title: `Merge auto PR #${pull_number}` | |
| }); | |
| core.setOutput('merge_sha', res.data.sha); | |
| - name: Create revert branch and revert the merge commit | |
| id: revert_commit | |
| env: | |
| MERGE_SHA: ${{ steps.merge_pr.outputs.merge_sha }} | |
| PR_NUM: ${{ steps.create_pr.outputs.number }} | |
| run: | | |
| BASE="${{ github.event.inputs.base }}" | |
| : "${BASE:=main}" # fallback to main if empty | |
| REVERT_BRANCH="revert-pr-${PR_NUM}" | |
| git fetch origin "${BASE}" | |
| git checkout -B "$REVERT_BRANCH" "origin/${BASE}" | |
| git revert -m 1 "$MERGE_SHA" --no-edit | |
| git push -u origin "$REVERT_BRANCH" | |
| echo "branch=$REVERT_BRANCH" >> $GITHUB_OUTPUT | |
| - name: Open revert PR (REST) | |
| id: create_revert_pr | |
| uses: actions/github-script@v7 | |
| with: | |
| github-token: ${{ env.GH_TOKEN }} | |
| script: | | |
| const { owner, repo } = context.repo; | |
| const base = core.getInput('base') || 'main'; | |
| const head = "${{ steps.revert_commit.outputs.branch }}"; | |
| const pr = await github.rest.pulls.create({ | |
| owner, repo, | |
| title: `Revert "Auto PR for run #${context.runNumber}"`, | |
| head, base, | |
| body: `This reverts merge commit \`${{ steps.merge_pr.outputs.merge_sha }}\`.` | |
| }); | |
| core.setOutput('number', pr.data.number); | |
| core.setOutput('html_url', pr.data.html_url); | |
| - name: Merge revert PR (REST, merge commit) | |
| id: merge_revert_pr | |
| uses: actions/github-script@v7 | |
| with: | |
| github-token: ${{ env.GH_TOKEN }} | |
| script: | | |
| const { owner, repo } = context.repo; | |
| const pull_number = Number("${{ steps.create_revert_pr.outputs.number }}"); | |
| const res = await github.rest.pulls.merge({ | |
| owner, repo, pull_number, | |
| merge_method: "merge", | |
| commit_title: `Merge revert PR #${pull_number}` | |
| }); | |
| core.setOutput('merge_sha', res.data.sha); | |
| - name: Summarize | |
| run: | | |
| echo "Opened PR: ${{ steps.create_pr.outputs.html_url }}" | |
| echo "Merged SHA: ${{ steps.merge_pr.outputs.merge_sha }}" | |
| echo "Revert PR: ${{ steps.create_revert_pr.outputs.html_url }}" | |
| echo "Merged revert SHA:${{ steps.merge_revert_pr.outputs.merge_sha }}" |