From 1fe15ea3ea56fa3106b5abf47024eefb93d8c771 Mon Sep 17 00:00:00 2001 From: shkim Date: Sun, 10 May 2026 20:10:46 +0900 Subject: [PATCH] Add polling deployment scripts --- .dockerignore | 4 ++++ README.md | 11 +++++++++++ cd-watch.sh | 26 ++++++++++++++++++++++++++ deploy.sh | 25 +++++++++++++++++++++++++ 4 files changed, 66 insertions(+) create mode 100644 .dockerignore create mode 100644 cd-watch.sh create mode 100644 deploy.sh diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..6fbc9ca --- /dev/null +++ b/.dockerignore @@ -0,0 +1,4 @@ +.git +.env +data +node_modules diff --git a/README.md b/README.md index 94d02c5..af4d193 100644 --- a/README.md +++ b/README.md @@ -15,11 +15,22 @@ sudo ./run.sh sudo ./stop.sh ``` +## Continuous Deployment + +`cd-watch.sh` checks `origin/main` and runs `deploy.sh` when the remote revision changes. + +```sh +sudo ./cd-watch.sh +``` + +The server cron runs this once per minute. + ## Paths - App: `/volume2/homes/whitekomani/photo-request-service` - Config: `.env` - DB: `data/db.json` +- Deploy log: `data/deploy.log` - Uploads: `/volume2/AKA Drive/사진 받기` Each request creates its own folder under the upload directory. diff --git a/cd-watch.sh b/cd-watch.sh new file mode 100644 index 0000000..2cefe71 --- /dev/null +++ b/cd-watch.sh @@ -0,0 +1,26 @@ +#!/bin/sh +set -eu + +APP_DIR="$(cd "$(dirname "$0")" && pwd)" +STATE_FILE="$APP_DIR/data/deployed-rev" +LOG_FILE="$APP_DIR/data/deploy.log" + +mkdir -p "$APP_DIR/data" +cd "$APP_DIR" + +remote_rev="$(git ls-remote origin refs/heads/main | awk '{print $1}')" +if [ -z "$remote_rev" ]; then + echo "$(date '+%Y-%m-%d %H:%M:%S') no remote revision found" >> "$LOG_FILE" + exit 1 +fi + +current_rev="" +[ -f "$STATE_FILE" ] && current_rev="$(cat "$STATE_FILE")" + +if [ "$remote_rev" = "$current_rev" ]; then + exit 0 +fi + +echo "$(date '+%Y-%m-%d %H:%M:%S') change detected $current_rev -> $remote_rev" >> "$LOG_FILE" +"$APP_DIR/deploy.sh" +printf '%s\n' "$remote_rev" > "$STATE_FILE" diff --git a/deploy.sh b/deploy.sh new file mode 100644 index 0000000..e09b03d --- /dev/null +++ b/deploy.sh @@ -0,0 +1,25 @@ +#!/bin/sh +set -eu + +APP_DIR="$(cd "$(dirname "$0")" && pwd)" +LOG_FILE="$APP_DIR/data/deploy.log" +LOCK_DIR="$APP_DIR/data/deploy.lock" + +mkdir -p "$APP_DIR/data" + +if ! mkdir "$LOCK_DIR" 2>/dev/null; then + echo "$(date '+%Y-%m-%d %H:%M:%S') deploy already running" >> "$LOG_FILE" + exit 0 +fi +trap 'rmdir "$LOCK_DIR"' EXIT + +cd "$APP_DIR" + +echo "$(date '+%Y-%m-%d %H:%M:%S') deploy start" >> "$LOG_FILE" +git fetch origin main >> "$LOG_FILE" 2>&1 +git reset --hard origin/main >> "$LOG_FILE" 2>&1 + +/usr/local/bin/docker build -t photoreq:latest . >> "$LOG_FILE" 2>&1 +./run.sh >> "$LOG_FILE" 2>&1 + +echo "$(date '+%Y-%m-%d %H:%M:%S') deploy done $(git rev-parse --short HEAD)" >> "$LOG_FILE"