Git with Auto Pull Deployment (Zero-Touch Server Updates)
Managing deployments can be repetitive, especially when you’re constantly pushing changes to a server. To simplify this, I created a shell script that automates:
- Git repository creation
- Auto-deployment via post-receive hook
- Apache virtual host setup
The result is a lightweight “git push to deploy” workflow where every push is instantly reflected on the server.
How It Works
This script:
- Creates a bare Git repository on the server
- Configures it with a working directory
- Adds a
post-receivehook to auto-checkout code - Clones the repo into the web directory
- Sets up an Apache virtual host
- Enables the site and reloads the server
Once set up, any git push will automatically deploy changes to production.
Prerequisites
- A server with Git installed
- Apache configured
- A
gituser (home directory:/home/git) - Sudo privileges
- Web root at
/var/www/
Usage
Run the script with your site name:
sudo sh script_name.sh site_nameExample:
sudo sh deploy.sh myprojectThe Script
#!/bin/bash
# Check if parameter is provided
if [ "$#" -eq 1 ]; then
echo "Creating git repo $1.git"
cd /home/git
sudo mkdir $1.git
cd $1.git
# Initialize bare repo
sudo git init --bare
sudo git --bare update-server-info
# Configure repo
sudo git config core.bare false
sudo git config receive.denyCurrentBranch ignore
sudo git config core.worktree /var/www/$1
cd ..
sudo chown git:www-data $1.git -R
# Create post-receive hook
cd $1.git/hooks
echo "#!/bin/sh" | sudo tee post-receive
echo "git checkout -f" | sudo tee -a post-receive
sudo chmod +x post-receive
else
echo "Please provide a site name"
exit
fi
# Clone repo into web directory
cd /var/www/
git clone git@yoursite.com:$1.git $1
cd $1
sudo chown git:www-data -R .
# Create Apache virtual host
cd /etc/apache2/sites-available/
sudo tee $1.yoursite.com > /dev/null <<EOF
<VirtualHost *:80>
ServerAdmin user@yoursite.com
ServerName $1.yoursite.com
DocumentRoot /var/www/$1
<Directory /var/www/$1>
AllowOverride All
Require all granted
</Directory>
ErrorLog /var/log/apache2/error.$1.log
CustomLog /var/log/apache2/access.$1.log combined
</VirtualHost>
EOF
# Enable site and reload Apache
sudo a2ensite $1.yoursite.com
sudo service apache2 reload
# Output info
echo "Repo created: git@yoursite.com:$1.git"
echo "Clone using: git clone git@yoursite.com:$1.git"
echo "Or add remote: git remote add origin git@yoursite.com:$1.git"
# Log creation
echo "git@yoursite.com:$1.git created on $(date)" >> ~/gitLogDeployment Workflow
Once the setup is complete:
git add -A
git commit -m "Update feature"
git push origin mainYour changes are automatically deployed to:
/var/www/site_name
No manual SSH, no pull commands, no extra steps.
Why This Approach
This setup is simple but effective for small to medium projects:
- Eliminates manual deployment steps
- Keeps deployment consistent
- Uses native Git hooks
- Requires minimal tooling
Limitations
- No versioned releases or rollback system
- Direct deployment to production
- Not ideal for large teams or CI/CD pipelines
- Requires careful permission management
Possible Improvements
You can extend this setup with:
- Branch-based deployments (staging vs production)
- Pre-deploy testing hooks
- Backup or rollback mechanisms
- Integration with CI/CD tools
- Logging and deployment notifications
Final Thoughts
This script is a lightweight alternative to full CI/CD pipelines. It’s especially useful for personal projects, prototypes, or small teams where speed and simplicity matter.
With just a single command, you can go from code to live deployment instantly, which makes iteration fast and frictionless.
