|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +# The script assumes the remote is named 'origin' but can be configured: |
| 5 | +REMOTE="origin" |
| 6 | + |
| 7 | +# Ask for confirmation before continuing the release |
| 8 | +function confirmOrAbort() { |
| 9 | + read -p "Do you want to continue? (y/N): " -n 1 -r |
| 10 | + echo |
| 11 | + if [[ ! $REPLY =~ ^[Yy]$ ]]; then |
| 12 | + echo "Aborting." |
| 13 | + exit 1 |
| 14 | + fi |
| 15 | +} |
| 16 | + |
| 17 | +# Check if current branch is either 'master' or 'release/v*' |
| 18 | +CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD) |
| 19 | +if [[ "$CURRENT_BRANCH" == "master" ]]; then |
| 20 | + MINOR_RELEASE=true |
| 21 | +elif [[ "$CURRENT_BRANCH" =~ ^release/v[0-9]+\.[0-9]+$ ]]; then |
| 22 | + MINOR_RELEASE=false |
| 23 | +else |
| 24 | + echo "❌ Please check out either 'master' branch or a 'release/v*' branch to perform a release first." |
| 25 | + exit 1 |
| 26 | +fi |
| 27 | +echo -n "✅ Current branch is '$CURRENT_BRANCH'. Performing a " |
| 28 | +if [ "$MINOR_RELEASE" = true ]; then |
| 29 | + echo "minor release." |
| 30 | +else |
| 31 | + echo "patch release." |
| 32 | +fi |
| 33 | + |
| 34 | +# Check if working copy is clean |
| 35 | +if ! git diff-index --quiet HEAD; then |
| 36 | + echo "❌ Working copy is not clean. Please commit or stash your changes before performing a release." |
| 37 | + exit 1 |
| 38 | +fi |
| 39 | + |
| 40 | +# Check if clone is up to date |
| 41 | +if ! git diff-index --quiet "$REMOTE/$CURRENT_BRANCH"; then |
| 42 | + echo "❌ Working copy is not up to date with $REMOTE/$CURRENT_BRANCH. Please pull the latest changes before performing a release." |
| 43 | + exit 1 |
| 44 | +fi |
| 45 | +echo "✅ Working copy is clean and up-to-date." |
| 46 | + |
| 47 | +# Check the git log history |
| 48 | +LAST_RELEASE_TAG=$(git describe --tags --abbrev=0) |
| 49 | +echo "ℹ️ Last release version: $LAST_RELEASE_TAG" |
| 50 | +SUSPICIOUS_COMMITS=$(git log --oneline --first-parent "$LAST_RELEASE_TAG"..HEAD | grep -E -v "Merge pull request #" | grep -E -v "\(#") |
| 51 | +if [ -n "$SUSPICIOUS_COMMITS" ]; then |
| 52 | + echo "❌ The following commits are not merge commits and may not be suitable for a release:" |
| 53 | + echo "$SUSPICIOUS_COMMITS" |
| 54 | + echo "Please review these commits before proceeding with the release." |
| 55 | + confirmOrAbort |
| 56 | +else |
| 57 | + echo "✅ All commits since the last release are merge commits." |
| 58 | +fi |
| 59 | + |
| 60 | +# Get the next release version |
| 61 | +VERSION=$(echo "$LAST_RELEASE_TAG" | grep -E '^v[0-9]+\.[0-9]+\.0$' | sed 's/^v//') |
| 62 | +if [ -z "$VERSION" ]; then |
| 63 | + echo "❌ Unable to determine the next release version from the last release tag: $LAST_RELEASE_TAG" |
| 64 | + exit 1 |
| 65 | +fi |
| 66 | +if [ "$MINOR_RELEASE" = true ]; then |
| 67 | + NEXT_RELEASE_VERSION=$(echo "$VERSION" | awk -F. '{printf "v%d.%d.0", $1, $2 + 1}') |
| 68 | +else |
| 69 | + NEXT_RELEASE_VERSION=$(echo "$VERSION" | awk -F. '{printf "v%d.%d.%d", $1, $2, $3 + 1}') |
| 70 | +fi |
| 71 | +echo "ℹ️ Next release version: $NEXT_RELEASE_VERSION" |
| 72 | + |
| 73 | +# Create and push the release tag |
| 74 | +echo "ℹ️ The release tag will be created and pushed. No abort is possible after this point." |
| 75 | +confirmOrAbort |
| 76 | +echo git tag -a -s -m "Release $NEXT_RELEASE_VERSION" "$NEXT_RELEASE_VERSION" |
| 77 | +echo git push "$REMOTE" "$NEXT_RELEASE_VERSION" --no-verify |
| 78 | +echo "✅ Release tag $NEXT_RELEASE_VERSION created and pushed to $REMOTE." |
0 commit comments