Skip to content

Instantly share code, notes, and snippets.

@z4nr34l
Last active February 27, 2025 10:10
Show Gist options
  • Select an option

  • Save z4nr34l/46a26a8e736319d096c8ba56cd9961f4 to your computer and use it in GitHub Desktop.

Select an option

Save z4nr34l/46a26a8e736319d096c8ba56cd9961f4 to your computer and use it in GitHub Desktop.
Updater script for usage in cron to update any project on remote machine. Just add it as * * * * * /opt/projects/project/updater.sh
#!/bin/bash
# Project settings
PROJECT_DIR="/opt/projects" # CHANGE PATH
# Function to log messages with date and time
log_message() {
echo "[$(date +"%Y-%m-%d %H:%M:%S")] $1"
}
if [ ! -d "$PROJECT_DIR" ]; then
echo "Directory $PROJECT_DIR does not exist."
exit 1
fi
cd $PROJECT_DIR || { log_message "Failed to change directory to $REPO_DIR. Exiting..."; exit 1; }
if [ ! -d ".git" ]; then
echo "Directory $PROJECT_DIR is not a git repository."
exit 1
fi
BRANCH=$(git rev-parse --abbrev-ref HEAD)
# Fetch the latest changes from the remote repository
git fetch origin $BRANCH
LOCAL=$(git rev-parse HEAD)
REMOTE=$(git rev-parse origin/$(git rev-parse --abbrev-ref HEAD))
if [ "$LOCAL" != "$REMOTE" ]; then
log_message "Changes detected in the repository. Updating..."
git pull origin $BRANCH:refs/remotes/origin/$BRANCH
# Do your things with project
else
log_message "No changes detected in the repository."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment