-
Notifications
You must be signed in to change notification settings - Fork 0
117 lines (99 loc) · 3.87 KB
/
Copy pathdeploy.yaml
File metadata and controls
117 lines (99 loc) · 3.87 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
name: Deploy to EC2
on:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
- name: Install Frontend Dependencies
run: npm ci
- name: Build Standalone Frontend
run: npm run build --workspace=apps/web
- name: Prepare Frontend Artifact
run: |
mkdir -p frontend_bundle/apps/web
cp -r apps/web/.next/standalone/* frontend_bundle/
if [ -d "apps/web/public" ]; then
cp -r apps/web/public frontend_bundle/apps/web/
fi
if [ -d "apps/web/.next/static" ]; then
mkdir -p frontend_bundle/apps/web/.next
cp -r apps/web/.next/static frontend_bundle/apps/web/.next/
fi
if [ -f "frontend_bundle/apps/web/server.js" ]; then
mv frontend_bundle/apps/web/server.js frontend_bundle/
fi
if [ -f "frontend_bundle/apps/web/package.json" ]; then
mv frontend_bundle/apps/web/package.json frontend_bundle/
fi
tar -czf frontend.tar.gz -C frontend_bundle .
- name: Prepare Backend Artifact
run: |
mkdir -p backend_bundle/backend
cp -r apps/api/* backend_bundle/backend/
cp -r ai backend_bundle/
tar -czf backend.tar.gz -C backend_bundle --exclude='node_modules' --exclude='.next' --exclude='venv' --exclude='__pycache__' .
- name: Copy Artifacts to EC2
uses: appleboy/scp-action@v0.1.7
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USERNAME }}
key: ${{ secrets.EC2_SSH_KEY }}
port: ${{ secrets.EC2_PORT }}
source: "frontend.tar.gz,backend.tar.gz"
target: "/tmp"
- name: Deploy and Restart Services
uses: appleboy/ssh-action@v1.0.3
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USERNAME }}
key: ${{ secrets.EC2_SSH_KEY }}
port: ${{ secrets.EC2_PORT }}
script: |
sudo mkdir -p /var/www/agrinn/frontend
sudo mkdir -p /var/www/agrinn/backend
sudo chown -R $USER:$USER /var/www/agrinn
# Deploy Frontend
rm -rf /var/www/agrinn/frontend/*
tar -xzf /tmp/frontend.tar.gz -C /var/www/agrinn/frontend
# Deploy Backend safely without deleting venv/models
tar -xzf /tmp/backend.tar.gz -C /var/www/agrinn/
rm /tmp/frontend.tar.gz /tmp/backend.tar.gz
# Setup Backend Dependencies
cd /var/www/agrinn/backend
if [ ! -d "venv" ]; then
python3 -m venv venv
fi
source venv/bin/activate
pip install -r requirements.txt
# Restart Frontend PM2 Process
cd /var/www/agrinn/frontend
PORT=3000 pm2 reload agrinn-web || PORT=3000 pm2 start server.js --name agrinn-web
pm2 save
# Restart Backend Systemd Service
if [ ! -f /etc/systemd/system/agrinn-api.service ]; then
sudo bash -c "cat > /etc/systemd/system/agrinn-api.service << 'EOF'
[Unit]
Description=AgriNN FastAPI Backend
After=network.target
[Service]
User=$USER
WorkingDirectory=/var/www/agrinn/backend
Environment=\"PATH=/var/www/agrinn/backend/venv/bin\"
ExecStart=/var/www/agrinn/backend/venv/bin/uvicorn app.main:app --host 0.0.0.0 --port 8000
Restart=always
[Install]
WantedBy=multi-user.target
EOF"
sudo systemctl daemon-reload
sudo systemctl enable agrinn-api
fi
sudo systemctl restart agrinn-api