**Purpose**: This script runs on a cronjob and automatically pulls down changes from a git repository somewhere (such as Gitea) and copies the changes to a specific directory within the server, such as for [Docusaurus](https://docs.cyberstrawberry.net/Container%20Documentation/Docker/Docker%20Compose/Docusaurus). This can be extremely useful for other things like dynamically updating an [NGINX](https://docs.cyberstrawberry.net/Container%20Documentation/Docker/Docker%20Compose/NGINX) website from a git repository as well. # Create the Bash script ``` mkdir -p ~/Repo_Watcher cd ~/Repo_Watcher touch repo_watcher.sh chmod +x repo_watcher.sh nano repo_watcher.sh ``` ### Populate the bash script Be sure to substitute the values for where you are running the script from and storing the repository data, as this is used as a sort of "cache" on the server running the script and saves bandwidth, and helps keep the script generalized. ```jsx title="repo_watcher.sh" # Navigate to the watcher directory, this is where you will store a repo and watch it for changes cd /home/nicole/Repo_Watcher/traefik-config # Fetch the latest changes from remote without applying them git fetch origin main # Check if the local repository is behind the remote LOCAL=$(git rev-parse @) REMOTE=$(git rev-parse @{u}) BASE=$(git merge-base @ @{u}) if [ $LOCAL = $REMOTE ]; then echo "Up-to-date" elif [ $LOCAL = $BASE ]; then echo "Need to pull" git pull origin main rsync -av --delete /home/nicole/Repo_Watcher/traefik-config/dynamic-config.yml /srv/containers/traefik/config/dynamic/dynamic-config.yml curl -d "Traefik Config Pulled from Gitea" https://ntfy.cyberstrawberry.net/d097b0ee-985d-4d3a-985f-58f99532e019 elif [ $REMOTE = $BASE ]; then echo "Need to push" else echo "Data diverged" fi ``` ### Pull the Repository Once ``` # Store the GitHub credentials for later use when querying the repository for changes and pulling cd ~/Repo_Watcher git config --global credential.helper store git clone https://git.cyberstrawberry.net/cyberstrawberry/traefik-config.git # Enter your username and password if prompted. ``` ### Configure Crontab You will need to set up a crontab to ensure that the script runs on a schedule to check for updates and push them into whatever server you are operating. ``` # Download Updates and Install Cron and Rsync (If you are using an "Ubuntu Minimal" Install) sudo apt update && sudo apt upgrade -y sudo apt install cron rsync sudo systemctl enable cron # Make sure cron is running sudo systemctl status cron # Configure Cronjob sudo crontab -e ``` ```jsx title="sudo crontab -e" # Example of job definition: # .---------------- minute (0 - 59) # | .------------- hour (0 - 23) # | | .---------- day of month (1 - 31) # | | | .------- month (1 - 12) OR jan,feb,mar,apr ... # | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat # | | | | | # * * * * * command to be executed * * * * * bash /home/nicole/Repo_Watcher/repo_watcher.sh && chown -R nicole:nicole /srv/containers/traefik/config/dynamic/dynamic-config.yml ``` :::caution The moment you save the crontab file, it will start running the script every minute. You need to be careful to test the script's functionality prior to scheduling it to automatically run to prevent unintended consequences. :::