Update README.md #14
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: | | |
console.log('Starting script'); | |
const { issue, comment } = context.payload; | |
console.log('Issue:', issue); | |
console.log('Comment:', comment); | |
if (!issue || !issue.pull_request || !comment || !comment.body.startsWith('/release to ')) { | |
console.log('Not a valid release command'); | |
core.setFailed('Not a valid release command on a pull request.'); | |
return; | |
} | |
console.log('Valid release command'); | |
const releaseBranch = comment.body.split('/release to ')[1].trim(); | |
console.log('Release branch:', releaseBranch); | |
core.setOutput('release_branch', releaseBranch); | |
core.setOutput('pr_number', issue.number); | |
console.log('Creating comment'); | |
await github.rest.issues.createComment({ | |
issue_number: issue.number, | |
owner: context.repo.owner, | |
repo: context.repo.name, | |
body: '🔄 Cherry-pick operation started. Please wait...' | |
}); | |
console.log('Comment created'); | |
- name: Checkout repository | |
uses: actions/checkout@v3 | |
with: | |
fetch-depth: 0 | |
- name: Setup Git | |
run: | | |
git config --global user.email "[email protected]" | |
git config --global user.name "Tyk Bot" | |
- name: Install GitHub CLI | |
run: | | |
type -p curl >/dev/null || sudo apt install curl -y | |
curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg \ | |
&& sudo chmod go+r /usr/share/keyrings/githubcli-archive-keyring.gpg \ | |
&& echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null \ | |
&& sudo apt update \ | |
&& sudo apt install gh -y | |
- name: Get PR details | |
id: pr_details | |
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: Cherry-pick commit | |
id: cherry_pick | |
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: | | |
set -e | |
BRANCH_NAME="merge/${GITHUB_BRANCH}/${GITHUB_CHERRY_PICK_COMMIT}" | |
git checkout $GITHUB_BRANCH || { echo "Failed to checkout branch $GITHUB_BRANCH"; exit 1; } | |
git pull | |
git checkout -b $BRANCH_NAME | |
if ! git cherry-pick -x $GITHUB_CHERRY_PICK_COMMIT; then | |
echo "Cherry-pick failed. Manual intervention required." | |
git add -A | |
git -c core.editor=true cherry-pick --continue --no-edit || true | |
MERGE_FAILED=1 | |
else | |
MERGE_FAILED=0 | |
fi | |
git push origin $BRANCH_NAME --force || { echo "Failed to push branch"; exit 1; } | |
TITLE=$(git log --format=%B -n 1 $GITHUB_CHERRY_PICK_COMMIT | head -n 1) | |
MESSAGE=$(git log --format=%B -n 1 $GITHUB_CHERRY_PICK_COMMIT | tail -n +3) | |
if [ $MERGE_FAILED -eq 1 ]; then | |
PR_URL=$(gh pr create --draft --title "Merging to $GITHUB_BRANCH: $TITLE" --body "$MESSAGE" --base $GITHUB_BRANCH --head $BRANCH_NAME) | |
else | |
PR_URL=$(gh pr create --title "Merging to $GITHUB_BRANCH: $TITLE" --body "$MESSAGE" --base $GITHUB_BRANCH --head $BRANCH_NAME) | |
PR_ID="${PR_URL##*/}" | |
gh pr merge --squash $PR_ID --auto --subject "Merging to $GITHUB_BRANCH: $TITLE" --body "$MESSAGE" | |
fi | |
echo "PR_URL=$PR_URL" >> $GITHUB_OUTPUT | |
echo "MERGE_FAILED=$MERGE_FAILED" >> $GITHUB_OUTPUT | |
- name: Comment on PR | |
if: 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.'; | |
} | |
github.rest.issues.createComment({ | |
issue_number: ${{ steps.check_command.outputs.pr_number }}, | |
owner: context.repo.owner, | |
repo: context.repo.name, | |
body: body | |
}); |