How to auto deploy git repository to your server

Michael Schmidt
Published: Sep 4, 2024 by Michael Schmidt
How to auto deploy git repository to your server

Prerequisites #

Our guides are written with the expectation that the following requirements are met:

What this script does? #

In the following guide we do setup a script, which periodically execute git pull command on your Git repository and execute your custom deploy commands when new commit detected.

  • Working with any Git service, Github/Gitlab/Self hosted
  • No dependency requirement, it will work any server having git package installed
  • Do not block your CI/CD once your server unreachable for deploy

Clone your Git repository #

Clone your Git repository in our example we do clone our Github repo

git clone [email protected]:Selfhostguru/git-deploy-script.git

Create the deploy script #

Create the script file

nano deployScript.sh

Add the following content

#!/bin/bash

# Exit immediately if a command exits with a non-zero status
set -e

# Set the repository directory (relative path)
REPO_DIR="/root/your-cloned-repo-directory"

# Navigate to the repository directory
cd "$REPO_DIR" || exit


get_local_commit_hash() {
  git rev-parse HEAD
}

# Function to get the latest commit hash
get_latest_commit_hash() {
  git ls-remote origin HEAD | awk '{print $1}'
}

# Initial latest commit hash
LAST_COMMIT_HASH=$(get_local_commit_hash)

while true; do
  # Check the latest commit hash
  CURRENT_COMMIT_HASH=$(get_latest_commit_hash)

  echo "Last commit hash: $LAST_COMMIT_HASH  |  Current commit hash: $CURRENT_COMMIT_HASH"

  if [ "$LAST_COMMIT_HASH" != "$CURRENT_COMMIT_HASH" ]; then
    echo "New commit detected, pulling changes and rebuilding..."

    LAST_COMMIT_HASH=$(get_local_commit_hash)

    # Pull the latest changes from the repo
    git pull

    # YOUR DEPLOYMENT SCRIPTS START HERE

    ### For Docker Compose
    # docker compose build
    # docker compose up -d --force-recreate --pull always

    ### For PM2
    # npm install --production
    # pm2 restart all

    # YOUR DEPLOYMENT SCRIPTS END HERE

  else
    echo "No new commits detected."
  fi

  # Wait for 10 seconds before checking again
  sleep 60
done

Edit the following lines according your system

Add the folder of your Git repo:

# Set the repository directory
REPO_DIR="./your-cloned-repo-directory"

Add your custom deploy commands

   # YOUR DEPLOYMENT SCRIPTS START HERE

    ### For Docker Compose
    # docker compose build
    # docker compose up -d --force-recreate --pull always

    ### For PM2
    # npm install --production
    # pm2 restart all

    # YOUR DEPLOYMENT SCRIPTS END HERE

Make script executable #

chmod +x  ./deployScript.sh

Folder structure #

This is how your folder structure should look like

9trp5VZ.png

Test your deploy script #

Start the script for testing

./deployScript.sh

Run script as Systemd #

Once everything work fine, add your script as Systemd to make it always run even after server restart

Run script as systemd under Ubuntu