From 9038a7c9b2e4ef773c09b59f7fe493c898ac4c27 Mon Sep 17 00:00:00 2001 From: Nicole Rappe Date: Fri, 22 Sep 2023 00:48:25 -0600 Subject: [PATCH] Updated Approach to Scheduling --- Dockerfile | 12 +++----- README.md | 4 --- crontab.txt | 1 - repo_watcher.sh | 79 +++++++++++++++++++++++++++---------------------- 4 files changed, 47 insertions(+), 49 deletions(-) delete mode 100644 README.md delete mode 100644 crontab.txt diff --git a/Dockerfile b/Dockerfile index 476f55e..f585d69 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,17 +2,13 @@ FROM ubuntu:latest # Install necessary packages RUN apt-get update && \ - apt-get install -y git curl cron rsync + apt-get install -y git curl rsync -# Add script and cronjob +# Add script 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 +# Start script +CMD ["/bin/bash", "-c", "/root/Repo_Watcher/repo_watcher.sh"] \ No newline at end of file diff --git a/README.md b/README.md deleted file mode 100644 index 7583bda..0000000 --- a/README.md +++ /dev/null @@ -1,4 +0,0 @@ -**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 deleted file mode 100644 index 9bc693c..0000000 --- a/crontab.txt +++ /dev/null @@ -1 +0,0 @@ -* * * * * /bin/bash /root/Repo_Watcher/repo_watcher.sh diff --git a/repo_watcher.sh b/repo_watcher.sh index 166f176..1207136 100644 --- a/repo_watcher.sh +++ b/repo_watcher.sh @@ -1,45 +1,52 @@ #!/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 +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 + # Fetch environment variables + repo_url=$REPO_URL + copy_dir=$COPY_DIR + ntfy_url=$NTFY_URL + git_username=$GIT_USERNAME + git_password=$GIT_PASSWORD -# Navigate to the watcher directory -cd /root/Repo_Watcher + # 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 -# Clone the repo if it doesn't exist -if [ ! -d "repo" ]; then - git clone $repo_url repo -fi + # Navigate to the watcher directory + cd /root/Repo_Watcher -cd repo + # Clone the repo if it doesn't exist + if [ ! -d "repo" ]; then + git clone $repo_url repo + fi -# Fetch the latest changes -git fetch origin main + cd repo -# Check if the local repository is behind the remote -LOCAL=$(git rev-parse @) -REMOTE=$(git rev-parse @{u}) -BASE=$(git merge-base @ @{u}) + # Fetch the latest changes + git fetch origin main -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 + # 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 + + # Wait for 5 seconds before the next iteration + sleep 5 + +done \ No newline at end of file