-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.sh
executable file
·92 lines (79 loc) · 2.12 KB
/
run.sh
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/bin/bash
# Func to log messages to term and file
log() {
local message="$1"
echo "$message" | tee -a "$LOGFILE"
}
# Set default log file name
LOGFILE="run.log"
# Helper func to load .env files
load_env_file() {
local env_file="$1"
BUILD_ARGS=""
if [ -f "$env_file" ]; then
# Use a while loop with file descriptor to ensure proper handling of the last line
while IFS='=' read -r key value || [ -n "$key" ]; do
# Skip lines that are empty or start with a comment
[[ -z "$key" || "$key" =~ ^[[:space:]]*# ]] && continue
# Escape single quotes in values
value=$(printf '%s\n' "$value" | sed "s/'/'\\\\''/g")
# Append build arg string
BUILD_ARGS+="--build-arg $key=$value "
done < "$env_file"
log "Loaded environment variables from $env_file"
log "Build args: $BUILD_ARGS"
else
log "Error: $env_file not found."
exit 1
fi
}
# Main run script logic
main() {
# Check for env arg
if [ -z "$ENV" ]; then
log "Usage: ./run.sh [dev|prod]"
exit 1
fi
# Determine env
case "$ENV" in
prod)
export RAILS_ENV="production"
ENV_FILE=".env.prod"
;;
dev)
export RAILS_ENV="development"
ENV_FILE=".env.dev"
;;
*)
log "Invalid environment: $ENV. Use 'dev' or 'prod'."
exit 1
;;
esac
log "Environment set to $ENV. RAILS_ENV set to $RAILS_ENV"
load_env_file "$ENV_FILE"
# Removing old volumes
log "Removing old volumes"
if [ "$(docker ps -q)" ]; then
docker-compose down -v >> "$LOGFILE" 2>&1 || {
log "Error removing old volumes. Check $LOGFILE for details."
exit 1
}
fi
# Build Docker containers
log "Building Docker containers"
docker-compose build --no-cache $BUILD_ARGS >> "$LOGFILE" 2>&1 || {
log "Build failed. Check $LOGFILE for details."
exit 1
}
# Start Docker containers
log "Starting Docker containers"
docker-compose up -d >> "$LOGFILE" 2>&1 || {
log "Error starting containers. Check $LOGFILE for details."
exit 1
}
log "Docker containers started successfully"
}
# Set env from arg
ENV=$1
# Start main func
main