femto-webapp/scripts/publish.sh
2025-06-17 10:47:46 +02:00

101 lines
3.2 KiB
Bash
Executable file

#!/bin/bash
set -euo pipefail
# Function to display help text
show_help() {
echo "Usage: $0 [OPTIONS]"
echo
echo "Description:"
echo " This script automates the process of bumping the version, building a Docker image,"
echo " pushing it to the registry, and optionally deploying to production."
echo
echo "Options:"
echo " -h,--help Display this help message and exit"
echo " -d,--deploy Deploy to production after building and pushing"
echo " --major Bump the major version (x.0.0)"
echo " --minor Bump the minor version (0.x.0)"
echo " --patch Bump the patch version (0.0.x) [default]"
echo
echo "Examples:"
echo " $0 # Bump patch version, build and push"
echo " $0 --minor # Bump minor version, build and push"
echo " $0 --major -d # Bump major version, build, push and deploy"
echo " $0 --patch --deploy # Bump patch version, build, push and deploy"
echo
}
# Parse command line arguments
DEPLOY=false
VERSION_TYPE="patch" # Default to patch version bump
for arg in "$@"; do
case "$arg" in
-h|--help) show_help; exit 0 ;;
-d|--deploy) DEPLOY=true ;;
--major) VERSION_TYPE="major" ;;
--minor) VERSION_TYPE="minor" ;;
--patch) VERSION_TYPE="patch" ;;
*) echo "Unknown option: $arg"; echo "Usage: $0 [-h|--help] [-d|--deploy] [--major|--minor|--patch]"; exit 1 ;;
esac
done
# CONFIGURATION
REGISTRY="docker.botris.dev"
USERNAME="johnbotris"
IMAGE_NAME="femto-webapp"
# Add this before the docker build line
export VITE_API_URL="https://api.botris.social"
# Step 0: Ensure clean working directory
if [[ -n $(git status --porcelain) ]]; then
echo "❌ Uncommitted changes detected. Please commit or stash them before running this script."
exit 1
fi
# Step 1: Store current version to revert if needed
OLD_VERSION=$(node -p "require('./package.json').version")
echo "🔍 Current version: $OLD_VERSION"
# Step 2: Bump version without Git tag/commit
echo "🚀 Bumping $VERSION_TYPE version..."
yarn version --$VERSION_TYPE --no-git-tag-version
NEW_VERSION=$(node -p "require('./package.json').version")
echo "📦 New version: $NEW_VERSION"
# Step 3: Attempt Docker build
echo "🔧 Building Docker image..."
if ! docker build --build-arg VITE_API_URL="$VITE_API_URL" -t $IMAGE_NAME .; then
echo "❌ Docker build failed. Reverting version bump..."
git checkout -- package.json yarn.lock
exit 1
fi
# Step 4: Tag and push Docker image
FULL_IMAGE="$REGISTRY/$USERNAME/$IMAGE_NAME"
echo "🏷️ Tagging Docker image..."
docker tag $IMAGE_NAME $FULL_IMAGE:$NEW_VERSION
docker tag $IMAGE_NAME $FULL_IMAGE:latest
echo "📤 Pushing Docker images..."
docker push $FULL_IMAGE:$NEW_VERSION
docker push $FULL_IMAGE:latest
# Step 5: Commit version bump & tag
echo "✅ Committing and tagging version bump..."
git add package.json yarn.lock
git commit -m "v$NEW_VERSION"
git tag "v$NEW_VERSION"
git push origin main
git push origin "v$NEW_VERSION"
echo "🎉 Release v$NEW_VERSION complete."
# Step 6: Deploy if flag is set
if [ "$DEPLOY" = true ]; then
echo "🚀 Deploying to production..."
ssh john@botris.social 'bash /home/john/docker/femto/update.sh'
echo "✅ Deployment complete."
fi