Skip to content

Commit c85b143

Browse files
committed
ci: add a workflow for backporting to other branches
To backport a PR, e.g. 1000, to another branch, e.g. `7.0.x`, add a label `backport 7.0.x` to the PR. This will trigger a workflow which will create a branch `backport-1000-to-7.0.x` based on the `7.0.x` branch with a cherry-pick of the PR's merge commit, and create a new PR for it against the `7.0.x` branch. It is very simplistic, for instance it doesn't handle cherry-pick failure gracefully, doesn't validate the state of the PR, doesn't check if the branch already exists, etc. But we can improve on it later as needed. Finally, PRs created by github actions do not themselves trigger further actions, i.e. the PR isn't checked. You need to close & reopen the PR for the checks to trigger. There are workarounds for this but they are either less secure or require more setup.
1 parent 0c8a54a commit c85b143

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

.github/workflows/backport.yml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
name: backport
2+
3+
on:
4+
pull_request:
5+
types: [labeled]
6+
7+
# Set permissions at the job level.
8+
permissions: {}
9+
10+
jobs:
11+
backport:
12+
if: ${{ startsWith(github.event.label.name, 'backport ') }}
13+
runs-on: ubuntu-latest
14+
permissions:
15+
contents: write
16+
pull-requests: write
17+
18+
steps:
19+
- uses: actions/checkout@v2
20+
with:
21+
fetch-depth: 0
22+
persist-credentials: true
23+
24+
- name: Create backport PR
25+
run: |
26+
set -eux
27+
28+
git config --global user.name "pytest bot"
29+
git config --global user.email "[email protected]"
30+
31+
label='${{ github.event.label.name }}'
32+
target_branch="${label#backport }"
33+
backport_branch=backport-${{ github.event.number }}-to-"${target_branch}"
34+
subject="[$target_branch] $(gh pr view --json title -q .title ${{ github.event.number }})"
35+
36+
git checkout origin/"${target_branch}" -b "${backport_branch}"
37+
git cherry-pick -x --mainline 1 ${{ github.event.pull_request.merge_commit_sha }}
38+
git commit --amend --message "$subject"
39+
git push --set-upstream origin --force-with-lease "${backport_branch}"
40+
gh pr create \
41+
--base "${target_branch}" \
42+
--title "${subject}" \
43+
--body "Backport of PR #${{ github.event.number }} to $target_branch branch. PR created by backport workflow."
44+
env:
45+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

CONTRIBUTING.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,13 @@ actual latest release). The procedure for this is:
391391
request, as described above. An exception to this is if the bug fix is not
392392
applicable to ``main`` anymore.
393393

394+
Automatic method:
395+
396+
Add a ``backport 1.2.x`` label to the PR you want to backport. This will create
397+
a backport PR against the ``1.2.x`` branch.
398+
399+
Manual method:
400+
394401
#. ``git checkout origin/1.2.x -b backport-XXXX`` # use the main PR number here
395402

396403
#. Locate the merge commit on the PR, in the *merged* message, for example:

0 commit comments

Comments
 (0)