BASH
Automating Website Deployment with Git Pull and Service Restart
Streamline your web deployments with this Bash script that pulls the latest code from Git, updates dependencies, and restarts essential services like Nginx or PHP-FPM.
#!/bin/bash
PROJECT_ROOT="/var/www/mywebsite"
SERVICE_NAME="nginx" # or "apache2", "php-fpm", "gunicorn", etc.
echo "Starting deployment for $PROJECT_ROOT..."
# Navigate to the project directory
cd "$PROJECT_ROOT" || { echo "Error: Project directory not found."; exit 1; }
# Pull the latest code from Git
echo "Pulling latest code from Git..."
git pull origin main || { echo "Error: Git pull failed."; exit 1; }
# Optional: Install/update dependencies (e.g., Composer for PHP, npm for Node.js)
# echo "Installing/updating dependencies..."
# composer install --no-dev --optimize-autoloader # For PHP
# npm install --production # For Node.js
# Optional: Clear caches or build assets
# php artisan cache:clear # For Laravel
# npm run build # For frontend assets
# Restart the web server or application service
echo "Restarting $SERVICE_NAME service..."
sudo systemctl restart "$SERVICE_NAME" || { echo "Error: Failed to restart $SERVICE_NAME."; exit 1; }
echo "Deployment completed successfully!"
How it works: This script automates the deployment process for a web application. It navigates to the project directory, pulls the latest code from a Git repository, and then restarts a specified service (like a web server or application server) to apply the changes. Optional steps for dependency installation and asset building are commented out for customization.