|
| 1 | +#!/bin/bash |
| 2 | +# Pre-release checks for Maven Central deploy. Exits non-zero on any failure |
| 3 | +# so subsequent steps don't proceed if anything looks off. |
| 4 | +# |
| 5 | +# Usage: scripts/preflight-release.sh [expected-version] |
| 6 | +# |
| 7 | +# If expected-version is given, fails when pom.xml's version doesn't match. |
| 8 | +set -e |
| 9 | + |
| 10 | +EXPECTED_VERSION="${1:-}" |
| 11 | +PROJECT_NAME=$(basename "$(pwd)") |
| 12 | +FAIL=0 |
| 13 | + |
| 14 | +red() { printf "\033[0;31m%s\033[0m\n" "$*"; } |
| 15 | +green() { printf "\033[0;32m%s\033[0m\n" "$*"; } |
| 16 | +yellow() { printf "\033[0;33m%s\033[0m\n" "$*"; } |
| 17 | + |
| 18 | +echo "=== ${PROJECT_NAME} pre-release preflight ===" |
| 19 | +echo |
| 20 | + |
| 21 | +# 1. Off corporate zscaler / network reaches Sonatype Central Portal |
| 22 | +echo "[1/7] Network connectivity to Sonatype Central Portal..." |
| 23 | +if curl -sS -o /dev/null -m 8 -w "%{http_code}" "https://central.sonatype.com/" 2>/dev/null | grep -qE "^(200|301|302)$"; then |
| 24 | + green " ✓ central.sonatype.com reachable" |
| 25 | +else |
| 26 | + red " ✗ central.sonatype.com NOT reachable (zscaler enabled? VPN?)" |
| 27 | + FAIL=$((FAIL + 1)) |
| 28 | +fi |
| 29 | + |
| 30 | +# 2. Working tree clean |
| 31 | +echo "[2/7] Working tree clean..." |
| 32 | +if [ -z "$(git status --porcelain)" ]; then |
| 33 | + green " ✓ no uncommitted changes" |
| 34 | +else |
| 35 | + red " ✗ uncommitted changes detected:" |
| 36 | + git status --porcelain | sed 's/^/ /' |
| 37 | + FAIL=$((FAIL + 1)) |
| 38 | +fi |
| 39 | + |
| 40 | +# 3. On master branch |
| 41 | +echo "[3/7] On master branch..." |
| 42 | +BRANCH=$(git rev-parse --abbrev-ref HEAD) |
| 43 | +if [ "$BRANCH" = "master" ]; then |
| 44 | + green " ✓ on master" |
| 45 | +else |
| 46 | + red " ✗ on '${BRANCH}' (expected master)" |
| 47 | + FAIL=$((FAIL + 1)) |
| 48 | +fi |
| 49 | + |
| 50 | +# 4. In sync with origin (not behind; ahead is OK — those will get released) |
| 51 | +echo "[4/7] In sync with origin/master..." |
| 52 | +git fetch origin --quiet 2>/dev/null || true |
| 53 | +BEHIND=$(git rev-list HEAD..origin/master --count 2>/dev/null || echo 0) |
| 54 | +AHEAD=$(git rev-list origin/master..HEAD --count 2>/dev/null || echo 0) |
| 55 | +if [ "$BEHIND" -eq 0 ]; then |
| 56 | + if [ "$AHEAD" -eq 0 ]; then |
| 57 | + green " ✓ identical to origin/master" |
| 58 | + else |
| 59 | + green " ✓ ${AHEAD} commit(s) ahead of origin/master (these will be in the release)" |
| 60 | + fi |
| 61 | +else |
| 62 | + red " ✗ ${BEHIND} commit(s) behind origin/master — pull before releasing" |
| 63 | + FAIL=$((FAIL + 1)) |
| 64 | +fi |
| 65 | + |
| 66 | +# 5. Version in pom.xml |
| 67 | +echo "[5/7] Version check..." |
| 68 | +VERSION=$(grep -m1 '<version>' pom.xml | sed -E 's|.*<version>([^<]+)</version>.*|\1|') |
| 69 | +if [ -n "$EXPECTED_VERSION" ]; then |
| 70 | + if [ "$VERSION" = "$EXPECTED_VERSION" ]; then |
| 71 | + green " ✓ pom.xml version is ${VERSION}" |
| 72 | + else |
| 73 | + red " ✗ pom.xml version is ${VERSION} (expected ${EXPECTED_VERSION})" |
| 74 | + FAIL=$((FAIL + 1)) |
| 75 | + fi |
| 76 | +else |
| 77 | + green " ✓ pom.xml version is ${VERSION} (no expected version supplied)" |
| 78 | +fi |
| 79 | +if echo "$VERSION" | grep -q "SNAPSHOT"; then |
| 80 | + red " ✗ pom.xml is a SNAPSHOT — release versions must not contain SNAPSHOT" |
| 81 | + FAIL=$((FAIL + 1)) |
| 82 | +fi |
| 83 | + |
| 84 | +# 6. GPG key available |
| 85 | +echo "[6/7] GPG key..." |
| 86 | +if gpg --list-secret-keys --keyid-format=long 2>/dev/null | grep -q '^sec'; then |
| 87 | + KEYID=$(gpg --list-secret-keys --keyid-format=long 2>/dev/null | grep '^sec' | head -1 | awk '{print $2}') |
| 88 | + green " ✓ GPG secret key available (${KEYID})" |
| 89 | +else |
| 90 | + red " ✗ no GPG secret key found — gpg signing will fail" |
| 91 | + FAIL=$((FAIL + 1)) |
| 92 | +fi |
| 93 | + |
| 94 | +# 7. Maven settings.xml has Sonatype credentials |
| 95 | +echo "[7/7] Maven settings.xml has Sonatype credentials..." |
| 96 | +SETTINGS_FILE="${HOME}/.m2/settings.xml" |
| 97 | +if [ -f "$SETTINGS_FILE" ]; then |
| 98 | + if grep -qE 'central|sonatype|ossrh' "$SETTINGS_FILE" 2>/dev/null; then |
| 99 | + green " ✓ settings.xml has central/sonatype/ossrh server entry" |
| 100 | + else |
| 101 | + yellow " ⚠ settings.xml exists but no central/sonatype/ossrh entry — verify credentials" |
| 102 | + fi |
| 103 | +else |
| 104 | + red " ✗ ~/.m2/settings.xml not found" |
| 105 | + FAIL=$((FAIL + 1)) |
| 106 | +fi |
| 107 | + |
| 108 | +echo |
| 109 | +if [ "$FAIL" -eq 0 ]; then |
| 110 | + green "=== preflight: PASS — ready to deploy ===" |
| 111 | + exit 0 |
| 112 | +else |
| 113 | + red "=== preflight: ${FAIL} check(s) FAILED — fix before deploying ===" |
| 114 | + exit 1 |
| 115 | +fi |
0 commit comments