forked from decodedcorp/decoded-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy-remote.sh
More file actions
executable file
Β·176 lines (144 loc) Β· 4.06 KB
/
deploy-remote.sh
File metadata and controls
executable file
Β·176 lines (144 loc) Β· 4.06 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/bin/bash
# Remote deployment script for decoded-app
# Usage: ./deploy-remote.sh
set -e
# Configuration
REMOTE_HOST="121.130.214.186"
REMOTE_PORT="2202"
REMOTE_USER="decoded"
REMOTE_DIR="/home/decoded/decoded-app"
LOCAL_DIR="."
echo "π Starting remote deployment to $REMOTE_USER@$REMOTE_HOST:$REMOTE_PORT"
# Check if we're in the right directory
if [ ! -f "package.json" ]; then
echo "β Error: package.json not found. Please run this script from the project root."
exit 1
fi
# Build the application locally
echo "π¨ Building the application locally..."
yarn build
if [ $? -ne 0 ]; then
echo "β Build failed!"
exit 1
fi
echo "β
Build completed successfully!"
# Create deployment package
echo "π¦ Creating deployment package..."
DEPLOY_DIR="deploy-package"
rm -rf $DEPLOY_DIR
mkdir -p $DEPLOY_DIR
# Copy necessary files
cp -r .next $DEPLOY_DIR/
cp -r public $DEPLOY_DIR/
cp package.json $DEPLOY_DIR/
cp yarn.lock $DEPLOY_DIR/
cp next.config.ts $DEPLOY_DIR/
cp tailwind.config.ts $DEPLOY_DIR/
cp tsconfig.json $DEPLOY_DIR/
cp -r src $DEPLOY_DIR/
# Create .env.production if it doesn't exist
if [ ! -f ".env.production" ]; then
echo "π Creating .env.production file..."
cat > $DEPLOY_DIR/.env.production << EOF
NODE_ENV=production
NEXT_PUBLIC_API_BASE_URL=https://api.decoded.style
NEXT_PUBLIC_GOOGLE_CLIENT_ID=your_google_client_id_here
NEXT_PUBLIC_GOOGLE_REDIRECT_URI=https://121.130.214.186:3000/auth/callback
PORT=3000
EOF
else
cp .env.production $DEPLOY_DIR/
fi
# Create remote deployment script
cat > $DEPLOY_DIR/deploy-remote-setup.sh << 'EOF'
#!/bin/bash
set -e
echo "π§ Setting up application on remote server..."
# Install dependencies
echo "π¦ Installing dependencies..."
yarn install --frozen-lockfile
# Start the application
echo "π Starting the application..."
yarn start
EOF
chmod +x $DEPLOY_DIR/deploy-remote-setup.sh
# Create PM2 ecosystem file for remote
cat > $DEPLOY_DIR/ecosystem.config.js << EOF
module.exports = {
apps: [
{
name: 'decoded-app',
script: 'yarn',
args: 'start',
cwd: '$REMOTE_DIR',
instances: 1,
autorestart: true,
watch: false,
max_memory_restart: '1G',
env: {
NODE_ENV: 'production',
PORT: 3000
}
}
]
};
EOF
# Create systemd service file
cat > $DEPLOY_DIR/decoded-app.service << EOF
[Unit]
Description=Decoded App
After=network.target
[Service]
Type=simple
User=decoded
WorkingDirectory=$REMOTE_DIR
ExecStart=/usr/bin/yarn start
Restart=always
RestartSec=10
Environment=NODE_ENV=production
Environment=PORT=3000
[Install]
WantedBy=multi-user.target
EOF
# Create deployment archive
echo "π¦ Creating deployment archive..."
tar -czf decoded-app-deploy.tar.gz -C $DEPLOY_DIR .
# Upload to remote server
echo "π€ Uploading to remote server..."
scp -P $REMOTE_PORT decoded-app-deploy.tar.gz $REMOTE_USER@$REMOTE_HOST:/tmp/
# Execute remote deployment
echo "π§ Executing remote deployment..."
ssh -p $REMOTE_PORT $REMOTE_USER@$REMOTE_HOST << 'EOF'
set -e
echo "π§ Starting remote deployment process..."
# Create app directory if it doesn't exist
mkdir -p /home/decoded/decoded-app
# Stop existing application if running
if pm2 list | grep -q "decoded-app"; then
echo "π Stopping existing application..."
pm2 stop decoded-app
pm2 delete decoded-app
fi
# Extract deployment package
echo "π¦ Extracting deployment package..."
cd /home/decoded/decoded-app
tar -xzf /tmp/decoded-app-deploy.tar.gz
rm /tmp/decoded-app-deploy.tar.gz
# Install dependencies
echo "π¦ Installing dependencies..."
yarn install --frozen-lockfile
# Start with PM2
echo "π Starting application with PM2..."
pm2 start ecosystem.config.js
pm2 save
echo "β
Deployment completed successfully!"
echo "π Application should be available at: http://121.130.214.186:3000"
echo "π PM2 Status:"
pm2 status
EOF
# Cleanup local files
echo "π§Ή Cleaning up local files..."
rm -rf $DEPLOY_DIR
rm decoded-app-deploy.tar.gz
echo "β
Remote deployment completed successfully!"
echo "π Your application should now be running at: http://121.130.214.186:3000"