Skip to content

Commit

Permalink
Fix workflow trigger for backport pr creation (#2471) (#2472)
Browse files Browse the repository at this point in the history
Co-authored-by: Hiroto Funakoshi <hiroto.funakoshi.hiroto@gmail.com>
  • Loading branch information
vdaas-ci and hlts2 committed Apr 1, 2024
1 parent 1eb3a6f commit cf3be2e
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions .github/workflows/backport.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,20 @@
#
name: "Run backport PR"
on:
pull_request:
types:
- "closed"
push:
branches:
- main
- "release/v*.*"
- "!release/v*.*.*"
env:
TARGET_LABEL_NAME_PREFIX: "actions/backport/"
BACKPORT_BRANCH_NAME_PREFIX: "backport"
FETCHED_GITHUB_INFO_PATH: github_info.json
GITHUB_USER: ${{ secrets.DISPATCH_USER }}
GITHUB_TOKEN: ${{ secrets.DISPATCH_TOKEN }}
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
Expand All @@ -44,28 +48,33 @@ jobs:
gpg_private_key: ${{ secrets.GPG_PRIVATE_KEY }}
git_user_signingkey: true
git_commit_gpgsign: true
- name: Fetch PR info
run: |
gh pr list --limit 10 --json number,title,body,labels,headRefName,headRefOid,mergeCommit --state merged | \
jq --arg oid "${GITHUB_SHA}" '.[] | select(.mergeCommit.oid == $oid)' > ${FETCHED_GITHUB_INFO_PATH}
cat ${FETCHED_GITHUB_INFO_PATH}
echo ${GITHUB_SHA}
- 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(" ")'`
LABEL_NAMES=`cat ${FETCHED_GITHUB_INFO_PATH} | jq -r --arg PREFIX $TARGET_LABEL_NAME_PREFIX '[.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"`
PR_TITLE=`cat $FETCHED_GITHUB_INFO_PATH | jq -r ".title"`
PR_BODY=`cat $FETCHED_GITHUB_INFO_PATH | jq -r ".body"`
PR_NUM=`cat $FETCHED_GITHUB_INFO_PATH | jq -r ".number"`
PR_BRANCH_NAME=`cat $FETCHED_GITHUB_INFO_PATH | jq -r ".headRefName"`
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}
BACKPORT_BRANCH_NAME="${BACKPORT_BRANCH_NAME_PREFIX}/${BRANCH_NAME}/${PR_BRANCH_NAME}" # e.g) backport/release/vx.x/{current branch name}
echo "BRANCH_NAME=${BRANCH_NAME}"
echo "BACKPORT_BRANCH_NAME=${BACKPORT_BRANCH_NAME}"
Expand All @@ -75,7 +84,7 @@ jobs:
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 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} \
Expand Down

0 comments on commit cf3be2e

Please sign in to comment.