-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy-app.sh
More file actions
executable file
Β·138 lines (118 loc) Β· 4.16 KB
/
deploy-app.sh
File metadata and controls
executable file
Β·138 lines (118 loc) Β· 4.16 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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/bin/bash
# Deployment script for bitspace-pos application
# Creates zips, uploads to server, and auto-extracts
set -e # Exit on error
# Trap errors and exit to prevent terminal from closing immediately
cleanup_on_error() {
exit_code=$?
if [ $exit_code -ne 0 ]; then
echo ""
echo "β Error occurred! (Exit code: $exit_code)"
echo "Check the output above for details."
echo ""
read -p "Press Enter to close..."
fi
}
trap cleanup_on_error EXIT ERR
# Load environment variables from .env file
if [ ! -f .env ]; then
echo "β Error: .env file not found!"
echo " Please create a .env file with APP_SERVER, APP_PORT, and APP_REMOTE_PATH variables."
echo " (Or use legacy SERVER, PORT, REMOTE_PATH variables)"
exit 1
fi
echo "π Loading configuration from .env file..."
# Try to load APP_* variables first, fall back to legacy variables
export $(grep -v '^#' .env | grep -E '^(APP_|SERVER|PORT|REMOTE_PATH)' | xargs)
# Use APP_* variables if available, otherwise use legacy variables
SERVER="${APP_SERVER:-${SERVER}}"
PORT="${APP_PORT:-${PORT}}"
REMOTE_PATH="${APP_REMOTE_PATH:-${REMOTE_PATH}}"
# Validate required variables
if [ -z "$SERVER" ] || [ -z "$PORT" ] || [ -z "$REMOTE_PATH" ]; then
echo "β Error: Missing required environment variables"
echo " Please ensure SERVER, PORT, and REMOTE_PATH are set in .env"
exit 1
fi
# Options
AUTO_EXTRACT=true # Auto-extract on server
CLEANUP_REMOTE_ZIPS=true # Remove zip files from server after extraction
CLEANUP_LOCAL_ZIPS=true # Remove local zip files after upload
echo "π Starting app deployment process..."
echo "π Target: ${SERVER}:${REMOTE_PATH}"
echo ""
# Step 1: Run yarn build
echo ""
echo "βοΈ Running yarn build with production config..."
cp .env.production .env.build.backup
NODE_ENV=production yarn build
echo "β Build complete"
# Validate required directories exist
echo ""
echo "π Checking required directories..."
missing_dirs=0
for dir in .nuxt .output; do
if [ ! -d "$dir" ]; then
echo " β Missing: $dir"
missing_dirs=$((missing_dirs + 1))
else
echo " β Found: $dir"
fi
done
if [ $missing_dirs -gt 0 ]; then
echo ""
echo "β Error: Missing required directories after build."
echo " Check the output above for errors."
exit 1
fi
# Step 2: Create zip files (excluding macOS metadata)
echo ""
echo "π¦ Creating zip files..."
echo " β Zipping .nuxt folder..."
zip -r -X -q nuxt.zip .nuxt
echo " β Zipping .output folder..."
zip -r -X -q output.zip .output
echo "β Zip files created"
# Step 3: Upload to server
echo ""
echo "β¬οΈ Uploading to ${SERVER}..."
scp -P "${PORT}" -q nuxt.zip output.zip "${SERVER}:${REMOTE_PATH}/"
echo " β Uploading .env.production..."
scp -P "${PORT}" -q .env.production "${SERVER}:${REMOTE_PATH}/.env"
echo "β Upload complete"
# Step 4: Extract on server (if enabled)
if [ "$AUTO_EXTRACT" = true ]; then
echo ""
echo "π Extracting files on server..."
ssh -p "${PORT}" "${SERVER}" "cd ${REMOTE_PATH} && \
unzip -o -q nuxt.zip && \
unzip -o -q output.zip && \
cp .env .output/.env && \
rm -rf __MACOSX"
echo "β Files extracted"
# Step 5: Restart PM2 process
echo ""
echo "π Configuring nostr bitos PM2 process..."
# Check if process runs (restart), otherwise start it
ssh -p "${PORT}" "${SERVER}" "cd ${REMOTE_PATH} && (pm2 restart bitos || pm2 start \"yarn preview -p 9009\" --name \"bitos\")"
echo "β PM2 process running"
# Cleanup remote zip files (if enabled)
if [ "$CLEANUP_REMOTE_ZIPS" = true ]; then
echo ""
echo "π§Ή Cleaning up remote zip files..."
ssh -p "${PORT}" "${SERVER}" "cd ${REMOTE_PATH} && rm -f nuxt.zip output.zip"
echo "β Remote cleanup complete"
fi
fi
# Step 6: Cleanup local zip files (if enabled)
if [ "$CLEANUP_LOCAL_ZIPS" = true ]; then
echo ""
echo "π§Ή Cleaning up local zip files..."
rm -f nuxt.zip output.zip
echo "β Local cleanup complete"
fi
echo ""
echo "β
Deployment complete!"
echo "π Deployed to: ${SERVER}:${REMOTE_PATH}/"
echo ""
read -p "Press Enter to close..."