:e ~/article/git-auto-pull-push-script-automate-your-deployment-workflow.md [readonly]

smjrifle@smjrifle.xyz:~/article/ $

Git Auto Pull Push Script — Automate Your Deployment Workflow

Manual git pull on every deployment is the kind of repetitive task that machines were born to eliminate. Here’s a Bash script that automates your pull-push workflow — whether you’re managing a single server or orchestrating across multiple environments.

The Problem

Every time you push code to your remote repository, you want it deployed on your server. Doing this manually means SSH-ing in, navigating to the project directory, running git pull, and possibly restarting services. Multiply this by ten deploys a day and you’ve got a productivity drain that compounds silently.

The Script

#!/bin/bash
# smjrifle Git Auto Pull Script
# Place this in your project root or /usr/local/bin/

PROJECT_DIR="/var/www/html/yourproject"
BRANCH="main"
LOG_FILE="/var/log/git-deploy.log"
TIMESTAMP=$(date +"%Y-%m-%d %H:%M:%S")

echo "[$TIMESTAMP] Starting deployment..." >> $LOG_FILE

cd $PROJECT_DIR || { echo "[$TIMESTAMP] ERROR: Project directory not found" >> $LOG_FILE; exit 1; }

git fetch origin >> $LOG_FILE 2>&1

LOCAL=$(git rev-parse HEAD)
REMOTE=$(git rev-parse origin/$BRANCH)

if [ "$LOCAL" != "$REMOTE" ]; then
    echo "[$TIMESTAMP] Changes detected. Pulling..." >> $LOG_FILE
    git pull origin $BRANCH >> $LOG_FILE 2>&1
    
    # Optional: restart services
    # systemctl restart php8.1-fpm
    # systemctl reload apache2
    
    echo "[$TIMESTAMP] Deployment complete." >> $LOG_FILE
else
    echo "[$TIMESTAMP] Already up to date. No action needed." >> $LOG_FILE
fi

Setting Up the Cron Job

Make the script executable and add it to your crontab:

chmod +x /usr/local/bin/git-deploy.sh
crontab -e

# Add this line to check every 5 minutes:
*/5 * * * * /usr/local/bin/git-deploy.sh

Git Hooks — The Better Approach

For immediate deploys triggered by push, use a post-receive hook on a bare repository. This is the professional approach used in production systems.

# On your server, create a bare repo:
mkdir /var/repo/myproject.git
cd /var/repo/myproject.git
git init --bare

# Create the hook:
nano hooks/post-receive

#!/bin/bash
GIT_WORK_TREE=/var/www/html/myproject git checkout -f main
echo "Deployed successfully."

chmod +x hooks/post-receive

Then on your local machine, add the server as a remote and push to it directly. No cron jobs needed. Every push to the server triggers an instant deploy.

Security Notes

  • Never store credentials in scripts. Use SSH keys for authentication.
  • Restrict script execution permissions: chmod 700
  • Log all deployments. If something breaks, you need a trail.
  • Test on staging first. Always.

This is one of those #HackLife automations that pays dividends for years. Set it up once, forget the manual deploys forever.

[:bp] OSX / Mac OS Empty Trash [:bn] Set Up Apache Virtual Host and Integrate Git with Auto Pull
-- NORMAL -- git-auto-pull-push-script-automate-your-deployment-workflow.md PHP UTF-8 0% smjrifle@xyz