-
Notifications
You must be signed in to change notification settings - Fork 78
84 lines (78 loc) · 3.4 KB
/
backport.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#
# Copyright (C) 2019-2024 vdaas.org vald team <[email protected]>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# You may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
name: "Run backport PR"
on:
pull_request:
types:
- "closed"
env:
TARGET_LABEL_NAME_PREFIX: "actions/backport/"
BACKPORT_BRANCH_NAME_PREFIX: "backport"
jobs:
dump-contexts-to-log:
runs-on: ubuntu-latest
if: ${{ github.event.pull_request.merged == true }}
steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/dump-context
create:
runs-on: ubuntu-latest
needs: [dump-contexts-to-log]
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.DISPATCH_TOKEN }}
- name: Set Git config
run: |
git config --global --add safe.directory ${GITHUB_WORKSPACE}
- uses: crazy-max/ghaction-import-gpg@v6
with:
gpg_private_key: ${{ secrets.GPG_PRIVATE_KEY }}
git_user_signingkey: true
git_commit_gpgsign: true
- name: Set context
id: set_context
run: |
LABEL_NAMES=`cat ${GITHUB_EVENT_PATH} | jq -r --arg PREFIX $TARGET_LABEL_NAME_PREFIX '[.pull_request.labels[]? | select(.name | startswith($PREFIX)) | .name] | join(" ")'`
echo "LABEL_NAMES=${LABEL_NAMES}" >> $GITHUB_OUTPUT # e.g.) actions/backport/v1.7 actions/backport/v1.8
echo "${LABEL_NAMES}"
- name: Create PR
if: ${{ steps.set_context.outputs.LABEL_NAMES != '' }}
env:
LABEL_NAMES: ${{ steps.set_context.outputs.LABEL_NAMES }}
GITHUB_USER: ${{ secrets.DISPATCH_USER }}
GITHUB_TOKEN: ${{ secrets.DISPATCH_TOKEN }}
run: |
PR_TITLE=`cat $GITHUB_EVENT_PATH | jq -r ".pull_request.title"`
PR_BODY=`cat $GITHUB_EVENT_PATH | jq -r ".pull_request.body"`
PR_NUM=`cat $GITHUB_EVENT_PATH | jq -r ".pull_request.number"`
echo "${PR_NUM} ${PR_TITLE}: ${PR_BODY}"
for LABEL_NAME in ${LABEL_NAMES}; do
BRANCH_NAME=`echo "${LABEL_NAME}" | sed -e "s:^${TARGET_LABEL_NAME_PREFIX}::"` # e.g) release/vx.x, main
BACKPORT_BRANCH_NAME="${BACKPORT_BRANCH_NAME_PREFIX}/${BRANCH_NAME}/${GITHUB_HEAD_REF}" # e.g) backport/release/vx.x/{current branch name}
echo "BRANCH_NAME=${BRANCH_NAME}"
echo "BACKPORT_BRANCH_NAME=${BACKPORT_BRANCH_NAME}"
echo "SHA=${GITHUB_SHA}"
git checkout ${BRANCH_NAME}
git checkout -b ${BACKPORT_BRANCH_NAME}
# Force cherry-pick. The conflicts will be modified within the backport PR.
git cherry-pick $GITHUB_SHA || (git add -A && git cherry-pick --continue --no-edit)
git push origin ${BACKPORT_BRANCH_NAME}
gh pr create --base ${BRANCH_NAME} \
--title "Backport PR #${PR_NUM} to ${BRANCH_NAME} for ${PR_TITLE}" \
--body "${PR_BODY}"
done