Skip to content

Commit d36a9f0

Browse files
Add workflow to backport PRs to another branch (#12964)
The workflow can be triggered by creating a comment on a merged PR: /backport <TARGET_BRANCH> The backport can only be triggered by people with write access to the repository. Co-authored-by: AbsurdlyLongUsername <22662897+absurdlylongusername@users.noreply.github.com>
1 parent 2704c20 commit d36a9f0

1 file changed

Lines changed: 46 additions & 0 deletions

File tree

.github/workflows/backport-pr.yml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
name: Backport merged pull request
2+
on:
3+
issue_comment:
4+
types: [created]
5+
permissions:
6+
contents: write # for comment creation on original PR
7+
pull-requests: write
8+
jobs:
9+
backport:
10+
name: Backport pull request
11+
runs-on: ubuntu-latest
12+
13+
# Only run when the comment starts with the `/backport` command on a PR and
14+
# the commenter has write access to the repository. We do not want to allow
15+
# everybody to trigger backports and create branches in our repository.
16+
if: >
17+
github.event.issue.pull_request &&
18+
startsWith(github.event.comment.body, '/backport ') &&
19+
(
20+
github.event.comment.author_association == 'OWNER' ||
21+
github.event.comment.author_association == 'COLLABORATOR' ||
22+
github.event.comment.author_association == 'MEMBER'
23+
)
24+
steps:
25+
- uses: actions/checkout@v4
26+
- name: Get backport metadata
27+
# the target branch is the first argument after `/backport`
28+
run: |
29+
set -euo pipefail
30+
body="${{ github.event.comment.body }}"
31+
32+
line=${body%%$'\n'*} # Get the first line
33+
if [[ $line =~ ^/backport[[:space:]]+([^[:space:]]+) ]]; then
34+
echo "BACKPORT_TARGET=${BASH_REMATCH[1]}" >> "$GITHUB_ENV"
35+
else
36+
echo "Usage: /backport <target-branch>" >&2
37+
exit 1
38+
fi
39+
40+
- name: Create backport pull request
41+
uses: korthout/backport-action@v4
42+
with:
43+
add_labels: 'backport'
44+
copy_labels_pattern: '.*'
45+
label_pattern: ''
46+
target_branches: ${{ env.BACKPORT_TARGET }}

0 commit comments

Comments
 (0)