Skip to content

Cherry-pick Commit to Branch #12

Cherry-pick Commit to Branch

Cherry-pick Commit to Branch #12

---
name: Cherry-pick Commit to Branch
'on':
workflow_dispatch:
inputs:
commit_id:
description: 'Commit SHA or ID to cherry-pick'
required: true
type: string
target_branch:
description: 'Target branch name to cherry-pick into'
required: true
type: string
create_pr:
description: 'Create a PR instead of direct cherry-pick'
required: false
type: boolean
default: false
permissions:
contents: write
pull-requests: write
jobs:
cherry-pick:
runs-on: ubuntu-latest
steps:
- name: Install GitHub CLI
run: |
sudo apt update
sudo apt install -y gh
- name: Authenticate GitHub CLI
run: echo "${{ secrets.GITHUB_TOKEN }}" | gh auth login --with-token
- name: Clone repository
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh auth setup-git
gh repo clone "${{ github.repository }}" repo
cd repo
git fetch origin
- name: Configure Git
working-directory: ./repo
run: |
git config user.name "React-Native-Windows Bot"
git config user.email "53619745+rnbot@users.noreply.github.com"
- name: Checkout target branch
working-directory: ./repo
run: |
git checkout "${{ github.event.inputs.target_branch }}"
git pull origin "${{ github.event.inputs.target_branch }}"
- name: Create cherry-pick branch (if creating PR)
if: github.event.inputs.create_pr == 'true'
working-directory: ./repo
run: |
COMMIT_ID="${{ github.event.inputs.commit_id }}"
SHORT_COMMIT_ID="${COMMIT_ID:0:8}"
BRANCH_NAME="cherry-pick-$SHORT_COMMIT_ID-to-${{ github.event.inputs.target_branch }}"
git checkout -b "$BRANCH_NAME"
echo "CHERRY_PICK_BRANCH=$BRANCH_NAME" >> $GITHUB_ENV
- name: Cherry-pick commit
working-directory: ./repo
run: |
COMMIT_ID="${{ github.event.inputs.commit_id }}"
TARGET_BRANCH="${{ github.event.inputs.target_branch }}"
echo "🍒 Cherry-picking commit $COMMIT_ID into branch $TARGET_BRANCH"
if git cherry-pick "$COMMIT_ID"; then
echo "✅ Cherry-pick successful"
else
echo "❌ Cherry-pick failed with conflicts"
echo "Conflict details:"
git status
exit 1
fi
- name: Push changes directly to target branch
if: github.event.inputs.create_pr == 'false'
working-directory: ./repo
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
COMMIT_ID="${{ github.event.inputs.commit_id }}"
TARGET_BRANCH="${{ github.event.inputs.target_branch }}"
echo "📤 Pushing cherry-picked commit directly to $TARGET_BRANCH"
REPO_URL="https://x-access-token:${GH_TOKEN}@github.com"
REPO_URL="${REPO_URL}/${{ github.repository }}.git"
git push "$REPO_URL" "$TARGET_BRANCH"
echo "✅ Successfully cherry-picked $COMMIT_ID to $TARGET_BRANCH"
- name: Push cherry-pick branch and create PR
if: github.event.inputs.create_pr == 'true'
working-directory: ./repo
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
COMMIT_ID="${{ github.event.inputs.commit_id }}"
TARGET_BRANCH="${{ github.event.inputs.target_branch }}"
BRANCH_NAME="${CHERRY_PICK_BRANCH}"
echo "📤 Pushing cherry-pick branch $BRANCH_NAME"
REPO_URL="https://x-access-token:${GH_TOKEN}@github.com"
REPO_URL="${REPO_URL}/${{ github.repository }}.git"
git push "$REPO_URL" "$BRANCH_NAME"
echo "📋 Creating Pull Request"
ORIGINAL_COMMIT_MSG=$(git log --format=%s -n 1 "$COMMIT_ID")
PR_TITLE="Cherry-pick $COMMIT_ID to $TARGET_BRANCH: $ORIGINAL_COMMIT_MSG"
PR_BODY="This PR cherry-picks commit $COMMIT_ID to the $TARGET_BRANCH branch.
**Original commit:** $COMMIT_ID
**Target branch:** $TARGET_BRANCH
**Original commit message:** $ORIGINAL_COMMIT_MSG
Please review the changes before merging."
gh pr create \
--title "$PR_TITLE" \
--body "$PR_BODY" \
--base "$TARGET_BRANCH" \
--head "$BRANCH_NAME"
echo "✅ Successfully created PR for cherry-pick of $COMMIT_ID to $TARGET_BRANCH"