Instead of figuring out the commit after the last merge and rebasing on that with a ~1 suffix, just figure out the last merge commit directly and rebase on it. This way, if HEAD happens to be a merge commit, the rebase just succeeds immediately without blank variables or errors.
Explanation of the problem from #28497 (comment):
The problem is that the PR only contains a one commit after the last merge, so the job should be skipped, but the
pull_request.commits != 1
check is not smart enough to skip it because the PR is based on another PR and has merge ancestor commits. So specifically what happens is that after HEAD~ is checked out, the new HEAD is a merge commit, so the range$(git log --merges -1 --format=%H)..HEAD
is equivalent to HEAD..HEAD, which is empty, so theCOMMIT_AFTER_LAST_MERGE
variable is empty and the rebase command fails.
Note: In the current version of this PR, the “test each commit” job is skipped, because this PR only contains a single commit. But I manually verified the code works in earlier versions of the PR that included dummy commits.