BASH

Automate Git Pull, Dependencies, and Service Restart for Deployment

Streamline your web application deployment with a bash script that pulls the latest Git changes, installs dependencies, and restarts your service efficiently.

#!/bin/bash

PROJECT_DIR="/var/www/mywebapp"
SERVICE_NAME="mywebapp.service"

echo "Starting deployment for $PROJECT_DIR..."

# Navigate to project directory
cd "$PROJECT_DIR" || {
  echo "Error: Could not change to directory $PROJECT_DIR"
  exit 1
}

# Pull latest changes from Git
echo "Pulling latest changes from Git..."
git pull origin main || {
  echo "Error: Git pull failed"
  exit 1
}

# Install Node.js dependencies (example: npm)
echo "Installing/updating Node.js dependencies..."
npm install || {
  echo "Error: npm install failed"
  exit 1
}

# Run build process (example: npm run build)
echo "Running build process..."
npm run build || {
  echo "Error: Build process failed"
  exit 1
}

# Restart the service
echo "Restarting service $SERVICE_NAME..."
sudo systemctl restart "$SERVICE_NAME" || {
  echo "Error: Failed to restart service $SERVICE_NAME"
  exit 1
}

echo "Deployment successful!"
How it works: This bash script automates a common web application deployment workflow. It navigates to the project directory, pulls the latest code from the 'main' branch of your Git repository, installs or updates Node.js dependencies using `npm install`, runs a build process, and finally restarts a systemd service associated with your application. Error handling is included at each critical step to ensure robustness. Remember to replace placeholder values like `/var/www/mywebapp` and `mywebapp.service` with your actual project details.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs