From 263e390a598a90a7cce70af906d9344fb22160a9 Mon Sep 17 00:00:00 2001 From: Nicole Rappe Date: Thu, 21 Sep 2023 23:10:31 -0600 Subject: [PATCH] Initial Commit --- Dockerfile | 18 ++++++++++++++++++ README.md | 4 ++++ crontab.txt | 1 + docker-compose.yml | 0 repo_watcher.sh | 45 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 68 insertions(+) create mode 100644 Dockerfile create mode 100644 README.md create mode 100644 crontab.txt create mode 100644 docker-compose.yml create mode 100644 repo_watcher.sh diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..476f55e --- /dev/null +++ b/Dockerfile @@ -0,0 +1,18 @@ +FROM ubuntu:latest + +# Install necessary packages +RUN apt-get update && \ + apt-get install -y git curl cron rsync + +# Add script and cronjob +COPY repo_watcher.sh /root/Repo_Watcher/repo_watcher.sh +COPY crontab.txt /crontab.txt + +# Make script executable +RUN chmod +x /root/Repo_Watcher/repo_watcher.sh + +# Apply cron job +RUN crontab /crontab.txt + +# Start cron in foreground +CMD ["cron", "-f"] \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..7583bda --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +**Basic Docker Run Command**: +``` +docker run -e REPO_URL='https://your.git.repo.url' -e COPY_DIR='/your/copy/dir' -e NTFY_URL='https://your.ntfy.url' -e GIT_USERNAME='your_username' -e GIT_PASSWORD='your_password' git_repo_updater +``` diff --git a/crontab.txt b/crontab.txt new file mode 100644 index 0000000..ad3bf79 --- /dev/null +++ b/crontab.txt @@ -0,0 +1 @@ +* * * * * /bin/bash /root/Repo_Watcher/repo_watcher.sh >/dev/null 2>&1 diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..e69de29 diff --git a/repo_watcher.sh b/repo_watcher.sh new file mode 100644 index 0000000..166f176 --- /dev/null +++ b/repo_watcher.sh @@ -0,0 +1,45 @@ +#!/bin/bash + +# Fetch environment variables +repo_url=$REPO_URL +copy_dir=$COPY_DIR +ntfy_url=$NTFY_URL +git_username=$GIT_USERNAME +git_password=$GIT_PASSWORD + +# 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 [ ! -d "repo" ]; then + git clone $repo_url repo +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 "Up-to-date" +elif [ $LOCAL = $BASE ]; then + echo "Need to pull" + git pull origin main + rsync -av --delete ./ $copy_dir + curl -d "Data updated from Git repo" $ntfy_url +elif [ $REMOTE = $BASE ]; then + echo "Need to push" +else + echo "Data diverged" +fi