|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | + |
| 6 | +# Usage function |
| 7 | +usage() { |
| 8 | + echo "Usage: ./scripts/release.sh <version> [--dry-run] [--skip-push]" |
| 9 | +} |
| 10 | + |
| 11 | +# Parse arguments |
| 12 | +VERSION="" |
| 13 | +DRY_RUN=false |
| 14 | +SKIP_PUSH=false |
| 15 | + |
| 16 | +while [[ $# -gt 0 ]]; do |
| 17 | + case $1 in |
| 18 | + --dry-run) |
| 19 | + DRY_RUN=true |
| 20 | + shift |
| 21 | + ;; |
| 22 | + --skip-push) |
| 23 | + SKIP_PUSH=true |
| 24 | + shift |
| 25 | + ;; |
| 26 | + -h|--help) |
| 27 | + usage |
| 28 | + exit 0 |
| 29 | + ;; |
| 30 | + *) |
| 31 | + if [[ -z "$VERSION" ]]; then |
| 32 | + VERSION="$1" |
| 33 | + else |
| 34 | + echo "Error: Unknown argument: $1" >&2 |
| 35 | + usage |
| 36 | + exit 1 |
| 37 | + fi |
| 38 | + shift |
| 39 | + ;; |
| 40 | + esac |
| 41 | +done |
| 42 | + |
| 43 | +if [[ -z "$VERSION" ]]; then |
| 44 | + echo "Error: Version is required" >&2 |
| 45 | + usage |
| 46 | + exit 1 |
| 47 | +fi |
| 48 | + |
| 49 | +# Validate version format (basic semver check) |
| 50 | +if [[ ! "$VERSION" =~ ^v[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9.-]+)?$ ]]; then |
| 51 | + echo "Error: Version must follow semantic versioning format (e.g., v1.2.3 or v1.2.3-beta.1)" >&2 |
| 52 | + exit 1 |
| 53 | +fi |
| 54 | + |
| 55 | +if ! git diff-index --quiet HEAD --; then |
| 56 | + echo "Error: Working directory is not clean." >&2 |
| 57 | + git status --porcelain |
| 58 | + exit 1 |
| 59 | +fi |
| 60 | + |
| 61 | +# Check if local branch is in sync with remote |
| 62 | +CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD) |
| 63 | +git fetch origin "$CURRENT_BRANCH" > /dev/null 2>&1 || { |
| 64 | + echo "Error: Failed to fetch remote branch '$CURRENT_BRANCH'" >&2 |
| 65 | + exit 1 |
| 66 | +} |
| 67 | + |
| 68 | +LOCAL_COMMIT=$(git rev-parse HEAD) |
| 69 | +REMOTE_COMMIT=$(git rev-parse "origin/$CURRENT_BRANCH" 2>/dev/null || echo "") |
| 70 | + |
| 71 | +if [[ -z "$REMOTE_COMMIT" ]]; then |
| 72 | + echo "Error: Remote branch 'origin/$CURRENT_BRANCH' does not exist" >&2 |
| 73 | + exit 1 |
| 74 | +fi |
| 75 | + |
| 76 | +if [[ "$LOCAL_COMMIT" != "$REMOTE_COMMIT" ]]; then |
| 77 | + echo "Error: Local branch '$CURRENT_BRANCH' is not in sync with remote 'origin/$CURRENT_BRANCH'" >&2 |
| 78 | + echo "Local: $LOCAL_COMMIT" |
| 79 | + echo "Remote: $REMOTE_COMMIT" |
| 80 | + echo "Please pull or push to sync before releasing." |
| 81 | + exit 1 |
| 82 | +fi |
| 83 | + |
| 84 | +if git tag --list | grep -q "^$VERSION$"; then |
| 85 | + echo "Error: Version '$VERSION' already exists locally" >&2 |
| 86 | + exit 1 |
| 87 | +fi |
| 88 | + |
| 89 | +# Check remote tags |
| 90 | +git fetch --tags > /dev/null 2>&1 || true |
| 91 | +if git ls-remote --tags origin | grep -q "refs/tags/$VERSION$"; then |
| 92 | + echo "Error: Version '$VERSION' already exists on remote" >&2 |
| 93 | + exit 1 |
| 94 | +fi |
| 95 | + |
| 96 | +# Show release information |
| 97 | +COMMIT=$(git rev-parse HEAD) |
| 98 | +SHORT_COMMIT=$(git rev-parse --short HEAD) |
| 99 | +REPO_URL=$(git config --get remote.origin.url | sed 's/git@github.com:/https:\/\/github.com\//' | sed 's/\.git$//') |
| 100 | +LAST_TAG=$(git tag --sort=-version:refname | grep -v -- '-rc' | head -n 1 2>/dev/null || echo "") |
| 101 | + |
| 102 | +echo "================================================" |
| 103 | +echo " Braintrust Spec Release" |
| 104 | +echo "================================================" |
| 105 | +printf "%-13s %s\n" "version:" "$VERSION" |
| 106 | +printf "%-13s %s\n" "commit:" "$SHORT_COMMIT" |
| 107 | +printf "%-13s %s\n" "code:" "$REPO_URL/commit/$COMMIT" |
| 108 | +if [[ -n "$LAST_TAG" ]]; then |
| 109 | + printf "%-13s %s\n" "changeset:" "$REPO_URL/compare/$LAST_TAG...$COMMIT" |
| 110 | +else |
| 111 | + printf "%-13s %s\n" "changeset:" "$REPO_URL/commits/$COMMIT" |
| 112 | +fi |
| 113 | +echo "" |
| 114 | + |
| 115 | +# Confirmation prompt (skip in dry-run) |
| 116 | +if [[ "$DRY_RUN" == true ]]; then |
| 117 | + echo "dry-run was requested. Bailing" |
| 118 | + exit 0 |
| 119 | +fi |
| 120 | + |
| 121 | +read -p "Are you ready to release version $VERSION? Type 'YOLO' to continue: " -r |
| 122 | +echo "" |
| 123 | +if [[ "$REPLY" != "YOLO" ]]; then |
| 124 | + echo "Release aborted" |
| 125 | + exit 0 |
| 126 | +fi |
| 127 | + |
| 128 | +if ! ./scripts/test.sh; then |
| 129 | + echo "Error: tests failed" >&2 |
| 130 | + exit 1 |
| 131 | +fi |
| 132 | + |
| 133 | +git tag -a "$VERSION" -m "Release $VERSION" |
| 134 | +if [[ "$SKIP_PUSH" == true ]]; then |
| 135 | + echo "skip-push was requested. tag is created locally but not pushed. Do what you will." |
| 136 | + exit 0 |
| 137 | +fi |
| 138 | +git push origin "$VERSION" |
| 139 | + |
| 140 | +echo "================================================" |
| 141 | +echo " Release Complete!" |
| 142 | +echo "================================================" |
| 143 | +echo "Version $VERSION has been created and pushed to origin." |
| 144 | +echo "" |
| 145 | +echo "View changelog: $REPO_URL/releases/tag/$VERSION" |
0 commit comments