|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +REPO="stakater/Reloader" |
| 5 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 6 | +ROOT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)" |
| 7 | + |
| 8 | +# Colors |
| 9 | +RED='\033[0;31m' |
| 10 | +GREEN='\033[0;32m' |
| 11 | +YELLOW='\033[1;33m' |
| 12 | +NC='\033[0m' # No Color |
| 13 | + |
| 14 | +info() { echo -e "${GREEN}[INFO]${NC} $*"; } |
| 15 | +warn() { echo -e "${YELLOW}[WARN]${NC} $*"; } |
| 16 | +error() { echo -e "${RED}[ERROR]${NC} $*" >&2; } |
| 17 | + |
| 18 | +confirm() { |
| 19 | + local msg="$1" |
| 20 | + echo -en "${YELLOW}$msg [y/N]:${NC} " |
| 21 | + read -r answer |
| 22 | + [[ "$answer" =~ ^[Yy]$ ]] |
| 23 | +} |
| 24 | + |
| 25 | +usage() { |
| 26 | + cat <<EOF |
| 27 | +Usage: $0 <APP_VERSION> <CHART_VERSION> |
| 28 | +
|
| 29 | +Automates the full Reloader release process. |
| 30 | +
|
| 31 | +Arguments: |
| 32 | + APP_VERSION Application version without 'v' prefix (e.g. 1.5.0, 1.5.0-alpha) |
| 33 | + CHART_VERSION Helm chart version (e.g. 2.3.0, 2.3.0-rc.1) |
| 34 | +
|
| 35 | +Prerequisites: |
| 36 | + - gh CLI authenticated with repo access |
| 37 | + - git configured with push access to $REPO |
| 38 | +
|
| 39 | +Example: |
| 40 | + $0 1.5.0 2.3.0 |
| 41 | +EOF |
| 42 | + exit 1 |
| 43 | +} |
| 44 | + |
| 45 | +# --- Input validation --- |
| 46 | +[[ $# -ne 2 ]] && usage |
| 47 | + |
| 48 | +APP_VERSION="$1" |
| 49 | +CHART_VERSION="$2" |
| 50 | + |
| 51 | +# Strip 'v' prefix if provided |
| 52 | +APP_VERSION="${APP_VERSION#v}" |
| 53 | +CHART_VERSION="${CHART_VERSION#v}" |
| 54 | + |
| 55 | +# Validate semver format (with optional prerelease suffix e.g. 1.5.0-alpha, 1.5.0-rc.1) |
| 56 | +SEMVER_RE='^[0-9]+\.[0-9]+\.[0-9]+([-][a-zA-Z0-9.]+)?$' |
| 57 | + |
| 58 | +if ! [[ "$APP_VERSION" =~ $SEMVER_RE ]]; then |
| 59 | + error "APP_VERSION '$APP_VERSION' is not valid semver (expected X.Y.Z or X.Y.Z-prerelease)" |
| 60 | + exit 1 |
| 61 | +fi |
| 62 | + |
| 63 | +if ! [[ "$CHART_VERSION" =~ $SEMVER_RE ]]; then |
| 64 | + error "CHART_VERSION '$CHART_VERSION' is not valid semver (expected X.Y.Z or X.Y.Z-prerelease)" |
| 65 | + exit 1 |
| 66 | +fi |
| 67 | + |
| 68 | +# Check prerequisites |
| 69 | +if ! command -v gh &> /dev/null; then |
| 70 | + error "gh CLI is not installed. Install from https://cli.github.com/" |
| 71 | + exit 1 |
| 72 | +fi |
| 73 | + |
| 74 | +if ! gh auth status &> /dev/null; then |
| 75 | + error "gh CLI is not authenticated. Run 'gh auth login' first." |
| 76 | + exit 1 |
| 77 | +fi |
| 78 | + |
| 79 | +RELEASE_BRANCH="release-v${APP_VERSION}" |
| 80 | +TAG="v${APP_VERSION}" |
| 81 | + |
| 82 | +info "Release plan:" |
| 83 | +info " App version: $APP_VERSION (tag: $TAG)" |
| 84 | +info " Chart version: $CHART_VERSION" |
| 85 | +info " Release branch: $RELEASE_BRANCH" |
| 86 | +echo "" |
| 87 | + |
| 88 | +# ============================================================================= |
| 89 | +# Phase 1: Create release branch |
| 90 | +# ============================================================================= |
| 91 | +info "Phase 1: Create release branch '$RELEASE_BRANCH' from master" |
| 92 | + |
| 93 | +if git ls-remote --heads origin "$RELEASE_BRANCH" | grep -q "$RELEASE_BRANCH"; then |
| 94 | + warn "Branch '$RELEASE_BRANCH' already exists on remote." |
| 95 | + if ! confirm "Continue using existing branch?"; then |
| 96 | + error "Aborted." |
| 97 | + exit 1 |
| 98 | + fi |
| 99 | +else |
| 100 | + if ! confirm "Create and push branch '$RELEASE_BRANCH' from master?"; then |
| 101 | + error "Aborted." |
| 102 | + exit 1 |
| 103 | + fi |
| 104 | + git fetch origin master |
| 105 | + git push origin origin/master:refs/heads/"$RELEASE_BRANCH" |
| 106 | + info "Branch '$RELEASE_BRANCH' created and pushed." |
| 107 | +fi |
| 108 | +echo "" |
| 109 | + |
| 110 | +# ============================================================================= |
| 111 | +# Phase 2: Trigger Init Release workflow and merge its PR |
| 112 | +# ============================================================================= |
| 113 | +info "Phase 2: Trigger Init Release workflow" |
| 114 | + |
| 115 | +if ! confirm "Trigger 'Init Release' workflow for branch '$RELEASE_BRANCH' with version '$APP_VERSION'?"; then |
| 116 | + error "Aborted." |
| 117 | + exit 1 |
| 118 | +fi |
| 119 | + |
| 120 | +gh workflow run init-branch-release.yaml \ |
| 121 | + --repo "$REPO" \ |
| 122 | + -f TARGET_BRANCH="$RELEASE_BRANCH" \ |
| 123 | + -f TARGET_VERSION="$APP_VERSION" |
| 124 | + |
| 125 | +info "Workflow triggered. Waiting for version bump PR to be created..." |
| 126 | + |
| 127 | +# Poll for the PR (created by the workflow targeting the release branch) |
| 128 | +MAX_ATTEMPTS=30 |
| 129 | +SLEEP_INTERVAL=10 |
| 130 | +PR_NUMBER="" |
| 131 | + |
| 132 | +for i in $(seq 1 $MAX_ATTEMPTS); do |
| 133 | + PR_NUMBER=$(gh pr list \ |
| 134 | + --repo "$REPO" \ |
| 135 | + --base "$RELEASE_BRANCH" \ |
| 136 | + --search "Bump version to $APP_VERSION" \ |
| 137 | + --json number \ |
| 138 | + --jq '.[0].number // empty' 2>/dev/null || true) |
| 139 | + |
| 140 | + if [[ -n "$PR_NUMBER" ]]; then |
| 141 | + info "Found PR #$PR_NUMBER" |
| 142 | + break |
| 143 | + fi |
| 144 | + echo -n "." |
| 145 | + sleep "$SLEEP_INTERVAL" |
| 146 | +done |
| 147 | + |
| 148 | +if [[ -z "$PR_NUMBER" ]]; then |
| 149 | + error "Timed out waiting for Init Release PR. Check workflow status at:" |
| 150 | + error " https://github.com/$REPO/actions/workflows/init-branch-release.yaml" |
| 151 | + exit 1 |
| 152 | +fi |
| 153 | + |
| 154 | +info "PR: https://github.com/$REPO/pull/$PR_NUMBER" |
| 155 | + |
| 156 | +if ! confirm "Merge PR #$PR_NUMBER (version bump to $APP_VERSION)?"; then |
| 157 | + error "Aborted. PR is still open: https://github.com/$REPO/pull/$PR_NUMBER" |
| 158 | + exit 1 |
| 159 | +fi |
| 160 | + |
| 161 | +gh pr merge "$PR_NUMBER" --repo "$REPO" --merge |
| 162 | +info "PR #$PR_NUMBER merged." |
| 163 | +echo "" |
| 164 | + |
| 165 | +# ============================================================================= |
| 166 | +# Phase 3: Create GitHub release |
| 167 | +# ============================================================================= |
| 168 | +info "Phase 3: Create GitHub release '$TAG' targeting '$RELEASE_BRANCH'" |
| 169 | +info "This will trigger the release workflow (Docker image builds, GoReleaser)." |
| 170 | + |
| 171 | +if ! confirm "Create GitHub release '$TAG'?"; then |
| 172 | + error "Aborted." |
| 173 | + exit 1 |
| 174 | +fi |
| 175 | + |
| 176 | +gh release create "$TAG" \ |
| 177 | + --repo "$REPO" \ |
| 178 | + --target "$RELEASE_BRANCH" \ |
| 179 | + --title "Release $TAG" \ |
| 180 | + --generate-notes |
| 181 | + |
| 182 | +info "GitHub release created: https://github.com/$REPO/releases/tag/$TAG" |
| 183 | +info "Release workflow will run in the background." |
| 184 | +echo "" |
| 185 | + |
| 186 | +# ============================================================================= |
| 187 | +# Phase 4: Bump Helm chart and create PR |
| 188 | +# ============================================================================= |
| 189 | +info "Phase 4: Bump Helm chart version to $CHART_VERSION (appVersion: v$APP_VERSION)" |
| 190 | + |
| 191 | +HELM_BRANCH="release-helm-chart-v${CHART_VERSION}" |
| 192 | + |
| 193 | +if ! confirm "Create branch '$HELM_BRANCH', bump chart files, and open PR with 'release/helm-chart' label?"; then |
| 194 | + error "Aborted." |
| 195 | + exit 1 |
| 196 | +fi |
| 197 | + |
| 198 | +# Create branch from latest master |
| 199 | +git fetch origin master |
| 200 | +git checkout -b "$HELM_BRANCH" origin/master |
| 201 | + |
| 202 | +# Bump Chart.yaml: version and appVersion |
| 203 | +CHART_FILE="deployments/kubernetes/chart/reloader/Chart.yaml" |
| 204 | +sed -i "s/^version:.*/version: ${CHART_VERSION}/" "$CHART_FILE" |
| 205 | +sed -i "s/^appVersion:.*/appVersion: v${APP_VERSION}/" "$CHART_FILE" |
| 206 | + |
| 207 | +# Bump values.yaml: image.tag |
| 208 | +VALUES_FILE="deployments/kubernetes/chart/reloader/values.yaml" |
| 209 | +sed -i "s/^\( tag:\).*/\1 v${APP_VERSION}/" "$VALUES_FILE" |
| 210 | + |
| 211 | +# Show changes for review |
| 212 | +info "Changes:" |
| 213 | +git diff |
| 214 | + |
| 215 | +git add "$CHART_FILE" "$VALUES_FILE" |
| 216 | +git commit -m "Bump helm chart to ${CHART_VERSION} and appVersion to v${APP_VERSION}" |
| 217 | +git push origin "$HELM_BRANCH" |
| 218 | + |
| 219 | +HELM_PR_URL=$(gh pr create \ |
| 220 | + --repo "$REPO" \ |
| 221 | + --base master \ |
| 222 | + --head "$HELM_BRANCH" \ |
| 223 | + --title "Bump Helm chart to ${CHART_VERSION} (appVersion v${APP_VERSION})" \ |
| 224 | + --body "Bump Helm chart version to ${CHART_VERSION} and appVersion to v${APP_VERSION}." \ |
| 225 | + --label "release/helm-chart") |
| 226 | + |
| 227 | +HELM_PR_NUMBER=$(echo "$HELM_PR_URL" | grep -o '[0-9]*$') |
| 228 | +info "Helm chart PR created: $HELM_PR_URL" |
| 229 | + |
| 230 | +if ! confirm "Merge Helm chart PR #$HELM_PR_NUMBER?"; then |
| 231 | + error "Aborted. PR is still open: $HELM_PR_URL" |
| 232 | + exit 1 |
| 233 | +fi |
| 234 | + |
| 235 | +gh pr merge "$HELM_PR_NUMBER" --repo "$REPO" --merge |
| 236 | +info "Helm chart PR #$HELM_PR_NUMBER merged." |
| 237 | + |
| 238 | +# Return to previous branch |
| 239 | +git checkout - |
| 240 | + |
| 241 | +echo "" |
| 242 | +info "=============================================" |
| 243 | +info "Release $TAG complete!" |
| 244 | +info "=============================================" |
| 245 | +info "" |
| 246 | +info "Summary:" |
| 247 | +info " - Release branch: $RELEASE_BRANCH" |
| 248 | +info " - GitHub release: https://github.com/$REPO/releases/tag/$TAG" |
| 249 | +info " - Helm chart: $CHART_VERSION (appVersion: v$APP_VERSION)" |
| 250 | +info "" |
| 251 | +info "The release workflow is running in the background." |
| 252 | +info "Monitor at: https://github.com/$REPO/actions" |
0 commit comments