Files
docs/Container Documentation/Docker/Docker Compose/Custom Containers/Git Repo Updater.md
Nicole Rappe b9aeaabbfb Initial Commit
Bringing Documentation into Gitea
2023-12-21 01:15:09 -07:00

3.6 KiB

Purpose: Docker container running Ubuntu Minimal 22.04 that automates much of the script mentioned in the Git Repo Updater document. It offers the additional benefits of checking for updates every 5 seconds instead of every 60 seconds. It also accepts environment variables to provide credentials and notification settings.

Deployment

You can find the current up-to-date Gitea repository that includes the docker-compose.yml and .env files that you need to deploy everything here

version: '3.3'
services:
    git-repo-updater:
        privileged: true
        container_name: git-repo-updater
        environment:
            - REPO_URL=${REPO_URL}
            - COPY_DIR=${COPY_DIR}
            - NTFY_URL=${NTFY_URL}
            - GIT_USERNAME=${GIT_USERNAME}
            - GIT_PASSWORD=${GIT_PASSWORD}
            - TZ=America/Denver
        image: git.cyberstrawberry.net/container-registry/git-repo-updater:latest
        volumes:
        #This folder is where the repository will be downloaded and updated - it needs a unique folder name.
           - ${TEMP_DIR}:/root/Repo_Watcher/repo 
        #This is where you want the git repository data to be copied to (e.g. a server's data folder)
           - ${DESTINATION}:/DATA
        restart: always
REPO_URL=https://git.cyberstrawberry.net/cyberstrawberry/placeholder.git
NTFY_URL=https://ntfy.cyberstrawberry.net/git-repo-updater
TEMP_DIR=/srv/containers/git-repo-updater/REPO-NAME
DESTINATION=/srv/containers/server-name/data
GIT_USERNAME=nicole.rappe
GIT_PASSWORD=USE-AN-APP-PASSWORD

Build / Development

If you want to learn how the container was assembled, the related build files are located here

FROM ubuntu:latest

# Install necessary packages
RUN apt-get update && \
    apt-get install -y git curl rsync

# Add script
COPY repo_watcher.sh /root/Repo_Watcher/repo_watcher.sh

# Make script executable
RUN chmod +x /root/Repo_Watcher/repo_watcher.sh

# Start script
CMD ["/bin/bash", "-c", "/root/Repo_Watcher/repo_watcher.sh"]
#!/bin/bash

while true; do
  # Set Git credentials
  git config --global credential.helper 'store --file /tmp/git-credentials'
  echo "url=$REPO_URL" > /tmp/git-credentials
  echo "username=$GIT_USERNAME" >> /tmp/git-credentials
  echo "password=$GIT_PASSWORD" >> /tmp/git-credentials

  # Navigate to the watcher directory
  cd /root/Repo_Watcher

  # Clone the repo if it doesn't exist
  if [ -z "$(find /root/Repo_Watcher/repo -maxdepth 1 -mindepth 1 -type f -o -type d 2>/dev/null)" ]; then
    curl -d "Repository $REPO_URL doesn't exist locally - Downloading..." $NTFY_URL
    echo "Repository $REPO_URL doesn't exist locally - Downloading..."
    git clone $REPO_URL repo
    cd repo
    rsync -av --delete --exclude '.git/' ./ /DATA
  fi

  cd repo

  # Fetch the latest changes
  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 "Repository Up-to-Date"
  else
      curl -d "$REPO_URL Automatically Pulling Updates..." $NTFY_URL
      echo "Pulling Updates from Repository..."
      git pull origin main
      rsync -av --delete --exclude '.git/' ./ /DATA
  fi

  # Wait for 5 seconds before the next iteration
  sleep 5

done