Created
December 13, 2025 23:50
-
-
Save tamadon/6460ed76b36a07750bce5c25c69fac29 to your computer and use it in GitHub Desktop.
(Auto) Cherry-Pick to Release Branch
This file contains hidden or 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
| # mainブランチへのPRマージ時に、releaseブランチへ自動でcherry-pickしてPRを作成する | |
| name: (Auto) Cherry-Pick to Release Branch | |
| on: | |
| pull_request: | |
| types: [closed] | |
| jobs: | |
| cherry-pick: | |
| # mainブランチへのマージ かつ 'need cherry-pick' ラベルが付いている場合のみ実行 | |
| if: | | |
| github.event.pull_request.merged == true && | |
| contains(github.event.pull_request.labels.*.name, 'need cherry-pick') && | |
| github.base_ref == 'main' | |
| runs-on: ubuntu-latest | |
| steps: | |
| # Step 1: リポジトリのコードをチェックアウト | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| # Step 2: Gitのユーザー情報を設定 | |
| - name: Set up Git user | |
| run: | | |
| git config --global user.name "github-actions[bot]" | |
| git config --global user.email "github-actions[bot]@users.noreply.github.com" | |
| # Step 3: cherry-pick処理の実行 | |
| - name: Cherry-pick to release branches and create PRs | |
| env: | |
| MERGE_COMMIT: ${{ github.event.pull_request.merge_commit_sha }} | |
| PR_NUMBER: ${{ github.event.pull_request.number }} | |
| # GITHUB_TOKENで作成したPRはCIが自動起動しないため | |
| # CIを自動起動させたい場合はPATに変更してください | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| run: | | |
| # スクリプト内で使用する変数の定義 | |
| MERGE_COMMIT_SHORT=$(echo "$MERGE_COMMIT" | cut -c1-7) | |
| failure_occurred=false | |
| echo "🍒 Starting cherry-pick for commit $MERGE_COMMIT_SHORT from PR #$PR_NUMBER" | |
| # Gitの全情報を取得 | |
| git fetch --all | |
| # [CUSTOMIZE] 必要に応じてcherry-pick対象ブランチのパターンを変更してください | |
| # 例: 'origin/release/[0-9]+\.[0-9]+\.[0-9]+' は release/1.2.3 形式にマッチ | |
| TARGET_BRANCHES=$(git branch -r | grep -E 'origin/release/[0-9]+\.[0-9]+\.[0-9]+' | sed 's/^\s*//' | sed 's/origin\///') | |
| if [[ -z "$TARGET_BRANCHES" ]]; then | |
| echo "No release branches found. Exiting." | |
| exit 0 | |
| fi | |
| # 取得したブランチの一覧を配列に変換 | |
| readarray -t branches <<< "$TARGET_BRANCHES" | |
| # 各cherry-pick対象ブランチに対してループ処理を実行 | |
| for BRANCH in "${branches[@]}"; do | |
| echo "----------------------------------------" | |
| echo "📌 Processing: $BRANCH" | |
| # 安全に実行するため、ループ開始時にmainブランチへ移動 | |
| git checkout main | |
| # cherry-pick用ブランチを作成('/'を'-'に置換) | |
| NEW_BRANCH="cherry-pick/${BRANCH//\//-}/$MERGE_COMMIT_SHORT" | |
| # 対象ブランチの最新状態を取得してチェックアウト | |
| git checkout "$BRANCH" | |
| git pull origin "$BRANCH" | |
| git checkout -b "$NEW_BRANCH" | |
| # cherry-pick実行(-m 1 はマージコミットの親を指定) | |
| if ! git cherry-pick -m 1 "$MERGE_COMMIT"; then | |
| echo "::error::❌ Cherry-pick to '$BRANCH' failed (conflict). Skipping." | |
| failure_occurred=true | |
| git cherry-pick --abort | |
| continue | |
| fi | |
| echo "✅ Cherry-pick successful" | |
| git push origin "$NEW_BRANCH" | |
| # [CUSTOMIZE] 必要に応じてPRのタイトルと本文は変更してください | |
| gh pr create \ | |
| --title "🍒 Cherry-pick $MERGE_COMMIT_SHORT to $BRANCH (from PR #$PR_NUMBER)" \ | |
| --body "Automated cherry-pick of commit \`$MERGE_COMMIT_SHORT\` from PR #$PR_NUMBER to \`$BRANCH\`" \ | |
| --head "$NEW_BRANCH" \ | |
| --base "$BRANCH" | |
| echo "✅ PR created for $BRANCH" | |
| done | |
| echo "----------------------------------------" | |
| if [[ "$failure_occurred" == "true" ]]; then | |
| echo "::warning::Some cherry-picks failed. Please resolve conflicts manually." | |
| exit 1 | |
| fi | |
| echo "🎉 All cherry-picks completed successfully!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment