release.sh 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #!/usr/bin/env bash
  2. # ------------------------------------------------------------
  3. # release.sh – semi-automatic release helper
  4. # ------------------------------------------------------------
  5. set -euo pipefail
  6. set -o errtrace
  7. trap 'echo "❌ Error – aborting"; exit 1' ERR
  8. # -------- configurable -----------------
  9. APP_NAME="${1:-LoopFollow}"
  10. SECOND_DIR="${APP_NAME}_Second"
  11. THIRD_DIR="${APP_NAME}_Third"
  12. VERSION_FILE="Config.xcconfig"
  13. MARKETING_KEY="LOOP_FOLLOW_MARKETING_VERSION"
  14. DEV_BRANCH="dev"
  15. MAIN_BRANCH="main"
  16. PATCH_DIR="../${APP_NAME}_update_patches"
  17. # ---------------------------------------
  18. # --- functions here ---
  19. pause() { read -rp "▶▶ Press Enter to continue (Ctrl-C to abort)…"; }
  20. echo_run() { echo "+ $*"; "$@"; }
  21. push_cmds=()
  22. queue_push() { push_cmds+=("git -C \"$(pwd)\" $*"); echo "+ [queued] (in $(pwd)) git $*"; }
  23. update_follower () {
  24. local DIR="$1"
  25. echo; echo "🔄 Updating $DIR …"
  26. cd "$DIR"
  27. echo; echo "If there are custom changes needed to the patch, make the change before continuing"
  28. pause
  29. # 1 · Make sure we’re on a clean, up-to-date main
  30. echo_run git switch "$MAIN_BRANCH"
  31. echo_run git fetch
  32. echo_run git pull
  33. # 2 · Apply the patch
  34. if ! git apply --whitespace=nowarn "$PATCH_FILE"; then
  35. echo "‼️ Some changes could not be applied, so no changes were made."
  36. echo "The command used was: git apply --whitespace=nowarn $PATCH_FILE"
  37. echo; echo "Use a different terminal to fix and apply the patch before continuing"
  38. pause
  39. fi
  40. # 3 · Pause if any conflict markers remain
  41. if git ls-files -u | grep -q .; then
  42. echo "⚠️ Conflicts detected."
  43. echo " If Fastfile or build_LoopFollow.yml were modified, these are expected."
  44. echo " Open your merge tool, resolve, then press Enter."
  45. pause
  46. fi
  47. # 4 · Single commit capturing all staged changes
  48. git add -u
  49. git add $(git ls-files --others --exclude-standard) 2>/dev/null || true
  50. git commit -m "transfer v${new_ver} updates from LF to ${DIR}"
  51. echo_run git status
  52. echo "💻 Build & test $DIR now."; pause # build & test checkpoint
  53. queue_push push origin "$MAIN_BRANCH"
  54. cd ..
  55. }
  56. # ---------- PRIMARY REPO ----------
  57. PRIMARY_ABS_PATH="$(pwd -P)"
  58. echo "🏁 Working in $PRIMARY_ABS_PATH …"
  59. # --- guard: make sure we're running the latest release.sh from origin/dev ----
  60. # The release flow assumes the newest script. If this copy differs from the one
  61. # on origin/dev, we're likely running a stale version — abort and tell the user.
  62. SCRIPT_PATH="${BASH_SOURCE[0]}"
  63. echo_run git fetch origin "$DEV_BRANCH"
  64. if ! diff -q <(git show "origin/${DEV_BRANCH}:release.sh") "$SCRIPT_PATH" >/dev/null 2>&1; then
  65. echo "❌ This release.sh does not match origin/${DEV_BRANCH}:release.sh."
  66. echo " You're likely running an outdated copy of the release script."
  67. echo " Switch to the latest '${DEV_BRANCH}' (git switch ${DEV_BRANCH} && git pull) and re-run. Aborting."
  68. exit 1
  69. fi
  70. # --- start out in main to capture old_ver ----
  71. echo_run git switch "$MAIN_BRANCH"
  72. echo_run git fetch
  73. echo_run git pull
  74. # -------- version bump logic (unchanged) -----------
  75. old_ver=$(grep -E "^${MARKETING_KEY}[[:space:]]*=" "$VERSION_FILE" | awk '{print $3}')
  76. major_candidate="$(awk -F. '{printf "%d.0.0", $1 + 1}' <<<"$old_ver")"
  77. minor_candidate="$(awk -F. '{printf "%d.%d.0", $1, $2 + 1}' <<<"$old_ver")"
  78. echo
  79. echo "Which version bump do you want?"
  80. echo " 1) Major → $major_candidate"
  81. echo " 2) Minor → $minor_candidate"
  82. read -rp "Enter 1 or 2 (default = 2): " choice
  83. echo
  84. case "$choice" in
  85. 1) new_ver="$major_candidate" ;; ""|2) new_ver="$minor_candidate" ;;
  86. *) echo "❌ Invalid choice – aborting."; exit 1 ;;
  87. esac
  88. echo "🔢 Bumping version: $old_ver → $new_ver"
  89. # --- switch to dev so the release branch is cut from latest dev ----
  90. echo_run git switch "$DEV_BRANCH"
  91. echo_run git fetch
  92. echo_run git pull
  93. # --- create release branch from dev's tip ----
  94. RELEASE_BRANCH="release/v${new_ver}"
  95. echo_run git switch -c "$RELEASE_BRANCH"
  96. # --- bump version on the release branch ----
  97. sed -i '' "s/${MARKETING_KEY}[[:space:]]*=.*/${MARKETING_KEY} = ${new_ver}/" "$VERSION_FILE"
  98. echo_run git diff "$VERSION_FILE"; pause
  99. # No [skip ci] here on purpose: it would skip the required SwiftFormat lint check
  100. # (leaving the release PRs blocked) and the tag_on_main workflow (no auto-tag).
  101. # auto_version_dev already avoids re-bumping via its "Config.xcconfig changed" guard.
  102. echo_run git commit -m "update version to ${new_ver}" "$VERSION_FILE"
  103. echo "💻 Build & test release branch now."; pause
  104. queue_push push origin "$RELEASE_BRANCH"
  105. # --- create a patch from main..release branch (includes the bump) -----
  106. mkdir -p "$PATCH_DIR"
  107. PATCH_FILE="${PATCH_DIR}/LF_diff_${old_ver}_to_${new_ver}.patch"
  108. git diff -M --binary "$MAIN_BRANCH" "$RELEASE_BRANCH" \
  109. > "$PATCH_FILE"
  110. cd ..
  111. update_follower "$SECOND_DIR"
  112. update_follower "$THIRD_DIR"
  113. # ---------- GitHub Actions Test ---------
  114. echo;
  115. echo "💻 Test GitHub Build Actions for all three repositories and then continue.";
  116. pause
  117. # --- return to primary path
  118. cd ${PRIMARY_ABS_PATH}
  119. # ---------- push queue ----------
  120. echo; echo "🚀 Ready to push changes upstream and open the release PR."
  121. echo_run git log --oneline -2
  122. read -rp "▶▶ Push everything now? (y/n): " confirm
  123. if [[ $confirm =~ ^[Yy]$ ]]; then
  124. for cmd in "${push_cmds[@]}"; do echo "+ $cmd"; bash -c "$cmd"; done
  125. echo "🎉 All pushes completed."
  126. # --- resolve the GitHub repo explicitly so gh doesn't depend on a default ----
  127. # Clones with multiple remotes have no inferred default repo, which makes
  128. # `gh pr create` fail. Derive owner/repo from the origin remote URL.
  129. # Handles both https://github.com/owner/repo(.git) and git@github.com:owner/repo(.git)
  130. REPO_SLUG="$(git remote get-url origin | sed -E -e 's#\.git$##' -e 's#^.*github\.com[:/]##')"
  131. echo; echo "📝 Opening sync PR ${RELEASE_BRANCH} → ${DEV_BRANCH} …"
  132. gh pr create \
  133. --repo "$REPO_SLUG" \
  134. --base "$DEV_BRANCH" \
  135. --head "$RELEASE_BRANCH" \
  136. --title "Sync v${new_ver} version bump to dev" \
  137. --body "Syncs the v${new_ver} version bump from the release branch back to \`dev\` so subsequent auto-bumps on \`dev\` continue from the released minor.
  138. \`auto_version_dev\` detects that \`Config.xcconfig\` was changed in this push and skips re-bumping.
  139. ⚠️ **Use \"Create a merge commit\"** (not squash or rebase) so \`dev\` and \`main\` share the same release lineage."
  140. echo; echo "📝 Opening release PR ${RELEASE_BRANCH} → ${MAIN_BRANCH} …"
  141. gh pr create \
  142. --repo "$REPO_SLUG" \
  143. --base "$MAIN_BRANCH" \
  144. --head "$RELEASE_BRANCH" \
  145. --title "Release v${new_ver}" \
  146. --body "Release v${new_ver}.
  147. Merging this PR triggers the tagging workflow, which creates tag \`v${new_ver}\` from \`LOOP_FOLLOW_MARKETING_VERSION\` in \`Config.xcconfig\`.
  148. ⚠️ **Use \"Create a merge commit\"** (not squash or rebase) so \`main\` keeps the original feature-PR commits."
  149. echo; echo "🎉 All repos updated to v${new_ver} (local). Release PRs opened (sync → dev, release → main)."
  150. echo "👉 Review and merge both PRs — the tag will be created automatically by .github/workflows/tag_on_main.yml."
  151. echo "👉 Remember to create a GitHub release for tag v${new_ver} after the tag exists."
  152. else
  153. echo "🚫 Pushes skipped. Run manually if needed:"; printf ' %s\n' "${push_cmds[@]}"
  154. echo "🚫 Release not completed, pushes to GitHub were skipped"
  155. fi