-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 40-func/setup-prod
40 func/setup prod
- Loading branch information
Showing
8 changed files
with
215 additions
and
154 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
name: Django CI & Deploy | ||
|
||
on: | ||
push: | ||
branches: | ||
- '**' # Trigger on push to any branch | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
strategy: | ||
max-parallel: 4 | ||
matrix: | ||
python-version: [3.12] | ||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- name: Set up Python ${{ matrix.python-version }} | ||
uses: actions/setup-python@v3 | ||
with: | ||
python-version: ${{ matrix.python-version }} | ||
|
||
- name: Install Dependencies | ||
run: | | ||
python -m pip install --upgrade pip | ||
pip install -r requirements-dev.txt | ||
- name: Start PostgreSQL service | ||
run: | | ||
docker run --name postgres-db -d \ | ||
-e POSTGRES_USER=${{ secrets.DB_USERNAME }} \ | ||
-e POSTGRES_PASSWORD=${{ secrets.DB_PASSWORD }} \ | ||
-e POSTGRES_DB=${{ secrets.DB_NAME }} \ | ||
-p 5432:5432 postgres:13 | ||
# Wait until PostgreSQL is ready | ||
until docker exec postgres-db pg_isready -U ${{ secrets.DB_USERNAME }}; do | ||
echo "Waiting for PostgreSQL to be ready..."; | ||
sleep 5; | ||
done | ||
- name: Create .env file on git machine | ||
run: | | ||
echo "SECRET_KEY=${{ secrets.SECRET_KEY }}" >> .env | ||
echo "DB_USER=${{ secrets.DB_USERNAME }}" >> .env | ||
echo "DB_PASSWORD=${{ secrets.DB_PASSWORD }}" >> .env | ||
echo "DB_NAME=${{ secrets.DB_NAME }}" >> .env | ||
echo "DB_HOST=localhost" >> .env | ||
echo "DB_PORT=${{ secrets.DB_PORT }}" >> .env | ||
- name: Apply Django Migrations | ||
run: | | ||
python src/tupan/manage.py makemigrations | ||
python src/tupan/manage.py migrate | ||
- name: Run Tests | ||
run: | | ||
cd src/tupan && pytest | ||
- name: Stop PostgreSQL container | ||
run: | | ||
docker stop postgres-db | ||
docker rm postgres-db | ||
# Job for deploying to AWS EC2, only if the build job is successful | ||
deploy: | ||
runs-on: ubuntu-24.04 | ||
needs: build # This job will only run if the build job is successful | ||
if: github.ref == 'refs/heads/main' # Run only on push to the main branch | ||
env: | ||
AWS_PRIVATE_KEY: ${{ secrets.KEYAWS }} | ||
SSH_OPTIONS: '-o StrictHostKeyChecking=no -i key.pem ubuntu@98.80.44.121' | ||
|
||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v4 | ||
|
||
- name: Set permissions for private key | ||
run: | | ||
echo "${{ env.AWS_PRIVATE_KEY }}" > key.pem | ||
chmod 600 key.pem | ||
- name: Stop running containers | ||
run: | | ||
ssh ${{ env.SSH_OPTIONS }} 'if [ -d /home/ubuntu/tupan-back/ ]; then cd /home/ubuntu/tupan-back/ && sudo docker-compose down; fi' | ||
- name: Create target directory on AWS instance | ||
run: | | ||
ssh ${{ env.SSH_OPTIONS }} 'mkdir -p /home/ubuntu/tupan-back/' | ||
- name: Create .env file | ||
run: | | ||
echo "SECRET_KEY=${{ secrets.SECRET_KEY }}" >> .env | ||
echo "DB_USER=${{ secrets.DB_USERNAME }}" >> .env | ||
echo "DB_PASSWORD=${{ secrets.DB_PASSWORD }}" >> .env | ||
echo "DB_NAME=${{ secrets.DB_NAME }}" >> .env | ||
echo "DB_HOST=${{ secrets.DB_HOST }}" >> .env | ||
echo "DB_PORT=${{ secrets.DB_PORT }}" >> .env | ||
- name: Transfer code to AWS instance | ||
run: | | ||
rsync -av --delete --exclude='.git' --exclude='.github' --exclude='.husky' -e "ssh -o StrictHostKeyChecking=no -i key.pem" --rsync-path="sudo rsync" ./ ubuntu@98.80.44.121:/home/ubuntu/tupan-back | ||
- name: Set permissions for entrypoint | ||
run: | | ||
ssh ${{ env.SSH_OPTIONS }} 'cd /home/ubuntu/tupan-back/ && sudo chmod +x entrypoint.sh' | ||
- name: Build Docker Compose | ||
run: | | ||
ssh ${{ env.SSH_OPTIONS }} 'cd /home/ubuntu/tupan-back/ && sudo docker-compose build' | ||
- name: Run Docker Compose | ||
run: | | ||
ssh ${{ env.SSH_OPTIONS }} 'cd /home/ubuntu/tupan-back/ && sudo docker-compose up -d' | ||
- name: Cleanup SSH key | ||
run: | | ||
rm -f key.pem |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
FROM python:3.11 | ||
|
||
# Definir o diretório de trabalho dentro do container | ||
WORKDIR /app | ||
|
||
# Instalar netcat (openbsd) | ||
RUN apt-get update && apt-get install -y netcat-openbsd | ||
|
||
# Copiar o arquivo de requisitos para o container | ||
COPY requirements.txt /app/ | ||
COPY entrypoint.sh /app/ | ||
|
||
# Instalar as dependências | ||
RUN pip install --upgrade pip && pip install -r requirements.txt | ||
|
||
# Copiar o código da aplicação | ||
COPY . /app/ | ||
|
||
# Criar um script de entrada | ||
COPY entrypoint.sh /app/entrypoint.sh | ||
RUN chmod +x /app/entrypoint.sh | ||
|
||
# Expôr a porta que o Django vai usar | ||
EXPOSE 8000 | ||
|
||
# Comando para rodar o script de entrada | ||
CMD ["/app/entrypoint.sh"] | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
version: '3' | ||
|
||
services: | ||
web: | ||
image: tupan-back | ||
build: . | ||
volumes: | ||
- .:/app | ||
ports: | ||
- "8000:8000" | ||
environment: | ||
- DEBUG=True | ||
- DATABASE_URL=postgres://postgres:fatec@db:5432/tupan | ||
restart: always | ||
db: | ||
image: postgres | ||
environment: | ||
POSTGRES_DB: tupan | ||
POSTGRES_USER: postgres | ||
POSTGRES_PASSWORD: fatec | ||
ports: | ||
- "5432:5432" | ||
volumes: | ||
- postgres_data:/var/lib/postgresql/data/ | ||
restart: always | ||
volumes: | ||
postgres_data: | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#!/bin/bash | ||
|
||
# Aguarde o PostgreSQL estar pronto | ||
echo "Aguardando o PostgreSQL estar pronto..." | ||
while ! nc -z db 5432; do | ||
sleep 0.1 # espera 1/10 de segundo | ||
done | ||
echo "PostgreSQL está pronto!" | ||
|
||
# Rodar migrações | ||
python src/tupan/manage.py makemigrations | ||
python src/tupan/manage.py migrate --noinput | ||
|
||
# Iniciar o servidor | ||
exec python src/tupan/manage.py runserver 0.0.0.0:8000 |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,2 @@ | ||
import pytest | ||
import json | ||
from django.urls import reverse | ||
from .models import Alerta, HistoricoAlerta | ||
|
||
@pytest.fixture | ||
def alerta_auxiliar(): | ||
alerta = { | ||
"nome": "Alerta1", | ||
"condicao": "<2", | ||
} | ||
return alerta | ||
|
||
|
||
class TestAlerta: | ||
@pytest.mark.django_db | ||
def teste_criar_alerta(self, alerta_auxiliar): | ||
Alerta.objects.create(**alerta_auxiliar) | ||
|
||
assert Alerta.objects.count() == 1 | ||
alerta_no_banco = Alerta.objects.first() | ||
|
||
assert alerta_no_banco.pk == 1 | ||
assert alerta_no_banco.nome == alerta_auxiliar['nome'] | ||
assert alerta_no_banco.condicao == alerta_auxiliar['condicao'] | ||
assert alerta_no_banco.ativo == True | ||
|
||
@pytest.mark.django_db | ||
def teste_url_listar_alertas(self, client, alerta_auxiliar): | ||
Alerta.objects.create(**alerta_auxiliar) | ||
|
||
url = reverse("alertas") | ||
response = client.get(url) | ||
|
||
json_data = response.json() | ||
|
||
assert response.status_code == 200 | ||
assert len(json_data) == 1 | ||
assert json_data[0]['nome'] == "Alerta1" | ||
assert json_data[0]['condicao'] == "<2" | ||
|
||
@pytest.mark.django_db | ||
def teste_url_cadastrar_alerta(self, client, alerta_auxiliar): | ||
|
||
url = reverse("alertas") | ||
response = client.post(url, data=json.dumps(alerta_auxiliar), content_type="application/json") | ||
|
||
json_data = response.json() | ||
assert response.status_code == 201 | ||
assert json_data['nome'] == 'Alerta1' | ||
assert json_data['ativo'] == True | ||
|
||
|
||
class TestHistoricoAlerta: | ||
@pytest.fixture | ||
def historico_alerta_auxiliar(self, alerta_auxiliar): | ||
alerta = Alerta.objects.create(**alerta_auxiliar) | ||
hist = { | ||
"timestamp": 1726067292, | ||
"alerta": alerta | ||
} | ||
return hist | ||
|
||
@pytest.mark.django_db | ||
def teste_criar_historico(self, historico_alerta_auxiliar): | ||
HistoricoAlerta.objects.create(**historico_alerta_auxiliar) | ||
|
||
assert HistoricoAlerta.objects.count() == 1 | ||
historico_no_banco = HistoricoAlerta.objects.first() | ||
|
||
assert historico_no_banco.pk == 1 | ||
assert historico_no_banco.timestamp == historico_alerta_auxiliar['timestamp'] | ||
assert historico_no_banco.alerta == historico_alerta_auxiliar['alerta'] |
This file contains 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
Oops, something went wrong.