Added Infinite Repo Functionality (Beta)

This commit is contained in:
Nicole Rappe 2024-01-07 06:06:53 -07:00
parent ec619ffecd
commit 462bfa5e01
3 changed files with 57 additions and 52 deletions

11
.env
View File

@ -1,10 +1,11 @@
# Repository to Pull # Gitea Credentials
REPO_URL=https://git.cyberstrawberry.net/cyberstrawberry/website.git
GIT_USERNAME=nicole.rappe GIT_USERNAME=nicole.rappe
GIT_PASSWORD=USE-AN-APP-PASSWORD GIT_PASSWORD=USE-AN-APP-PASSWORD
# NTFY Server to Notify on Pull Events # NTFY Push Notification Server URL
NTFY_URL=https://ntfy.cyberstrawberry.net/git-repo-updater NTFY_URL=https://ntfy.cyberstrawberry.net/git-repo-updater
# Folder of the destination server / container folder where you want the repository data to transfer # Repository/Destination Pairs (Add as Many as Needed)
DESTINATION=/srv/containers/nginx-portfolio-website/www REPO_01="https://git.bunny-lab.io/repo1.git,/srv/containers/destination"
REPO_02="https://git.bunny-lab.io/repo1.git,/srv/containers/destination"
REPO_03="https://git.bunny-lab.io/repo1.git,/srv/containers/destination"

View File

@ -3,13 +3,9 @@ services:
git-repo-updater: git-repo-updater:
privileged: true privileged: true
container_name: git-repo-updater container_name: git-repo-updater
environment: env_file:
- REPO_URL=${REPO_URL} - stack.env
- COPY_DIR=${COPY_DIR} image: git.bunny-lab.io/container-registry/git-repo-updater:latest
- NTFY_URL=${NTFY_URL}
- GIT_USERNAME=${GIT_USERNAME}
- GIT_PASSWORD=${GIT_PASSWORD}
image: git.cyberstrawberry.net/container-registry/git-repo-updater:latest
volumes: volumes:
- ${DESTINATION}:/DATA - /srv/containers:/srv/containers
restart: always restart: always

View File

@ -1,44 +1,52 @@
#!/bin/sh #!/bin/sh
# Function to process each repo-destination pair
process_repo() {
REPO_URL=$1
DESTINATION=$2
# 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
# Directory to hold the repository locally
REPO_DIR="/root/Repo_Watcher/$(basename $REPO_URL)"
# Clone the repo if it doesn't exist, or navigate to it if it does
if [ ! -d "$REPO_DIR" ]; then
curl -d "Cloning: $REPO_URL" $NTFY_URL
echo "Cloning: $REPO_URL"
git clone "$REPO_URL" "$REPO_DIR"
fi
cd "$REPO_DIR"
# 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})
if [ $LOCAL != $REMOTE ]; then
curl -d "Updating: $REPO_URL" $NTFY_URL
echo "Updating: $REPO_URL"
git pull origin main
rsync -av --delete --exclude '.git/' ./ "$DESTINATION"
else
echo "Repository $REPO_URL Up-to-Date"
fi
}
# Main loop
while true; do while true; do
# Set Git credentials # Iterate over each environment variable matching 'REPO_[0-9]+'
git config --global credential.helper 'store --file /tmp/git-credentials' env | grep '^REPO_[0-9]\+=' | while IFS='=' read -r name value; do
echo "url=$REPO_URL" > /tmp/git-credentials IFS=',' read -ra REPO_DEST <<< "$value"
echo "username=$GIT_USERNAME" >> /tmp/git-credentials process_repo "${REPO_DEST[0]}" "${REPO_DEST[1]}"
echo "password=$GIT_PASSWORD" >> /tmp/git-credentials done
# 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 "Pulling: $REPO_URL" $NTFY_URL
echo "Pulling: $REPO_URL"
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 "Pulling: $REPO_URL" $NTFY_URL
echo "Pulling: $REPO_URL"
git pull origin main
rsync -av --delete --exclude '.git/' ./ /DATA
fi
# Wait for 5 seconds before the next iteration
sleep 5
# Wait for 5 seconds before the next iteration
sleep 5
done done