|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# ============================================================================= |
| 4 | +# Prepare Release PR (Phase 1) |
| 5 | +# ============================================================================= |
| 6 | +# Description: Creates a release branch, runs pre-release for target module(s), |
| 7 | +# stages changes, commits, pushes the branch, and creates a PR. |
| 8 | +# This is Phase 1 of the two-phase release process. |
| 9 | +# |
| 10 | +# Usage: ./.github/scripts/prepare-release-pr.sh [module] |
| 11 | +# |
| 12 | +# Arguments: |
| 13 | +# module - Name of specific module to release (optional) |
| 14 | +# If not provided, releases all modules |
| 15 | +# |
| 16 | +# Environment Variables: |
| 17 | +# BUMP_TYPE - Type of version bump (default: prerelease) |
| 18 | +# |
| 19 | +# Dependencies: |
| 20 | +# - git (configured with push permissions, origin must point to docker/go-sdk) |
| 21 | +# - go (for go.work parsing and go mod tidy) |
| 22 | +# - gh (GitHub CLI, for creating PRs) |
| 23 | +# - jq (for parsing go.work) |
| 24 | +# - Docker (for semver-tool, used by pre-release.sh) |
| 25 | +# |
| 26 | +# ============================================================================= |
| 27 | + |
| 28 | +set -eo pipefail |
| 29 | + |
| 30 | +# Source common functions |
| 31 | +readonly SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" |
| 32 | +source "${SCRIPT_DIR}/common.sh" |
| 33 | + |
| 34 | +# Validate git remote before doing anything |
| 35 | +validate_git_remote |
| 36 | + |
| 37 | +MODULE=$(echo "${1:-}" | tr '[:upper:]' '[:lower:]') |
| 38 | +BUMP_TYPE="${BUMP_TYPE:-prerelease}" |
| 39 | +TIMESTAMP="$(date +%Y%m%d%H%M%S)" |
| 40 | + |
| 41 | +# Determine branch name and commit title |
| 42 | +if [[ -n "${MODULE}" ]]; then |
| 43 | + BRANCH_NAME="release/bump-${MODULE}-${TIMESTAMP}" |
| 44 | + COMMIT_TITLE="chore(${MODULE}): bump version" |
| 45 | +else |
| 46 | + BRANCH_NAME="release/bump-versions-${TIMESTAMP}" |
| 47 | + COMMIT_TITLE="chore(release): bump module versions" |
| 48 | +fi |
| 49 | + |
| 50 | +# Ensure we start from a clean, up-to-date main branch |
| 51 | +CURRENT_BRANCH=$(git -C "${ROOT_DIR}" rev-parse --abbrev-ref HEAD) |
| 52 | +if [[ "${CURRENT_BRANCH}" != "main" ]]; then |
| 53 | + echo "❌ Error: Must be on the 'main' branch to create a release PR" |
| 54 | + echo " Current branch: ${CURRENT_BRANCH}" |
| 55 | + echo "" |
| 56 | + echo "Switch to main first:" |
| 57 | + echo " git checkout main" |
| 58 | + exit 1 |
| 59 | +fi |
| 60 | + |
| 61 | +if [[ -n "$(git -C "${ROOT_DIR}" status --porcelain)" ]]; then |
| 62 | + echo "❌ Error: Working tree is not clean" |
| 63 | + echo " Commit or stash your changes before running a release." |
| 64 | + exit 1 |
| 65 | +fi |
| 66 | + |
| 67 | +echo "Fetching latest from origin..." |
| 68 | +git -C "${ROOT_DIR}" fetch origin main |
| 69 | +LOCAL_SHA=$(git -C "${ROOT_DIR}" rev-parse HEAD) |
| 70 | +REMOTE_SHA=$(git -C "${ROOT_DIR}" rev-parse origin/main) |
| 71 | +if [[ "${LOCAL_SHA}" != "${REMOTE_SHA}" ]]; then |
| 72 | + echo "❌ Error: Local main is not up to date with origin/main" |
| 73 | + echo " Local: ${LOCAL_SHA}" |
| 74 | + echo " Remote: ${REMOTE_SHA}" |
| 75 | + echo "" |
| 76 | + echo "Update your local main first:" |
| 77 | + echo " git pull origin main" |
| 78 | + exit 1 |
| 79 | +fi |
| 80 | + |
| 81 | +echo "=== Phase 1: Prepare Release PR ===" |
| 82 | +echo " Module: ${MODULE:-all}" |
| 83 | +echo " Bump type: ${BUMP_TYPE}" |
| 84 | +echo " Branch: ${BRANCH_NAME}" |
| 85 | +echo "" |
| 86 | + |
| 87 | +# Create release branch from up-to-date main |
| 88 | +git checkout -b "${BRANCH_NAME}" |
| 89 | + |
| 90 | +# Clean build directory |
| 91 | +rm -rf "${BUILD_DIR}" |
| 92 | +mkdir -p "${BUILD_DIR}" |
| 93 | + |
| 94 | +# Run pre-release for target module(s) |
| 95 | +if [[ -n "${MODULE}" ]]; then |
| 96 | + echo "Running pre-release for module: ${MODULE}" |
| 97 | + env DRY_RUN=false BUMP_TYPE="${BUMP_TYPE}" "${SCRIPT_DIR}/pre-release.sh" "${MODULE}" |
| 98 | +else |
| 99 | + echo "Running pre-release for all modules..." |
| 100 | + MODULES=$(get_modules) |
| 101 | + for m in $MODULES; do |
| 102 | + echo "" |
| 103 | + echo "--- Pre-releasing module: ${m} ---" |
| 104 | + env DRY_RUN=false BUMP_TYPE="${BUMP_TYPE}" "${SCRIPT_DIR}/pre-release.sh" "${m}" |
| 105 | + done |
| 106 | +fi |
| 107 | + |
| 108 | +# Get all modules for staging |
| 109 | +ALL_MODULES=$(get_modules) |
| 110 | + |
| 111 | +# Determine which modules to include in version summary |
| 112 | +if [[ -n "${MODULE}" ]]; then |
| 113 | + MODULES_TO_TAG="${MODULE}" |
| 114 | +else |
| 115 | + MODULES_TO_TAG="${ALL_MODULES}" |
| 116 | +fi |
| 117 | + |
| 118 | +# Stage version.go files for released modules and build commit body |
| 119 | +commit_body="" |
| 120 | +for m in $MODULES_TO_TAG; do |
| 121 | + next_tag_path=$(get_next_tag "${m}") |
| 122 | + if [[ ! -f "${next_tag_path}" ]]; then |
| 123 | + echo "Skipping ${m} because the pre-release script did not run" |
| 124 | + continue |
| 125 | + fi |
| 126 | + |
| 127 | + git add "${ROOT_DIR}/${m}/version.go" |
| 128 | + nextTag=$(cat "${next_tag_path}") |
| 129 | + commit_body="${commit_body}\n - ${m}: ${nextTag}" |
| 130 | +done |
| 131 | + |
| 132 | +# Stage go.mod and go.sum for ALL modules |
| 133 | +for m in $ALL_MODULES; do |
| 134 | + git add "${ROOT_DIR}/${m}/go.mod" |
| 135 | + if [[ -f "${ROOT_DIR}/${m}/go.sum" ]]; then |
| 136 | + git add "${ROOT_DIR}/${m}/go.sum" |
| 137 | + fi |
| 138 | +done |
| 139 | + |
| 140 | +# Verify there are staged changes |
| 141 | +if [[ -z "$(git diff --cached)" ]]; then |
| 142 | + echo "No changes detected. Aborting." |
| 143 | + exit 1 |
| 144 | +fi |
| 145 | + |
| 146 | +# Commit |
| 147 | +git commit -m "${COMMIT_TITLE}" -m "$(echo -e "${commit_body}")" |
| 148 | + |
| 149 | +# Push the branch |
| 150 | +git push origin "${BRANCH_NAME}" |
| 151 | + |
| 152 | +# Build PR body |
| 153 | +PR_BODY="## Release Version Bump |
| 154 | +
|
| 155 | +**Bump type**: \`${BUMP_TYPE}\` |
| 156 | +
|
| 157 | +### Version changes: |
| 158 | +$(echo -e "${commit_body}") |
| 159 | +
|
| 160 | +--- |
| 161 | +This PR was created automatically by the release workflow. |
| 162 | +Merging this PR will trigger Phase 2 (automatic tagging and Go proxy update)." |
| 163 | + |
| 164 | +# Create PR with gh |
| 165 | +PR_URL=$(gh pr create \ |
| 166 | + --title "${COMMIT_TITLE}" \ |
| 167 | + --body "${PR_BODY}" \ |
| 168 | + --base main \ |
| 169 | + --head "${BRANCH_NAME}" \ |
| 170 | + --label "chore" \ |
| 171 | + 2>&1) || { |
| 172 | + echo "Warning: gh pr create failed. The branch has been pushed." |
| 173 | + echo "You can create the PR manually from: ${BRANCH_NAME}" |
| 174 | + echo "Error: ${PR_URL}" |
| 175 | + exit 1 |
| 176 | + } |
| 177 | + |
| 178 | +echo "" |
| 179 | +echo "✅ Release PR created successfully!" |
| 180 | +echo " PR: ${PR_URL}" |
| 181 | +echo "" |
| 182 | +echo "Next steps:" |
| 183 | +echo " 1. Review the PR" |
| 184 | +echo " 2. Merge it to trigger Phase 2 (automatic tagging)" |
0 commit comments