-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.sh
More file actions
61 lines (47 loc) · 1.69 KB
/
deploy.sh
File metadata and controls
61 lines (47 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/bin/bash
# --- Configuration ---
PROJECT_ROOT="/data/nmrxiv-nodejs-microservice"
IMAGE="nfdi4chem/nodejs-microservice:latest"
COMPOSE_FILE="$PROJECT_ROOT/docker-compose.prod.yml"
LOG_FILE="/data/nmrxiv-nodejs-deploy.log"
LOG_DIR=$(dirname "$LOG_FILE")
# --- Helper Functions ---
log() {
echo "$(date '+%Y-%m-%d %H:%M:%S') $1" | tee -a "$LOG_FILE"
}
fatal() {
log "ERROR: $1"
exit 1
}
# --- Environment Setup ---
cd "$PROJECT_ROOT" || fatal "Could not change to project root directory $PROJECT_ROOT"
if [ ! -d "$LOG_DIR" ]; then
mkdir -p "$LOG_DIR" || fatal "Could not create log directory $LOG_DIR"
chmod 755 "$LOG_DIR"
fi
if [ ! -f "$LOG_FILE" ]; then
touch "$LOG_FILE" || fatal "Could not create log file $LOG_FILE"
chmod 644 "$LOG_FILE"
fi
START_TIME=$(date '+%Y-%m-%d %H:%M:%S')
log "Script started at: $START_TIME"
# --- Pre-checks ---
[ -f ".env" ] || fatal ".env file not found in $PROJECT_ROOT. Exiting."
[ -f "$COMPOSE_FILE" ] || fatal "Docker compose file not found at $COMPOSE_FILE. Exiting."
# --- Main Logic ---
log "Checking for updates to Docker image: $IMAGE"
PULL_OUTPUT=$(docker pull "$IMAGE")
log "$PULL_OUTPUT"
if [ "$(echo "$PULL_OUTPUT" | grep -c "Status: Image is up to date")" -eq 0 ]; then
log "New image detected. Redeploying service..."
docker compose -f "$COMPOSE_FILE" down | tee -a "$LOG_FILE"
docker compose -f "$COMPOSE_FILE" pull | tee -a "$LOG_FILE"
docker compose -f "$COMPOSE_FILE" up -d | tee -a "$LOG_FILE"
log "Deployment complete."
else
log "Image is up to date. No redeployment needed."
fi
log "Cleaning up unused Docker images..."
docker image prune -f | tee -a "$LOG_FILE"
END_TIME=$(date '+%Y-%m-%d %H:%M:%S')
log "Script finished at: $END_TIME"