Created
August 13, 2025 07:58
-
-
Save geek-at/a7e85c9b26fe8b38ac0e9b00427a3354 to your computer and use it in GitHub Desktop.
This script will check if a git repo has changed every time its ran. The idea is to have a folder called "active" which is filled with multiple docker-compose files with the name of the service eg "pictshare.yml" and if a file was edited, it will redeploy this very service. If a yml file is removed, the service is shut down. This way I can manag…
This file contains hidden or 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 | |
set -euo pipefail | |
SIGNAL_API_URL="url to my signal rest api endpoint" | |
SIGNAL_SENDER="phonenumberofsignalapi" | |
SIGNAL_RECIPIENTS='["myphonenumber"]' | |
REPO_DIR="/srv/docker-swarm" | |
REPO_REMOTE="origin" | |
REPO_BRANCH="main" | |
STACK_DIR="$REPO_DIR/active" | |
DEPLOY_LOG="/var/log/last-deploy.log" | |
LOCKFILE="/tmp/swarm-deploy.lock" | |
send_signal() { | |
local message="$1" | |
curl -s -X POST -H "Content-Type: application/json" "$SIGNAL_API_URL" \ | |
-d "{\"message\": \"[SWARM] $message\", \"number\": \"$SIGNAL_SENDER\", \"recipients\": $SIGNAL_RECIPIENTS}" >/dev/null | |
} | |
# Avoid concurrent execution | |
exec 200>"$LOCKFILE" | |
flock -n 200 || exit 1 | |
cd "$REPO_DIR" | |
# Fetch changes | |
git fetch "$REPO_REMOTE" "$REPO_BRANCH" | |
CHANGED_FILES=$(git diff --name-status HEAD.."$REPO_REMOTE"/"$REPO_BRANCH" -- "$STACK_DIR") | |
# Update repo | |
git pull "$REPO_REMOTE" "$REPO_BRANCH" >/dev/null | |
if [[ -z "$CHANGED_FILES" ]]; then | |
echo "No changes." | |
exit 0 | |
fi | |
echo "Changes detected:" | |
echo "$CHANGED_FILES" | |
while read -r status file; do | |
[[ -z "$file" ]] && continue | |
stack_name=$(basename "$file" .yml) | |
case "$status" in | |
A|M) | |
echo "Deploying $stack_name..." | |
if ! docker stack deploy -c "$file" "$stack_name"; then | |
send_signal "🚨 Deployment failed for $stack_name" | |
else | |
send_signal "✅ Successfully deployed $stack_name" | |
fi | |
;; | |
D) | |
echo "Removing $stack_name..." | |
if ! docker stack rm "$stack_name"; then | |
send_signal "⚠️ Failed to remove stack $stack_name" | |
else | |
send_signal "✅ Successfully removed $stack_name" | |
fi | |
;; | |
esac | |
done <<< "$CHANGED_FILES" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment