BASH
Automate Simple Website Deployment with Git and Service Restart
Create a bash script to automate website deployment by pulling the latest code from Git and restarting a configured web service or application process.
#!/bin/bash
# Configuration
REPO_PATH="/var/www/mywebsite"
SERVICE_NAME="nginx" # Or 'apache2', 'node-app', etc.
echo "Starting deployment for $(basename $REPO_PATH)"
# Navigate to the repository directory
cd $REPO_PATH || { echo "Error: Repository path not found."; exit 1; }
# Pull the latest changes from Git
echo "Pulling latest code from Git..."
git pull origin main || { echo "Error: Git pull failed."; exit 1; }
# Optional: Run build steps (e.g., npm install, npm run build)
# echo "Running build steps..."
# npm install && npm run build || { echo "Error: Build failed."; exit 1; }
# Restart the web service
echo "Restarting service: $SERVICE_NAME..."
sudo systemctl restart $SERVICE_NAME || { echo "Error: Failed to restart service $SERVICE_NAME."; exit 1; }
echo "Deployment complete!"
How it works: This bash script automates a basic website deployment. It navigates to a specified repository path, pulls the latest code from the 'main' branch using Git, and then restarts a systemd service (e.g., Nginx, Apache, or a Node.js application managed by systemd). Error handling is included to ensure the script exits if a step fails. Optional build steps can be uncommented and adapted for projects requiring compilation.