The simple deployer bash script will run every 5 min and continuously checks for changes on the project’s git repository. After that, I added a condition to build and pull only if there are changes between local and remote repositories. Also, the packages will be installed only if the package managers’ config file is updated.

1. Connect your Linux server to GIT Repository

  • Generate SSH key on your Linux server
  • Add the public key to your remote git repository server (GitHub, Bitbucket, Gitlab, …)
  • Test the git pull in your project folder and be sure your code is accessible from your server.

2. Create bash scripts

What is this script doing:

  • gets the current branch name
  • compares it with the remote server origin/branch
  • pulls the latest updates
  • if the package manager config file is updated - install the packages
  • it runs from anywhere

Usage:

  • Copy the script and adjust your project’s folder paths and commands.
  • run it with the path argument of your git project
  • deployer.sh /dir/project

PHP/Composer-based deployer.sh script

  • This script is for PHP projects but can be adjusted for other languages as well.
#! /bin/bash

if [ -z $1 ]; then
    echo "Path is required please set path: deployer.sh /dir/project"
    exit
else
    path=$1
fi
if [ -d .git ]; then
    cd "$path"
    git fetch &> /dev/null
else
    echo "$path is not git repository."
    exit
fi

# Get the current branch
current_branch=$(git branch --show-current)

# Get changed files between the local branch and the remote branch
changed_files=$(git diff --name-only HEAD origin/"$current_branch")

# Search for config files (composer.json, packages.json...) in the changed_files
changed_config_files=$(echo "$changed_files" | grep -E "composer.json|package.json")

if [ -z "$changed_files" ]; then
    echo "No changes for deployment."
    exit
else
    echo "There are deployment changes."
    git pull

	if [ -z "$changed_config_files" ]; then
	    php cache:clear
	else
	    echo "Install package manager changes."
	    composer install
     	php cache:clear
	fi
    exit
fi

To check the package manager config file

  • if package managers’ config files (composer.json, package.json) are in the changed files
  • $(echo "$changed_files" | grep -E "composer.json|package.json")
  • install/update the latest changes

3. Setup cronjob with your scripts

  • Add your deployer scripts to cronjob crontab -e
  • Adjust auto deploy checking period
  • For me, every 5 minutes is enough
  • the period can be set to auto-deploy only during working hours between 8-18
  • just check the timezone on the server and adjust
*/5 * * * * ~/scripts/deployer.sh /var/www/html/project

Conclusion

Usually, the CTOs, DevOps and all cool guys on the internet recommend using CI/CD for deployment. All git providers have tools for CI/CD deployment: Bitbucket Pipes, GitHub Actions or GitLab CI/CD. But I wanted to make something simple - without much configuration.

This can be useful also if the project endpoints are not exposed to the public and there is no way to consume webhooks. The only way is to implement an internal process inside the server to check for changes on the git repository.