If you, like me, are looking for an automatic way for checking if a github repository is up-to-date because you cannot setup a Continuous Deployment (CD) environment, this is the right place. I made a set of bash scripts together with a cron job for solving such a problem. Let’s start from the script:

#!/bin/bash

FOLDER=${1:-'@{u}'}
cd $FOLDER

git fetch

LOCAL=$(git rev-parse @)
REMOTE=$(git rev-parse @{u})

if [ $LOCAL = $REMOTE ]; then
    echo "The branch is Up-to-date"
else
    echo "The branch Needs to pull"
    git pull origin master && \
    do stuff with your docker container or your code
fi


If we analyze the above bash script, we can see that it supposes to check if the master brach is up-to-date. In particular, we use the command git rev-parse that prints the SHA1 hashes given a revision specifier (you can find more details here). Then, we check if the local SHA1 is equal to the remote one in order to understand if everything is up-to-date.

In order to make it automatic, we can build a cron job. The cron command-line utility is a job scheduler on Unix-like operating systems.</> For doing it, we need just to execute the following command in the terminal:

$ crontab -e -u <your_user>

Then, you can choose the editor with which you want to edit your cron configuartion file and you have to add the following line at the end of the file:

 0 0 * * * /home/<your_username>/path/to/your/script.sh >> /home/<your_username>/path/to/a/file/where/saving/your/log 2>&1

In such a way, every day at midnight you check if everything is up-to-date. If you want to check evey 5 minutes, you have to change it in the following way:

*/5 * * * * /home/<your_username>/path/to/your/script.sh >> /home/<your_username>/path/to/a/file/where/saving/your/log 2>&1

Save everything and restart the cron service with the following command:

$ sudo service cron restart

I hope you find it useful.