feat: simplify production deployment workflow #7
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Deploy to EC2 | |
| on: | |
| push: | |
| branches: | |
| - main | |
| jobs: | |
| deploy: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout Repository | |
| uses: actions/checkout@v4 | |
| - name: Archive Repository | |
| run: | | |
| tar -czf deploy.tar.gz \ | |
| --exclude='node_modules' \ | |
| --exclude='.next' \ | |
| --exclude='venv' \ | |
| --exclude='dataset' \ | |
| --exclude='__pycache__' \ | |
| --exclude='.git' \ | |
| --exclude='outputs' \ | |
| --exclude='.pytest_cache' \ | |
| --exclude='.mypy_cache' \ | |
| --exclude='.ruff_cache' \ | |
| . | |
| - name: Copy Archive 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: "deploy.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: | | |
| mkdir -p ~/agrinn | |
| # Extract over existing repository safely (preserves venv/models) | |
| tar -xzf /tmp/deploy.tar.gz -C ~/agrinn/ | |
| rm /tmp/deploy.tar.gz | |
| # Build Frontend natively on EC2 | |
| cd ~/agrinn/apps/web | |
| npm install | |
| npm run build | |
| # Restart Frontend PM2 | |
| pm2 delete agrinn-web || true | |
| PORT=3000 pm2 start npm --name agrinn-web -- start | |
| pm2 save | |
| # Setup Backend Dependencies | |
| cd ~/agrinn/apps/api | |
| if [ ! -d "venv" ]; then | |
| python3 -m venv venv | |
| fi | |
| source venv/bin/activate | |
| pip install -r requirements.txt | |
| # 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=/home/$USER/agrinn/apps/api | |
| Environment=\"PATH=/home/$USER/agrinn/apps/api/venv/bin\" | |
| ExecStart=/home/$USER/agrinn/apps/api/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 |