deploy.sh•6.2 kB
#!/bin/bash
# Deploy script for Radius MCP Server
# Supports different environments and deployment strategies
set -e # Exit on any error
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Default values
ENVIRONMENT="dev"
NAMESPACE="radius-mcp-server"
DRY_RUN=false
FORCE=false
# Function to print colored output
print_status() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Function to show usage
show_usage() {
echo "Usage: $0 [OPTIONS]"
echo ""
echo "Options:"
echo " -e, --environment ENV Target environment (dev|testing|prod20) [default: dev]"
echo " -n, --namespace NS Kubernetes namespace [default: radius-mcp-server]"
echo " -d, --dry-run Show what would be deployed without actually deploying"
echo " -f, --force Force deployment even if tests fail"
echo " -h, --help Show this help message"
echo ""
echo "Examples:"
echo " $0 -e dev # Deploy to development"
echo " $0 -e testing -d # Dry run for testing environment"
echo " $0 -e prod20 -f # Force deploy to production"
}
# Parse command line arguments
while [[ $# -gt 0 ]]; do
case $1 in
-e|--environment)
ENVIRONMENT="$2"
shift 2
;;
-n|--namespace)
NAMESPACE="$2"
shift 2
;;
-d|--dry-run)
DRY_RUN=true
shift
;;
-f|--force)
FORCE=true
shift
;;
-h|--help)
show_usage
exit 0
;;
*)
print_error "Unknown option: $1"
show_usage
exit 1
;;
esac
done
# Validate environment
case $ENVIRONMENT in
dev|testing|prod20)
print_status "Target environment: $ENVIRONMENT"
;;
*)
print_error "Invalid environment: $ENVIRONMENT"
print_error "Valid environments: dev, testing, prod20"
exit 1
;;
esac
# Set namespace based on environment if not specified
if [[ "$NAMESPACE" == "radius-mcp-server" ]]; then
case $ENVIRONMENT in
dev)
NAMESPACE="radius-mcp-server-dev"
;;
testing)
NAMESPACE="radius-mcp-server-testing"
;;
prod20)
NAMESPACE="radius-mcp-server-prod"
;;
esac
fi
print_status "Target namespace: $NAMESPACE"
# Check if werf is available
if ! command -v werf &> /dev/null; then
print_error "werf is not installed or not in PATH"
print_error "Please install werf: https://werf.io/install.html"
exit 1
fi
# Check if kubectl is available
if ! command -v kubectl &> /dev/null; then
print_error "kubectl is not installed or not in PATH"
exit 1
fi
# Check if we're in a Git repository
if ! git rev-parse --git-dir > /dev/null 2>&1; then
print_error "Not in a Git repository"
exit 1
fi
# Get current branch
CURRENT_BRANCH=$(git branch --show-current)
print_status "Current branch: $CURRENT_BRANCH"
# Validate branch for environment
case $ENVIRONMENT in
dev)
if [[ "$CURRENT_BRANCH" != "develop" ]]; then
print_warning "You're not on 'develop' branch. Current: $CURRENT_BRANCH"
if [[ "$FORCE" != "true" ]]; then
print_error "Use --force to deploy from non-standard branch"
exit 1
fi
fi
;;
testing)
if [[ "$CURRENT_BRANCH" != "testing" ]]; then
print_warning "You're not on 'testing' branch. Current: $CURRENT_BRANCH"
if [[ "$FORCE" != "true" ]]; then
print_error "Use --force to deploy from non-standard branch"
exit 1
fi
fi
;;
prod20)
if [[ "$CURRENT_BRANCH" != "prod20" ]]; then
print_warning "You're not on 'prod20' branch. Current: $CURRENT_BRANCH"
if [[ "$FORCE" != "true" ]]; then
print_error "Use --force to deploy from non-standard branch"
exit 1
fi
fi
;;
esac
# Run tests unless forced
if [[ "$FORCE" != "true" ]]; then
print_status "Running tests before deployment..."
if ! npm run test; then
print_error "Tests failed. Use --force to deploy anyway"
exit 1
fi
print_success "All tests passed"
else
print_warning "Skipping tests (--force specified)"
fi
# Check if values file exists
VALUES_FILE=".helm/values.${ENVIRONMENT}.yaml"
if [[ ! -f "$VALUES_FILE" ]]; then
print_error "Values file not found: $VALUES_FILE"
exit 1
fi
print_status "Using values file: $VALUES_FILE"
# Build the application
print_status "Building application..."
if ! npm run build; then
print_error "Build failed"
exit 1
fi
print_success "Build completed"
# Prepare werf command
WERF_CMD="werf converge"
WERF_CMD="$WERF_CMD --releases-history-max=3"
WERF_CMD="$WERF_CMD --values $VALUES_FILE"
WERF_CMD="$WERF_CMD --namespace $NAMESPACE"
if [[ "$DRY_RUN" == "true" ]]; then
WERF_CMD="$WERF_CMD --dry-run"
print_status "DRY RUN MODE - No actual deployment will occur"
fi
# Deploy
print_status "Deploying to $ENVIRONMENT environment..."
print_status "Command: $WERF_CMD"
if eval $WERF_CMD; then
if [[ "$DRY_RUN" == "true" ]]; then
print_success "Dry run completed successfully"
else
print_success "Deployment completed successfully"
# Show deployment status
print_status "Checking deployment status..."
kubectl get pods -n $NAMESPACE -l app.kubernetes.io/name=radius-mcp-server
# Show service URL if available
print_status "Service URLs:"
kubectl get ingress -n $NAMESPACE -o jsonpath='{.items[*].spec.rules[*].host}' 2>/dev/null || echo "No ingress found"
fi
else
print_error "Deployment failed"
exit 1
fi
print_success "Deploy script completed"