-
Notifications
You must be signed in to change notification settings - Fork 0
148 lines (121 loc) · 4.5 KB
/
Copy pathdeploy-backend.yml
File metadata and controls
148 lines (121 loc) · 4.5 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
name: Deploy Backend to EC2
on:
push:
branches:
- main
paths:
- 'backend/**'
workflow_dispatch: # Permite ejecutar manualmente
env:
AWS_REGION: us-east-1 # Cambia a tu región
jobs:
test:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:15
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: ventu_test
ports:
- 5432:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- name: Checkout código
uses: actions/checkout@v4
- name: Configurar Python
uses: actions/setup-python@v5
with:
python-version: '3.11'
cache: 'pip'
cache-dependency-path: backend/requirements.txt
- name: Instalar dependencias
working-directory: backend
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Ejecutar tests
working-directory: backend
env:
DATABASE_URL: postgres://postgres:postgres@localhost:5432/ventu_test
SECRET_KEY: test-secret-key-for-ci
DEBUG: 'False'
ALLOWED_HOSTS: localhost,127.0.0.1
run: |
python manage.py migrate --noinput
python manage.py test --verbosity=2
continue-on-error: true # No fallar si no hay tests aún
deploy:
needs: test
runs-on: ubuntu-latest
steps:
- name: Checkout código
uses: actions/checkout@v4
- name: Configurar credenciales AWS
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}
- name: Agregar host SSH conocido
run: |
mkdir -p ~/.ssh
ssh-keyscan -H ${{ secrets.EC2_HOST }} >> ~/.ssh/known_hosts
- name: Configurar clave SSH
run: |
echo "${{ secrets.EC2_SSH_PRIVATE_KEY }}" > ~/.ssh/ec2_key.pem
chmod 600 ~/.ssh/ec2_key.pem
- name: Deploy al servidor EC2
env:
EC2_USER: ${{ secrets.EC2_USER }}
EC2_HOST: ${{ secrets.EC2_HOST }}
run: |
ssh -v -o ConnectTimeout=30 -i ~/.ssh/ec2_key.pem $EC2_USER@$EC2_HOST << 'ENDSSH'
set -ex
echo "📂 Navegando al directorio del proyecto..."
cd ~/ventu/backend
echo "🔄 Actualizando código desde GitHub..."
git fetch origin main
git reset --hard origin/main
echo "🐍 Activando entorno virtual..."
source venv/bin/activate
echo "📦 Instalando dependencias..."
pip install -r requirements.txt --quiet
echo "🗄️ Aplicando migraciones..."
python manage.py migrate --noinput
echo "📁 Recolectando archivos estáticos..."
python manage.py collectstatic --noinput
echo "🔄 Reiniciando Gunicorn..."
sudo systemctl restart gunicorn
echo "✅ Deploy completado exitosamente!"
ENDSSH
- name: Verificar salud del servidor
env:
API_URL: ${{ secrets.VITE_API_URL }}
run: |
echo "Esperando que el servidor inicie..."
sleep 10
# Verificar que el servidor responde
HTTP_STATUS=$(curl -s -o /dev/null -w "%{http_code}" "${API_URL}/api/health/" || echo "000")
if [ "$HTTP_STATUS" = "200" ]; then
echo "✅ Servidor respondiendo correctamente (HTTP $HTTP_STATUS)"
else
echo "⚠️ Servidor respondió con HTTP $HTTP_STATUS"
# No fallar el workflow, solo advertir
fi
- name: Resumen del deploy
run: |
echo "## ✅ Backend desplegado exitosamente" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "**Servidor:** EC2" >> $GITHUB_STEP_SUMMARY
echo "**Región:** ${{ env.AWS_REGION }}" >> $GITHUB_STEP_SUMMARY
echo "**Commit:** ${{ github.sha }}" >> $GITHUB_STEP_SUMMARY
echo "**Fecha:** $(date -u '+%Y-%m-%d %H:%M:%S UTC')" >> $GITHUB_STEP_SUMMARY
- name: Limpiar clave SSH
if: always()
run: rm -f ~/.ssh/ec2_key.pem