Deploying a Static Website Automatically via Git Hooks and Bash

Deploying a Static Website Automatically via Git Hooks and Bash

Deploying a Static Website Automatically via Git Hooks and Bash

 

Introduction

Automation is the heart of modern development workflows. For static websites — think HTML/CSS projects, documentation pages, or portfolios — we can eliminate manual deployment tasks using Git hooks and Bash scripts. In this guide, we’ll set up an automated deployment process that triggers right after every commit, automatically syncing the latest code changes to a web server.

Section 1: Understanding Git Hooks and Post-Commit Actions

Git hooks are scripts that run automatically on certain Git events, like before a commit, after a commit, or before pushing. The post-commit hook runs right after a commit succeeds, making it perfect for triggering deployments.

By placing a simple Bash script in .git/hooks/post-commit, you can automate numerous actions — testing, building, or even syncing your static site files to a server. Here’s what the hook setup looks like:

#!/bin/bash
# .git/hooks/post-commit

# Print a message to confirm the hook ran
echo "Running post-commit hook... Deploying site."

# Execute deployment script
./deploy.sh

Make sure the hook is executable using chmod +x .git/hooks/post-commit. This will trigger the deploy script automatically after every commit.

Section 2: Writing the Deployment Script

Now, let’s create the deploy.sh script that handles syncing your site to the server. Common tools for this include rsync, scp, or even git pull from a bare repository clone.

#!/bin/bash
# deploy.sh

# Configuration
REMOTE_USER="username"
REMOTE_HOST="yourserver.com"
REMOTE_DIR="/var/www/html"
LOCAL_DIR="./public"

# Sync local files to remote server using rsync
rsync -avz --delete "$LOCAL_DIR/" "$REMOTE_USER@$REMOTE_HOST:$REMOTE_DIR"

# Optional: Log output
echo "Deployment complete: $(date)" >> deploy.log

This setup leverages rsync for efficient file synchronization. The --delete flag ensures old files are cleaned up, and -avz ensures data is transferred securely and efficiently. Make the script executable using chmod +x deploy.sh.

Section 3: Securing and Optimizing SSH Connections

If your deployment involves SSH, use key-based authentication for secure and passwordless access. You can generate a key pair as follows:

ssh-keygen -t ed25519 -C "deploy-key"
ssh-copy-id username@yourserver.com

To speed up multiple deployments, add the following to your ~/.ssh/config file:

Host yourserver
    HostName yourserver.com
    User username
    IdentityFile ~/.ssh/id_ed25519
    ControlMaster auto
    ControlPath ~/.ssh/sockets/%r@%h-%p
    ControlPersist 10m

This configuration reuses existing SSH sessions, dramatically improving connection speed during repeated deployments.

Section 4: Adding Automation Logic and Error Handling

You can enhance your deployment script by adding hooks for build steps or error handling. For example, let’s integrate static site generation (if you use tools like Jekyll or Hugo) and handle errors gracefully:

#!/bin/bash

set -e  # Exit on error

# Build step
if [ -f "build.sh" ]; then
  echo "Running build script..."
  ./build.sh || { echo "Build failed!"; exit 1; }
fi

# Deployment step
echo "Deploying site..."
rsync -az --delete ./public/ user@server:/var/www/html || {
  echo "Deployment failed! Check connection or permissions."; exit 1;
}

echo "Deployment completed successfully!"

Using set -e allows the script to stop on failure. Logging errors and timestamps helps with debugging.

Section 5: Testing, Maintenance, and Continuous Integration

Before relying on this automation, test it locally. Commit a small change and observe the console output — you should see your post-commit hook execute and your updated files on the remote server. Additionally, you can combine this workflow with simple CI/CD pipelines by having GitHub Actions or GitLab CI trigger similar scripts remotely.

Over time, consider adding versioning, automatic backups, or Slack notifications after successful deployments. Even a lightweight cron job to verify deployment integrity can save hours of downtime debugging.

Conclusion

Git hooks and Bash scripts offer a lightweight yet powerful way to automate static site deployments without third-party services. This workflow scales well for small projects and personal sites, and it’s an excellent introduction to automating developer operations. With just a couple of scripts, your development process becomes faster, more reliable, and repeatable.

 

Useful links: