3131 - name : Checkout branch
3232 uses : actions/checkout@v3
3333 with :
34+ # Check out the branch: use GITHUB_HEAD_REF if available (PR context),
35+ # otherwise fall back to GITHUB_REF.
3436 ref : ${{ github.head_ref || github.ref }}
3537
3638 - name : Set up Git user configuration
@@ -44,15 +46,29 @@ jobs:
4446 echo "Reverting commit: $LAST_COMMIT"
4547 git revert --no-edit $LAST_COMMIT
4648
47- - name : Push changes
48- env :
49- GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
49+ - name : Determine branch name
50+ id : branch
5051 run : |
51- # Determine branch using GITHUB_HEAD_REF if set (for PRs), else derive from GITHUB_REF
52+ # If running in a PR, GITHUB_HEAD_REF should be set.
5253 if [ -n "$GITHUB_HEAD_REF" ]; then
53- BRANCH="$GITHUB_HEAD_REF"
54+ echo "Using GITHUB_HEAD_REF: $GITHUB_HEAD_REF"
55+ echo "::set-output name=branch::$GITHUB_HEAD_REF"
5456 else
55- BRANCH="${GITHUB_REF#refs/heads/}"
57+ # Try to extract the branch from the event payload (for issue_comment events on PRs)
58+ BRANCH=$(jq -r '.pull_request.head.ref // .issue.pull_request.head.ref' "$GITHUB_EVENT_PATH")
59+ if [ "$BRANCH" = "null" ] || [ -z "$BRANCH" ]; then
60+ # Fallback: remove 'refs/heads/' from GITHUB_REF
61+ BRANCH="${GITHUB_REF#refs/heads/}"
62+ fi
63+ echo "Determined branch: $BRANCH"
64+ echo "::set-output name=branch::$BRANCH"
5665 fi
57- echo "Pushing changes to branch: $BRANCH"
58- git push origin HEAD:"$BRANCH"
66+ shell : bash
67+
68+ - name : Push changes
69+ env :
70+ GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
71+ run : |
72+ TARGET_BRANCH="${{ steps.branch.outputs.branch }}"
73+ echo "Pushing changes to branch: $TARGET_BRANCH"
74+ git push origin HEAD:"$TARGET_BRANCH"
0 commit comments