Last active
January 7, 2025 19:30
-
-
Save gingerbeardman/e1c513c69b9e9d41aa91155893ae7334 to your computer and use it in GitHub Desktop.
Webhook git pull watcher Shell script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
TRIGGER_FILE="/var/www/example.com/triggers/git_pull_trigger" | |
LOG_FILE="/var/log/git_pull_watcher.log" | |
REPO_PATH="/var/www/example.com" | |
log() { | |
echo "$(date): $1" >> "$LOG_FILE" | |
} | |
do_git_pull() { | |
log "Attempting git pull in $REPO_PATH" | |
cd "$REPO_PATH" || { | |
log "Error: Could not change to directory $REPO_PATH" | |
return 1 | |
} | |
git rev-parse --is-inside-work-tree > /dev/null 2>&1 | |
if [ $? -ne 0 ]; then | |
log "Error: Not a git repository or command failed" | |
return 1 | |
fi | |
pull_output=$(git pull origin master 2>&1) | |
log "Pull output: $pull_output" | |
if echo "$pull_output" | grep -q "Already up to date"; then | |
log "Repository is already up to date" | |
elif echo "$pull_output" | grep -q "Updating"; then | |
log "Changes pulled successfully" | |
else | |
log "Error during git pull: $pull_output" | |
return 1 | |
fi | |
return 0 | |
} | |
log "Script started" | |
while true; do | |
if [ -f "$TRIGGER_FILE" ]; then | |
log "Trigger file found" | |
if do_git_pull; then | |
log "Git pull executed successfully" | |
else | |
log "Git pull failed" | |
fi | |
rm "$TRIGGER_FILE" | |
log "Trigger file removed" | |
fi | |
sleep 5 | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment