BASH
Automate Git Pull and Service Restart
A simple bash script to automate pulling the latest code from a Git repository and restarting a web service, ideal for basic deployment workflows.
#!/bin/bash
REPO_PATH="/var/www/mywebapp"
SERVICE_NAME="nginx"
echo "Navigating to repository: $REPO_PATH"
cd "$REPO_PATH" || { echo "Error: Could not change directory to $REPO_PATH"; exit 1; }
echo "Pulling latest changes from Git..."
git pull origin main || { echo "Error: Git pull failed"; exit 1; }
echo "Restarting service: $SERVICE_NAME"
sudo systemctl restart "$SERVICE_NAME" || { echo "Error: Failed to restart $SERVICE_NAME"; exit 1; }
echo "Deployment complete!"
How it works: This script navigates to a specified Git repository, pulls the latest changes from the 'main' branch, and then restarts a defined system service (e.g., Nginx, Apache, or a Node.js process managed by systemd). It includes error handling for each step, ensuring the script exits if a command fails, providing a robust solution for automating simple deployment tasks.