BASH
Automate Web Project Update with Git Pull and Build
Streamline your web development workflow with a Bash script that pulls the latest code from Git, installs npm dependencies, and runs build commands for project updates.
#!/bin/bash
# Configuration
PROJECT_DIR="/var/www/mywebapp" # Set your project directory
GIT_BRANCH="main" # Set your main Git branch
# Navigate to the project directory
cd "$PROJECT_DIR" || {
echo "Error: Project directory '$PROJECT_DIR' not found." >&2
exit 1
}
echo "Pulling latest changes from Git branch: $GIT_BRANCH..."
git pull origin "$GIT_BRANCH"
# Check if git pull was successful
if [ $? -eq 0 ]; then
echo "Git pull successful. Installing dependencies and building project..."
# Install Node.js dependencies
npm install
if [ $? -ne 0 ]; then
echo "Error: npm install failed." >&2
exit 1
fi
# Run the build command
npm run build
if [ $? -ne 0 ]; then
echo "Error: npm run build failed." >&2
exit 1
fi
echo "Project update and build complete."
else
echo "Error: Git pull failed. Please check for conflicts or connectivity." >&2
exit 1
fi
How it works: This script automates the process of updating a web project deployed on a server or a local development environment. It first navigates to the specified project directory, then pulls the latest changes from a configured Git branch. Upon a successful pull, it proceeds to install Node.js dependencies using `npm install` and then executes the project's build command with `npm run build`. Basic error handling is included to ensure smooth execution and provide feedback if any step fails.