Created
July 7, 2025 05:04
-
-
Save swikars1/c0cc70cdc2a92ef3c448471eecc5c436 to your computer and use it in GitHub Desktop.
Local to Server Deployment using Rsync for Docker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # EOL Website Deployment Script | |
| # Compatible with macOS and Linux | |
| # Usage: ./deploy.sh [environment] | |
| # Environment: staging (default) | production | |
| set -e # Exit on any error | |
| # Check if we're on macOS | |
| if [[ "$OSTYPE" == "darwin"* ]]; then | |
| echo "π Running on macOS" | |
| fi | |
| # Configuration as your needs | |
| LOCAL_SRC_PATH="/Users/username/projectpath/project_name/src/" | |
| SSH_KEY="~/.ssh/your-ssh-key.pem" | |
| REMOTE_USER="ec2-user" | |
| # Server IP | |
| REMOTE_HOST="xx.xxx.xxx.xxx" | |
| REMOTE_PROJECT_PATH="/home/ec2-user/projects/staging_projectname" | |
| # Colors for output | |
| RED='\033[0;31m' | |
| GREEN='\033[0;32m' | |
| YELLOW='\033[1;33m' | |
| BLUE='\033[0;34m' | |
| NC='\033[0m' # No Color | |
| # Environment handling | |
| ENVIRONMENT=${1:-staging} | |
| if [ "$ENVIRONMENT" = "production" ]; then | |
| REMOTE_PROJECT_PATH="/home/ec2-user/projects/production_projectname" | |
| echo -e "${YELLOW}β οΈ DEPLOYING TO PRODUCTION${NC}" | |
| else | |
| echo -e "${BLUE}π¦ DEPLOYING TO STAGING${NC}" | |
| fi | |
| echo "Are you sure you want to deploy to $ENVIRONMENT? (y/N)" | |
| read -r confirmation | |
| if [[ ! "$confirmation" =~ ^[Yy]$ ]]; then | |
| echo "Deployment cancelled." | |
| exit 0 | |
| fi | |
| echo -e "${BLUE}π Starting deployment to $ENVIRONMENT...${NC}" | |
| # Function to print step headers | |
| print_step() { | |
| echo -e "\n${BLUE}==== $1 ====${NC}" | |
| } | |
| # Function to handle errors | |
| handle_error() { | |
| echo -e "${RED}β Error: $1${NC}" | |
| exit 1 | |
| } | |
| # Validate local source directory exists | |
| if [ ! -d "$LOCAL_SRC_PATH" ]; then | |
| handle_error "Local source directory not found: $LOCAL_SRC_PATH" | |
| fi | |
| # Validate SSH key exists | |
| if [ ! -f "${SSH_KEY/#\~/$HOME}" ]; then | |
| handle_error "SSH key not found: $SSH_KEY" | |
| fi | |
| print_step "1. Syncing files to remote server" | |
| echo "Source: $LOCAL_SRC_PATH" | |
| echo "Target: $REMOTE_USER@$REMOTE_HOST:$REMOTE_PROJECT_PATH/src/" | |
| rsync -avzP \ | |
| --backup \ | |
| --backup-dir=../backups/$(date +%Y%m%d_%H%M%S) \ | |
| --exclude '.git/' \ | |
| --exclude 'node_modules/' \ | |
| --exclude '.DS_Store' \ | |
| --exclude '*.log' \ | |
| --exclude '.env.local' \ | |
| -e "ssh -i $SSH_KEY" \ | |
| "$LOCAL_SRC_PATH" \ | |
| "$REMOTE_USER@$REMOTE_HOST:$REMOTE_PROJECT_PATH/src/" || handle_error "Failed to sync files" | |
| print_step "2. Restarting services on remote server" | |
| ssh -i "$SSH_KEY" "$REMOTE_USER@$REMOTE_HOST" << EOF || handle_error "Failed to restart services" | |
| cd $REMOTE_PROJECT_PATH | |
| echo "Current directory: \$(pwd)" | |
| echo "Restarting Docker services..." | |
| docker-compose restart | |
| echo "Checking service status..." | |
| docker-compose ps | |
| EOF | |
| echo -e "\n${GREEN}β Deployment to $ENVIRONMENT completed successfully!${NC}" | |
| echo -e "${BLUE}π Summary:${NC}" | |
| echo " - Files synced to: $REMOTE_HOST:$REMOTE_PROJECT_PATH/src/" | |
| echo " - Docker services restarted" | |
| echo " - Backup created with timestamp: $(date +%Y%m%d_%H%M%S)" | |
| # Optional: Health check | |
| echo -e "\n${YELLOW}π‘ Tip: You can check the application status by running:${NC}" | |
| echo "ssh -i $SSH_KEY $REMOTE_USER@$REMOTE_HOST 'cd $REMOTE_PROJECT_PATH && docker-compose logs --tail=50'" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment