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