BASH
Automate Git Pull and Web Project Update
A bash script to automate pulling latest Git changes, installing Node.js dependencies, and building a web project for efficient updates and deployments.
#!/bin/bash
# Configuration
PROJECT_DIR="/var/www/mywebapp"
BRANCH="main"
echo "--- Starting Web Project Update ---"
# Navigate to the project directory
cd "$PROJECT_DIR" || { echo "Error: Project directory not found at $PROJECT_DIR"; exit 1; }
# Pull latest changes from Git
echo "Fetching latest changes from Git..."
git checkout "$BRANCH" || { echo "Error: Failed to checkout branch $BRANCH"; exit 1; }
git pull origin "$BRANCH" || { echo "Error: Failed to pull from Git"; exit 1; }
# Install Node.js dependencies (if package.json exists)
if [ -f "package.json" ]; then
echo "Installing Node.js dependencies..."
npm install || { echo "Error: npm install failed"; exit 1; }
echo "Building project..."
npm run build || { echo "Error: npm run build failed"; exit 1; }
else
echo "No package.json found, skipping npm operations."
fi
echo "--- Web Project Update Complete ---"
How it works: This script streamlines the process of updating a web project by automating several common steps. It navigates to a specified project directory, ensures the correct Git branch is checked out, pulls the latest changes from the remote repository, and then conditionally runs `npm install` and `npm run build` if a `package.json` file is detected. This makes it ideal for CI/CD pipelines or simple local development environment updates.