Skip to content

Update README.md

Update README.md #40

Workflow file for this run

name: Cherry-pick to Release Branch
on:
issue_comment:
types: [created]
workflow_call:
jobs:
cherry_pick:
runs-on: ubuntu-latest
steps:
- name: Check for release command
id: check_command
uses: actions/github-script@v6
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
const { issue, comment } = context.payload;
if (!issue || !issue.pull_request || !comment || !comment.body.startsWith('/release to ')) {
core.setOutput('release_valid', 'false');
return;
}
const releaseBranch = comment.body.split('/release to ')[1].trim();
core.setOutput('release_valid', 'true');
core.setOutput('release_branch', releaseBranch);
core.setOutput('pr_number', issue.number);
- name: Checkout repository
if: steps.check_command.outputs.release_valid == 'true'
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Set default branch variable
if: steps.check_command.outputs.release_valid == 'true'
run: |
echo "DEFAULT_BRANCH=${{ github.event.repository.default_branch }}" >> $GITHUB_ENV
- name: Skip jobs if not a valid release command
if: steps.check_command.outputs.release_valid == 'false'
run: |
echo "Skipping cherry-pick as the release command is not valid."
continue-on-error: true
- name: Setup Git
if: steps.check_command.outputs.release_valid == 'true'
run: |
git config --global user.email "[email protected]"
git config --global user.name "Tyk Bot"
- name: Get PR details
id: pr_details
if: steps.check_command.outputs.release_valid == 'true'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
PR_DATA=$(gh pr view ${{ steps.check_command.outputs.pr_number }} --json headRefOid)
COMMIT_SHA=$(echo $PR_DATA | jq -r .headRefOid)
echo "COMMIT_SHA=${COMMIT_SHA}" >> $GITHUB_OUTPUT
- name: Clone repository and cherry-pick commit
if: steps.check_command.outputs.release_valid == 'true'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_REPO: ${{ github.repository }}
GITHUB_BRANCH: ${{ steps.check_command.outputs.release_branch }}
GITHUB_CHERRY_PICK_COMMIT: ${{ steps.pr_details.outputs.COMMIT_SHA }}
run: |
# Clone the repository
export FOLDER=$(echo $GITHUB_REPO | cut -d '/' -f2)
rm -rf $FOLDER
git clone https://x-access-token:[email protected]/$GITHUB_REPO || true
cd $FOLDER
# Reset and checkout default branch (master or main)
git reset --hard
git checkout $DEFAULT_BRANCH
git pull
git checkout $GITHUB_BRANCH
git reset --hard
git pull
# Delete old branch if it exists
git branch -d merge/$GITHUB_BRANCH/$GITHUB_CHERRY_PICK_COMMIT || true
git push origin --delete merge/$GITHUB_BRANCH/$GITHUB_CHERRY_PICK_COMMIT || true
# Create and checkout the new branch
git checkout -b merge/$GITHUB_BRANCH/$GITHUB_CHERRY_PICK_COMMIT
# Cherry-pick the commit
MERGE_FAILED=0
git cherry-pick -x $GITHUB_CHERRY_PICK_COMMIT || MERGE_FAILED=$?
# If the cherry-pick failed, resolve conflicts
if ! [ $MERGE_FAILED -eq 0 ]; then
git add -A && git -c core.editor=true cherry-pick --continue --no-edit || true
fi
# Push the new branch
git push origin merge/$GITHUB_BRANCH/$GITHUB_CHERRY_PICK_COMMIT --force || true
# Prepare the message and title for the PR
MESSAGE=$(git log --format=%B -n 1 $GITHUB_CHERRY_PICK_COMMIT)
TITLE=$(git log --format=%B -n 1 $GITHUB_CHERRY_PICK_COMMIT | head -n 1)
# Check GH token
echo $GITHUB_TOKEN || gh auth login --with-token
# Create the PR
PR_URL=
if ! [ $MERGE_FAILED -eq 0 ]; then
PR_URL=$(gh pr create --draft --title "Merging to $GITHUB_BRANCH: $TITLE" --body "$MESSAGE" --repo $GITHUB_REPO --base $GITHUB_BRANCH --head merge/$GITHUB_BRANCH/$GITHUB_CHERRY_PICK_COMMIT)
else
PR_URL=$(gh pr create --title "Merging to $GITHUB_BRANCH: $TITLE" --body "$MESSAGE" --repo $GITHUB_REPO --base $GITHUB_BRANCH --head merge/$GITHUB_BRANCH/$GITHUB_CHERRY_PICK_COMMIT)
fi
# Extract PR ID
PR_ID="${PR_URL##*/}"
# If no merge failure, auto-merge the PR
if [ $MERGE_FAILED -eq 0 ]; then
gh pr merge --squash $PR_ID --auto --subject "Merging to $GITHUB_BRANCH: $TITLE" --body "$MESSAGE"
fi
# Set outputs for use in the next step
echo "PR_URL=${PR_URL}" >> $GITHUB_OUTPUT
echo "MERGE_FAILED=${MERGE_FAILED}" >> $GITHUB_OUTPUT
- name: Comment on PR
if: steps.check_command.outputs.release_valid == 'true' && always()
uses: actions/github-script@v6
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
const prUrl = '${{ steps.cherry_pick.outputs.PR_URL }}';
const mergeFailed = '${{ steps.cherry_pick.outputs.MERGE_FAILED }}' === '1';
let body;
if ('${{ job.status }}' === 'success') {
if (mergeFailed) {
body = `⚠️ Cherry-pick operation completed with conflicts. A draft pull request has been created: ${prUrl}\n\nPlease resolve the conflicts manually.`;
} else {
body = `✅ Cherry-pick operation completed successfully. New pull request created: ${prUrl}`;
}
} else {
body = '❌ Cherry-pick operation failed. Please check the action logs for more information.';
}
const owner = context.repo.owner || '${{ github.repository_owner }}';
const repo = context.repo.repo || '${{ github.event.repository.name }}';
github.rest.issues.createComment({
issue_number: ${{ steps.check_command.outputs.pr_number }},
owner: owner,
repo: repo,
body: body
});