deploy-watch git e docker compose

# Git Deploy Watch
A simple Linux service that automatically deploys your Docker Compose application when new Git changes are detected.
It checks the repository every 30 seconds and, when a new commit is found, runs:
```bash
docker compose up -d --build
```
The service runs in the background, starts automatically after reboot, and restarts if it crashes.
## Install
Run this inside your project directory:
```bash
cd /opt/my-project
curl -fsSL "https://gist.githubusercontent.com/MarcosViniicius/d6e5d30bfe4703c3d4c7f543266f59e9/raw/deploy-watch.sh" | sudo bash
## Check status
```bash
systemctl status git-deploy-watch
```
## View logs
```bash
journalctl -u git-deploy-watch -f
```
## Uninstall
```bash
curl -fsSL "https://gist.githubusercontent.com/MarcosViniicius/d6e5d30bfe4703c3d4c7f543266f59e9/raw/deploy-watch.sh" | sudo bash -s -- remove
```
### Requirements
* Linux + systemd
* Git
* Docker + Docker Compose
* A Git repository with `docker-compose.yml` or `compose.yml`
#!/bin/bash
set -e
SERVICE_NAME="git-deploy-watch"
SCRIPT_PATH="/usr/local/bin/$SERVICE_NAME"
SERVICE_FILE="/etc/systemd/system/$SERVICE_NAME.service"
# =========================
# REMOVE
# =========================
if [ "$1" = "remove" ]; then
echo "Removing $SERVICE_NAME..."
systemctl disable --now "$SERVICE_NAME" 2>/dev/null || true
rm -f "$SERVICE_FILE"
rm -f "$SCRIPT_PATH"
systemctl daemon-reload
systemctl reset-failed "$SERVICE_NAME" 2>/dev/null || true
echo "Monitor removed successfully."
exit 0
fi
# =========================
# INSTALL
# =========================
APP_DIR="$(pwd)"
if [ ! -d "$APP_DIR/.git" ]; then
echo "Error: the current directory is not a Git repository."
exit 1
fi
if [ ! -f "$APP_DIR/docker-compose.yml" ] &&
[ ! -f "$APP_DIR/compose.yml" ]; then
echo "Error: no docker-compose.yml or compose.yml file found."
exit 1
fi
echo "Installing monitor..."
echo "Application directory: $APP_DIR"
cat > "$SCRIPT_PATH" <<EOF
#!/bin/bash
APP_DIR="$APP_DIR"
LOCK_FILE="/tmp/$SERVICE_NAME.lock"
cd "\$APP_DIR" || exit 1
while true; do
exec 9>"\$LOCK_FILE"
if flock -n 9; then
BEFORE=\$(git rev-parse HEAD 2>/dev/null || echo "")
git pull --quiet
AFTER=\$(git rev-parse HEAD 2>/dev/null || echo "")
if [ -n "\$BEFORE" ] && [ "\$BEFORE" != "\$AFTER" ]; then
echo "[\$(date '+%Y-%m-%d %H:%M:%S')] Changes detected."
echo "[\$(date '+%Y-%m-%d %H:%M:%S')] Running Docker Compose..."
docker compose up -d --build
echo "[\$(date '+%Y-%m-%d %H:%M:%S')] Deployment completed."
fi
fi
sleep 30
done
EOF
chmod +x "$SCRIPT_PATH"
cat > "$SERVICE_FILE" <<EOF
[Unit]
Description=Git Pull Docker Deploy Watcher
After=network-online.target docker.service
Wants=network-online.target
Requires=docker.service
[Service]
Type=simple
ExecStart=$SCRIPT_PATH
WorkingDirectory=$APP_DIR
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable "$SERVICE_NAME"
systemctl restart "$SERVICE_NAME"
echo
echo "========================================"
echo " Monitor installed successfully"
echo "========================================"
echo
echo "Application directory: $APP_DIR"
echo "Service: $SERVICE_NAME"
echo
echo "Logs:"
echo "journalctl -u $SERVICE_NAME -f"
echo
echo "Remove:"
echo "curl -fsSL https://gist.githubusercontent.com/MarcosViniicius/d6e5d30bfe4703c3d4c7f543266f59e9/raw/deploy-watch.sh | sudo bash -s -- remove"
echo
添加评论
点赞收藏
点踩分享查看原文
评论
?
参与讨论