|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -e |
| 4 | + |
| 5 | +function usage() { |
| 6 | + echo -n "Usage: $(basename "$0") [OPTIONS] |
| 7 | +Automate the release process using git flow and CalVer versioning. |
| 8 | +
|
| 9 | +This script will: |
| 10 | +1. Checkout the develop branch |
| 11 | +2. Identify the latest release tag |
| 12 | +3. Calculate the next CalVer version (or use provided version) |
| 13 | +4. Start a git flow release |
| 14 | +5. Bump version in package.json |
| 15 | +6. Publish and finish the release |
| 16 | +
|
| 17 | +Options: |
| 18 | +--version VERSION: Specify a custom version instead of auto-calculating (format: YYYY.M.P) |
| 19 | +--help: Display this help message |
| 20 | +" |
| 21 | +} |
| 22 | + |
| 23 | +function error() { |
| 24 | + echo "Error: $1" >&2 |
| 25 | + exit 1 |
| 26 | +} |
| 27 | + |
| 28 | +function confirm() { |
| 29 | + local prompt="$1" |
| 30 | + local response |
| 31 | + read -r -p "$prompt [y/N]: " response |
| 32 | + case "$response" in |
| 33 | + [yY][eE][sS]|[yY]) |
| 34 | + return 0 |
| 35 | + ;; |
| 36 | + *) |
| 37 | + return 1 |
| 38 | + ;; |
| 39 | + esac |
| 40 | +} |
| 41 | + |
| 42 | +function check_git_flow() { |
| 43 | + if ! command -v git-flow &> /dev/null; then |
| 44 | + error "git-flow is not installed. Please install it first: https://github.com/nvie/gitflow/wiki/Installation" |
| 45 | + fi |
| 46 | + |
| 47 | + # Check for macOS and ensure GNU getopt is available |
| 48 | + if [[ "$(uname)" == "Darwin" ]]; then |
| 49 | + if ! command -v /usr/local/opt/gnu-getopt/bin/getopt &> /dev/null && ! command -v /opt/homebrew/opt/gnu-getopt/bin/getopt &> /dev/null; then |
| 50 | + error "On macOS, git-flow requires GNU getopt. Install it with: brew install gnu-getopt" |
| 51 | + fi |
| 52 | + fi |
| 53 | +} |
| 54 | + |
| 55 | +function check_gh_cli() { |
| 56 | + if ! command -v gh &> /dev/null; then |
| 57 | + error "GitHub CLI (gh) is not installed. Please install it first: https://cli.github.com/" |
| 58 | + fi |
| 59 | +} |
| 60 | + |
| 61 | +function check_clean_working_directory() { |
| 62 | + if [[ -n $(git status --porcelain) ]]; then |
| 63 | + error "Working directory is not clean. Please commit or stash your changes first." |
| 64 | + fi |
| 65 | +} |
| 66 | + |
| 67 | +function get_latest_tag() { |
| 68 | + git tag | sort -V | tail -n 1 |
| 69 | +} |
| 70 | + |
| 71 | +function calculate_next_version() { |
| 72 | + local latest_tag="$1" |
| 73 | + local current_year=$(date +%Y) |
| 74 | + local current_month=$(date +%-m) |
| 75 | + |
| 76 | + if [[ -z "$latest_tag" ]]; then |
| 77 | + echo "${current_year}.${current_month}.1" |
| 78 | + return |
| 79 | + fi |
| 80 | + |
| 81 | + # Parse the latest tag (format: YYYY.M.P) |
| 82 | + if [[ $latest_tag =~ ^([0-9]{4})\.([0-9]+)\.([0-9]+)$ ]]; then |
| 83 | + local tag_year="${BASH_REMATCH[1]}" |
| 84 | + local tag_month="${BASH_REMATCH[2]}" |
| 85 | + local tag_patch="${BASH_REMATCH[3]}" |
| 86 | + |
| 87 | + # If same year and month, increment patch |
| 88 | + if [[ "$tag_year" == "$current_year" ]] && [[ "$tag_month" == "$current_month" ]]; then |
| 89 | + echo "${current_year}.${current_month}.$((tag_patch + 1))" |
| 90 | + else |
| 91 | + # New month or year, reset patch to 1 |
| 92 | + echo "${current_year}.${current_month}.1" |
| 93 | + fi |
| 94 | + else |
| 95 | + error "Latest tag '$latest_tag' does not follow CalVer format (YYYY.M.P)" |
| 96 | + fi |
| 97 | +} |
| 98 | + |
| 99 | +function update_package_json_version() { |
| 100 | + local version="$1" |
| 101 | + |
| 102 | + if [[ ! -f "package.json" ]]; then |
| 103 | + error "package.json not found in current directory" |
| 104 | + fi |
| 105 | + |
| 106 | + # Use Node.js to update the version in package.json |
| 107 | + if command -v node &> /dev/null; then |
| 108 | + node -e " |
| 109 | + const fs = require('fs'); |
| 110 | + const pkg = JSON.parse(fs.readFileSync('package.json', 'utf8')); |
| 111 | + pkg.version = '$version'; |
| 112 | + fs.writeFileSync('package.json', JSON.stringify(pkg, null, 2) + '\n'); |
| 113 | + " |
| 114 | + else |
| 115 | + # Fallback to sed if node is not available |
| 116 | + sed -i.bak 's/"version": ".*"/"version": "'$version'"/' package.json && rm package.json.bak |
| 117 | + fi |
| 118 | + |
| 119 | + echo "Updated package.json to version $version" |
| 120 | +} |
| 121 | + |
| 122 | +function run_release() { |
| 123 | + local custom_version="$1" |
| 124 | + |
| 125 | + echo "=== Planetary Computer Data Catalog Release Process ===" |
| 126 | + echo "" |
| 127 | + |
| 128 | + # Check prerequisites |
| 129 | + check_git_flow |
| 130 | + check_gh_cli |
| 131 | + check_clean_working_directory |
| 132 | + |
| 133 | + # Step 1: Checkout develop branch |
| 134 | + echo "Step 1: Checking out develop branch..." |
| 135 | + git checkout develop |
| 136 | + echo "Pulling latest changes from origin..." |
| 137 | + git pull origin develop |
| 138 | + echo "" |
| 139 | + |
| 140 | + # Step 2: Identify latest release |
| 141 | + echo "Step 2: Identifying latest release..." |
| 142 | + latest_tag=$(get_latest_tag) |
| 143 | + if [[ -z "$latest_tag" ]]; then |
| 144 | + echo "No existing tags found." |
| 145 | + else |
| 146 | + echo "Latest release tag: $latest_tag" |
| 147 | + fi |
| 148 | + echo "" |
| 149 | + |
| 150 | + # Step 3: Determine next version |
| 151 | + if [[ -n "$custom_version" ]]; then |
| 152 | + echo "Step 3: Using custom version..." |
| 153 | + # Validate custom version format |
| 154 | + if [[ ! $custom_version =~ ^([0-9]{4})\.([0-9]+)\.([0-9]+)$ ]]; then |
| 155 | + error "Custom version '$custom_version' does not follow CalVer format (YYYY.M.P)" |
| 156 | + fi |
| 157 | + next_version="$custom_version" |
| 158 | + echo "Using version: $next_version" |
| 159 | + else |
| 160 | + echo "Step 3: Calculating next version..." |
| 161 | + next_version=$(calculate_next_version "$latest_tag") |
| 162 | + echo "Next version will be: $next_version" |
| 163 | + fi |
| 164 | + echo "" |
| 165 | + |
| 166 | + if ! confirm "Continue with version $next_version?"; then |
| 167 | + echo "Release cancelled." |
| 168 | + exit 0 |
| 169 | + fi |
| 170 | + echo "" |
| 171 | + |
| 172 | + # Step 4: Start git flow release |
| 173 | + echo "Step 4: Starting git flow release..." |
| 174 | + if ! git flow release start "$next_version" 2>&1; then |
| 175 | + # Check if there's an existing release branch |
| 176 | + existing_release=$(git branch | grep "release/" | sed 's/^[ *]*//' | head -n 1) |
| 177 | + if [[ -n "$existing_release" ]]; then |
| 178 | + echo "" |
| 179 | + echo "Found existing release branch: $existing_release" |
| 180 | + if confirm "Delete the existing release branch and continue?"; then |
| 181 | + echo "Deleting $existing_release..." |
| 182 | + git checkout develop |
| 183 | + git branch -D "$existing_release" || error "Failed to delete local branch $existing_release" |
| 184 | + |
| 185 | + # Try to delete remote branch if it exists |
| 186 | + if git ls-remote --heads origin "$existing_release" | grep -q "$existing_release"; then |
| 187 | + echo "Deleting remote branch..." |
| 188 | + git push origin --delete "$existing_release" 2>/dev/null || echo "Note: Could not delete remote branch (may not exist or lack permissions)" |
| 189 | + fi |
| 190 | + |
| 191 | + echo "Retrying release start..." |
| 192 | + if ! git flow release start "$next_version"; then |
| 193 | + error "Failed to start git flow release after cleanup" |
| 194 | + fi |
| 195 | + else |
| 196 | + error "Cannot proceed with existing release branch. Please finish or delete it manually." |
| 197 | + fi |
| 198 | + else |
| 199 | + error "Failed to start git flow release" |
| 200 | + fi |
| 201 | + fi |
| 202 | + echo "" |
| 203 | + |
| 204 | + # Step 5: Bump version in package.json |
| 205 | + echo "Step 5: Updating version in package.json..." |
| 206 | + update_package_json_version "$next_version" |
| 207 | + |
| 208 | + if ! confirm "Version updated in package.json. Review the changes and continue?"; then |
| 209 | + echo "Release cancelled. You are still on the release branch." |
| 210 | + echo "To abort, run: git flow release delete $next_version" |
| 211 | + exit 0 |
| 212 | + fi |
| 213 | + |
| 214 | + git add package.json |
| 215 | + git commit -m "$next_version" |
| 216 | + echo "" |
| 217 | + |
| 218 | + # Step 6: Publish release |
| 219 | + echo "Step 6: Publishing release..." |
| 220 | + if ! confirm "Publish release branch to origin?"; then |
| 221 | + echo "Release not published. You are still on the release branch." |
| 222 | + echo "To publish later, run: git flow release publish $next_version" |
| 223 | + echo "To abort, run: git flow release delete $next_version" |
| 224 | + exit 0 |
| 225 | + fi |
| 226 | + |
| 227 | + if ! git flow release publish "$next_version"; then |
| 228 | + error "Failed to publish git flow release" |
| 229 | + fi |
| 230 | + echo "" |
| 231 | + |
| 232 | + # Step 7: Create pull request to main |
| 233 | + echo "Step 7: Creating pull request to main..." |
| 234 | + echo "" |
| 235 | + |
| 236 | + if ! confirm "Ready to create a pull request from release/$next_version to main?"; then |
| 237 | + echo "Pull request not created. The release branch has been published." |
| 238 | + echo "To create PR later, run: gh pr create --base main --head release/$next_version" |
| 239 | + exit 0 |
| 240 | + fi |
| 241 | + |
| 242 | + # Create PR to main branch |
| 243 | + echo "Creating pull request..." |
| 244 | + if ! gh pr create \ |
| 245 | + --base main \ |
| 246 | + --head "release/$next_version" \ |
| 247 | + --title "Release $next_version" \ |
| 248 | + --body "Automated release $next_version |
| 249 | +
|
| 250 | +This release includes all changes from the develop branch. |
| 251 | +
|
| 252 | +After merging: |
| 253 | +- GitHub Actions will build and deploy to production |
| 254 | +- The release will be tagged as $next_version |
| 255 | +- Changes will need to be merged back to develop"; then |
| 256 | + error "Failed to create pull request. You may need to check your GitHub CLI authentication." |
| 257 | + fi |
| 258 | + |
| 259 | + echo "" |
| 260 | + echo "=== Pull request for release $next_version created successfully! ===" |
| 261 | + echo "" |
| 262 | + echo "Next steps:" |
| 263 | + echo " 1. Review and merge the PR to main" |
| 264 | + echo " 2. GitHub Actions will build and deploy to production" |
| 265 | + echo " 3. After merge, the release will be tagged as $next_version" |
| 266 | + echo " 4. Merge main back to develop to complete the release cycle" |
| 267 | + echo "" |
| 268 | + echo "View the PR with: gh pr view" |
| 269 | +} |
| 270 | + |
| 271 | +if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then |
| 272 | + custom_version="" |
| 273 | + |
| 274 | + while [[ $# -gt 0 ]]; do |
| 275 | + case "$1" in |
| 276 | + --help) |
| 277 | + usage |
| 278 | + exit 0 |
| 279 | + ;; |
| 280 | + --version) |
| 281 | + if [[ -z "${2:-}" ]]; then |
| 282 | + error "Missing value for --version option" |
| 283 | + fi |
| 284 | + custom_version="$2" |
| 285 | + shift 2 |
| 286 | + ;; |
| 287 | + *) |
| 288 | + error "Unknown option: $1" |
| 289 | + ;; |
| 290 | + esac |
| 291 | + done |
| 292 | + |
| 293 | + run_release "$custom_version" |
| 294 | +fi |
0 commit comments