Skip to content

Commit e3af973

Browse files
authored
Refactor deployment script for production use
1 parent d6e2f04 commit e3af973

1 file changed

Lines changed: 32 additions & 86 deletions

File tree

deployment.sh

Lines changed: 32 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,24 @@
11
#!/usr/bin/env bash
2-
#
3-
# deploy_recapp_to_test.sh
4-
# This script deploys the RecApp application to the test environment.
5-
# It assumes:
6-
# - The deploy user has passwordless sudo for the needed npm/docker commands.
7-
# - $HOME is the home directory of that user, and ~/recapp is the app folder.
8-
# - npm scripts: stop:docker:prod, build:docker:prod, start:docker:prod exist in package.json.
9-
# - set -e is in effect, so any command failing will abort the script.
10-
112
set -euo pipefail
123

13-
BRANCH="${1:?Usage: deployment.sh <branch>}"
14-
15-
# Define log file and rotation parameters
16-
# The log file will be stored in the user's home directory.
17-
# It will rotate when it exceeds 10 MB, keeping the last 3 old logs.
18-
# The log file will be named deploy.log, and old logs will be named deploy.log.1, deploy.log.2, etc.
19-
# The log rotation will be handled by the rotate_logs function.
20-
LOG_DIR="$HOME"
21-
LOG_FILE="$LOG_DIR/deploy.log"
22-
MAX_LOG_SIZE=$((10 * 1024 * 1024)) # 10 MB
23-
MAX_OLD_LOGS=3
24-
25-
rotate_logs() {
26-
# If deploy.log does not exist, nothing to rotate
27-
[ -f "$LOG_FILE" ] || return 0
28-
29-
local actual_size
30-
actual_size=$(stat -c%s "$LOG_FILE")
31-
if [ "$actual_size" -le "$MAX_LOG_SIZE" ]; then
32-
return 0
33-
fi
34-
35-
# Shift old logs: deploy.log.2 -> deploy.log.3, deploy.log.1 -> deploy.log.2, deploy.log -> deploy.log.1
36-
if [ -f "$LOG_DIR/deploy.log.$((MAX_OLD_LOGS - 1))" ]; then
37-
rm -f "$LOG_DIR/deploy.log.$((MAX_OLD_LOGS - 1))"
38-
fi
39-
40-
for (( i=MAX_OLD_LOGS-1; i>=1; i-- )); do
41-
if [ -f "$LOG_DIR/deploy.log.$i" ]; then
42-
mv "$LOG_DIR/deploy.log.$i" "$LOG_DIR/deploy.log.$((i + 1))"
43-
fi
44-
done
45-
46-
mv "$LOG_FILE" "$LOG_DIR/deploy.log.1"
47-
: > "$LOG_FILE"
48-
echo "$(date '+%Y-%m-%d %H:%M:%S') - Log rotated: previous log moved to deploy.log.1" >> "$LOG_FILE"
49-
return 0
50-
}
4+
BRANCH="${1:-production}"
5+
REPO_DIR="$HOME/recapp"
6+
LOG_FILE="$HOME/deploy.log"
517

52-
# Rotate logs if necessary
53-
rotate_logs
54-
echo "test end"
55-
# Append a timestamped message to both the log file and stdout
56-
log() {
57-
local msg="$1"
58-
echo "$(date '+%Y-%m-%d %H:%M:%S') - ${msg}" | tee -a "$LOG_FILE"
59-
}
8+
log() { echo "$(date '+%Y-%m-%d %H:%M:%S') - $*" | tee -a "$LOG_FILE" ; }
609

61-
# On any unexpected exit (non-zero), log it
6210
on_error() {
6311
local exit_code=$?
64-
log "❌ Deployment script exited with code ${exit_code}."
65-
exit "${exit_code}"
12+
log "❌ Deployment failed (exit ${exit_code})."
13+
exit "$exit_code"
6614
}
67-
trap on_error ERR
15+
trap on_error ERR INT TERM
6816

69-
log "=== Starting deployment to test environment ==="
17+
log "=== Starting deployment to PROD (branch: ${BRANCH}) ==="
7018

71-
# Verify sudo privileges (without a password prompt)
19+
# Sudo check (only needed for docker/compose, ideally)
7220
if ! sudo -n true 2>/dev/null; then
73-
log "ERROR: This script requires passwordless sudo privileges. Exiting."
74-
exit 1
75-
fi
76-
77-
# Ensure the recapp directory exists
78-
REPO_DIR="$HOME/recapp"
79-
if [ ! -d "$REPO_DIR" ]; then
80-
log "ERROR: Directory '$REPO_DIR' not found. Cannot deploy."
21+
log "ERROR: Need passwordless sudo for docker/compose."
8122
exit 1
8223
fi
8324

@@ -86,24 +27,29 @@ cd "$REPO_DIR"
8627
log "Fetching origin..."
8728
git fetch origin --prune
8829

89-
log "Checking out branch '$BRANCH' (force)..."
90-
git checkout --force -B "$BRANCH" "origin/$BRANCH"
91-
92-
# 1) Stop the existing Docker production container
93-
log "Stopping existing production container..."
94-
sudo npm run stop:docker:prod 2>&1 | tee -a "$LOG_FILE"
30+
log "Checking out ${BRANCH}..."
31+
git checkout "$BRANCH" 2>/dev/null || git checkout -b "$BRANCH"
32+
git reset --hard "origin/$BRANCH"
9533

96-
# 2) Install dependencies (CI)
97-
log "Installing npm dependencies for CI..."
98-
sudo npm ci 2>&1 | tee -a "$LOG_FILE"
34+
DEPLOY_REF="$(git rev-parse HEAD)"
35+
DEPLOY_DESC="$(git show -s --format='%h %ci %d %s' HEAD)"
36+
echo "$(date -Is) branch=$BRANCH ref=$DEPLOY_REF $DEPLOY_DESC" | tee -a "$HOME/deploy.log" >> "$HOME/deploy.refs.log"
9937

100-
# 3) Build the Docker image for production
101-
log "Building Docker image for production..."
102-
sudo npm run build:docker:prod 2>&1 | tee -a "$LOG_FILE"
38+
log "Installing npm deps (non-root)..."
39+
npm ci 2>&1 | tee -a "$LOG_FILE"
10340

104-
# 4) Start the new production container
105-
log "Starting new production container..."
106-
sudo npm run start:docker:prod 2>&1 | tee -a "$LOG_FILE"
41+
log "Building docker images..."
42+
# Prefer calling compose directly so you don’t depend on opaque npm scripts
43+
sudo docker compose --env-file .env.production -f docker/docker-compose.yaml -f docker/docker-compose.prod.yaml build 2>&1 | tee -a "$LOG_FILE"
44+
45+
log "Bringing services up (recreate if needed)..."
46+
sudo docker compose --env-file .env.production -f docker/docker-compose.yaml -f docker/docker-compose.prod.yaml up -d 2>&1 | tee -a "$LOG_FILE"
47+
48+
log "Health check backend..."
49+
# Adjust URL/port for your setup:
50+
if ! curl -fsS "http://127.0.0.1:3123/ping" >/dev/null 2>&1; then
51+
log "ERROR: Backend health check failed."
52+
exit 1
53+
fi
10754

108-
log "✅ Deployment to test environment completed successfully."
109-
exit 0
55+
log "✅ Deployment completed successfully."

0 commit comments

Comments
 (0)