release.sh 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #!/bin/bash
  2. # Interactive release script
  3. # Usage: ./release.sh
  4. # Exit immediately if a command exits with a non-zero status
  5. set -e
  6. # Function to display error messages
  7. error_exit() {
  8. echo "❌ ERROR: $1" >&2
  9. exit 1
  10. }
  11. # Function to display success messages
  12. success_message() {
  13. echo "✅ SUCCESS: $1"
  14. }
  15. # Function to display info messages
  16. info_message() {
  17. echo "ℹ️ INFO: $1"
  18. }
  19. # Function to display section headers
  20. section_header() {
  21. echo ""
  22. echo "================================================"
  23. echo "$1"
  24. echo "================================================"
  25. }
  26. # Function to increment version based on user choice
  27. increment_version() {
  28. local current_version="$1"
  29. local choice="$2"
  30. # Split version into parts
  31. IFS='.' read -ra VERSION_PARTS <<<"$current_version"
  32. local major=${VERSION_PARTS[0]}
  33. local minor=${VERSION_PARTS[1]}
  34. local patch=${VERSION_PARTS[2]}
  35. case $choice in
  36. 1)
  37. # Increment major, reset minor and patch to 0
  38. major=$((major + 1))
  39. minor=0
  40. patch=0
  41. ;;
  42. 2)
  43. # Increment minor, reset patch to 0
  44. minor=$((minor + 1))
  45. patch=0
  46. ;;
  47. 3)
  48. # Increment patch only
  49. patch=$((patch + 1))
  50. ;;
  51. *)
  52. error_exit "Invalid choice: $choice"
  53. ;;
  54. esac
  55. echo "$major.$minor.$patch"
  56. }
  57. # Function to update package.json version
  58. update_package_json() {
  59. local version="$1"
  60. local package_file="package.json"
  61. if [ ! -f "$package_file" ]; then
  62. error_exit "package.json not found in current directory"
  63. fi
  64. info_message "Updating version in $package_file to $version"
  65. # Update version using sed (macOS compatible)
  66. if [[ "$OSTYPE" == "darwin"* ]]; then
  67. sed -i '' "s/\"version\": \"[^\"]*\"/\"version\": \"$version\"/" "$package_file"
  68. else
  69. sed -i "s/\"version\": \"[^\"]*\"/\"version\": \"$version\"/" "$package_file"
  70. fi
  71. # Verify the change
  72. local new_version=$(grep -m 1 '"version":' "$package_file" | awk -F '"' '{print $4}')
  73. if [ "$new_version" = "$version" ]; then
  74. success_message "package.json version updated to $version"
  75. else
  76. error_exit "Failed to update package.json version"
  77. fi
  78. }
  79. section_header "🚀 Interactive Release Process"
  80. # Step 1: Check if we are on dev or master branch
  81. info_message "Checking current branch..."
  82. CURRENT_BRANCH=$(git branch --show-current)
  83. if [ "$CURRENT_BRANCH" != "dev" ] && [ "$CURRENT_BRANCH" != "master" ]; then
  84. error_exit "Must be on 'dev' or 'master' branch. Current branch: $CURRENT_BRANCH. Please switch to dev or master branch first."
  85. fi
  86. success_message "Currently on $CURRENT_BRANCH branch"
  87. # Step 2: Check for uncommitted changes
  88. info_message "Checking for uncommitted changes..."
  89. if [ -n "$(git status --porcelain)" ]; then
  90. echo "⚠️ WARNING: You have uncommitted changes:"
  91. git status --short
  92. read -p "Do you want to continue anyway? (y/n): " -n 1 -r
  93. echo
  94. if [[ ! $REPLY =~ ^[Yy]$ ]]; then
  95. error_exit "Please commit or stash your changes before release"
  96. fi
  97. fi
  98. success_message "Working directory is clean or user confirmed to continue"
  99. # Step 3: Fetch latest changes from remote
  100. info_message "Fetching latest changes from remote..."
  101. git fetch origin || error_exit "Failed to fetch from remote"
  102. success_message "Fetched latest changes from remote"
  103. # Step 4: Check if local branch is behind remote
  104. info_message "Checking if local $CURRENT_BRANCH branch is up to date..."
  105. LOCAL_COMMIT=$(git rev-parse $CURRENT_BRANCH)
  106. REMOTE_COMMIT=$(git rev-parse origin/$CURRENT_BRANCH)
  107. if [ "$LOCAL_COMMIT" != "$REMOTE_COMMIT" ]; then
  108. info_message "Local $CURRENT_BRANCH branch is behind remote. Pulling latest changes..."
  109. git pull origin $CURRENT_BRANCH || error_exit "Failed to pull latest changes from origin/$CURRENT_BRANCH"
  110. success_message "Successfully pulled latest changes"
  111. else
  112. success_message "Local $CURRENT_BRANCH branch is up to date"
  113. fi
  114. # Step 5: Get current version and display version selection menu
  115. info_message "Getting current version..."
  116. # Get current version from package.json
  117. CURRENT_VERSION=$(grep -m 1 '"version":' package.json | awk -F '"' '{print $4}')
  118. if [ -z "$CURRENT_VERSION" ]; then
  119. error_exit "Could not determine current version from package.json"
  120. fi
  121. # Split current version into parts for display
  122. IFS='.' read -ra VERSION_PARTS <<<"$CURRENT_VERSION"
  123. MAJOR=${VERSION_PARTS[0]}
  124. MINOR=${VERSION_PARTS[1]}
  125. PATCH=${VERSION_PARTS[2]}
  126. section_header "Version Selection"
  127. echo "📦 Current version: $CURRENT_VERSION"
  128. echo ""
  129. echo "Please select which version number to increment:"
  130. echo ""
  131. echo "1) Major version (${MAJOR}.${MINOR}.${PATCH} → $((MAJOR + 1)).0.0)"
  132. echo " Use for: Breaking changes, major new features"
  133. echo ""
  134. echo "2) Minor version (${MAJOR}.${MINOR}.${PATCH} → ${MAJOR}.$((MINOR + 1)).0)"
  135. echo " Use for: New features, backward-compatible changes"
  136. echo ""
  137. echo "3) Patch version (${MAJOR}.${MINOR}.${PATCH} → ${MAJOR}.${MINOR}.$((PATCH + 1)))"
  138. echo " Use for: Bug fixes, small improvements"
  139. echo ""
  140. # Get user choice
  141. while true; do
  142. read -p "Enter your choice (1-3): " -n 1 -r
  143. echo
  144. case $REPLY in
  145. [1-3])
  146. CHOICE=$REPLY
  147. break
  148. ;;
  149. *)
  150. echo "❌ Invalid choice. Please enter 1, 2, or 3."
  151. ;;
  152. esac
  153. done
  154. # Calculate new version
  155. NEW_VERSION=$(increment_version "$CURRENT_VERSION" "$CHOICE")
  156. VERSION_TAG="v$NEW_VERSION"
  157. section_header "Release Confirmation"
  158. echo "📋 Release Summary:"
  159. echo " Current version: $CURRENT_VERSION"
  160. echo " New version: $NEW_VERSION"
  161. echo " Tag to create: $VERSION_TAG"
  162. echo ""
  163. # Final confirmation
  164. read -p "Do you want to proceed with the release? (y/n): " -n 1 -r
  165. echo
  166. if [[ ! $REPLY =~ ^[Yy]$ ]]; then
  167. info_message "Release cancelled by user"
  168. exit 0
  169. fi
  170. # Step 6: Verify the tag doesn't already exist
  171. info_message "Checking if tag $VERSION_TAG already exists..."
  172. if git rev-parse "$VERSION_TAG" >/dev/null 2>&1; then
  173. error_exit "Tag $VERSION_TAG already exists. This should not happen as we generated it automatically."
  174. fi
  175. success_message "Tag $VERSION_TAG is available"
  176. section_header "Executing Release"
  177. # Step 7: Update version in package.json
  178. info_message "Updating version in package.json..."
  179. update_package_json "$NEW_VERSION"
  180. success_message "Version updated in package.json"
  181. # Step 8: Stage and commit changes
  182. info_message "Staging and committing version update..."
  183. git add package.json || error_exit "Failed to stage package.json"
  184. git commit -m "chore: bump version to $NEW_VERSION for release $VERSION_TAG" || error_exit "Failed to commit changes"
  185. success_message "Version change committed"
  186. # Step 10: Create the tag at the new commit
  187. info_message "Creating tag $VERSION_TAG..."
  188. git tag -a "$VERSION_TAG" -m "Release $VERSION_TAG" || error_exit "Failed to create tag"
  189. success_message "Tag $VERSION_TAG created at current commit"
  190. # Step 11: Push changes and tag to remote
  191. info_message "Pushing changes and tag to remote..."
  192. git push origin $CURRENT_BRANCH || error_exit "Failed to push changes to remote"
  193. git push origin "$VERSION_TAG" || error_exit "Failed to push tag to remote"
  194. success_message "Changes and tag pushed to remote"
  195. section_header "🎉 Release Completed Successfully!"
  196. echo "✅ Tag $VERSION_TAG has been created and pushed to remote"
  197. echo "✅ Version updated from $CURRENT_VERSION to $NEW_VERSION"
  198. echo "✅ Commit: $(git rev-parse HEAD)"
  199. echo ""
  200. echo "Jenkins will automatically deploy to Cloudflare Pages."
  201. echo "🔗 Production: https://tg-live-game.pwtk-dev.work"