Skip to content

Instantly share code, notes, and snippets.

@z4nr34l
Created May 31, 2024 08:14
Show Gist options
  • Save z4nr34l/14d01cd380c8331cb4130e11eb93ccbf to your computer and use it in GitHub Desktop.
Save z4nr34l/14d01cd380c8331cb4130e11eb93ccbf to your computer and use it in GitHub Desktop.
Auto project update on dedicated server, just put it in cron :D
#!/bin/bash
# Project settings
PROJECT_DIR="<PROJECT DIR>"
# 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
# TODO: Do other things like dependency update, project restart etc.
log_message "Repository updated."
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