From 4b66ddde6fc503d037162b543837848e31da91cf Mon Sep 17 00:00:00 2001 From: Iago Souza Date: Tue, 5 Nov 2024 09:56:02 -0300 Subject: [PATCH] oper: juntando teste com deploy --- .github/workflows/deployEC2.yaml | 60 ------------- .github/workflows/django.yaml | 65 -------------- .github/workflows/testAndDeploy.yaml | 124 +++++++++++++++++++++++++++ 3 files changed, 124 insertions(+), 125 deletions(-) delete mode 100644 .github/workflows/deployEC2.yaml delete mode 100644 .github/workflows/django.yaml create mode 100644 .github/workflows/testAndDeploy.yaml diff --git a/.github/workflows/deployEC2.yaml b/.github/workflows/deployEC2.yaml deleted file mode 100644 index d329f7d..0000000 --- a/.github/workflows/deployEC2.yaml +++ /dev/null @@ -1,60 +0,0 @@ -name: Deploy to AWS EC2 -on: - push: - branches: - - main - pull_request: - branches: - - main -env: - AWS_PRIVATE_KEY: ${{ secrets.KEYAWS }} - SSH_OPTIONS: '-o StrictHostKeyChecking=no -i key.pem ubuntu@98.80.44.121' - -jobs: - deploy: - runs-on: ubuntu-24.04 - steps: - - name: Checkout code - uses: actions/checkout@v2 - - - 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 \ No newline at end of file diff --git a/.github/workflows/django.yaml b/.github/workflows/django.yaml deleted file mode 100644 index ccb249d..0000000 --- a/.github/workflows/django.yaml +++ /dev/null @@ -1,65 +0,0 @@ -name: Django CI - -on: - push: - branches: - - '*' - -jobs: - build: - runs-on: ubuntu-latest - strategy: - max-parallel: 4 - matrix: - python-version: [3.7, 3.8, 3.9] - - services: - postgres: - image: postgres:13 - env: - POSTGRES_USER: ${{ secrets.DB_USERNAME }} - POSTGRES_PASSWORD: ${{ secrets.DB_PASSWORD }} - POSTGRES_DB: ${{ secrets.DB_NAME }} - ports: - - 5432:5432 - options: >- - --health-cmd "pg_isready -U ${{ secrets.DB_USERNAME }}" - --health-interval 10s - --health-timeout 5s - --health-retries 5 - - env: - DB_USERNAME: ${{ secrets.DB_USERNAME }} - DB_PASSWORD: ${{ secrets.DB_PASSWORD }} - DB_NAME: ${{ secrets.DB_NAME }} - DB_HOST: localhost - DB_PORT: 5432 - - 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.txt - - - name: Wait for PostgreSQL - run: | - until pg_isready -h ${{ env.DB_HOST }} -p ${{ env.DB_PORT }} -U ${{ secrets.DB_USERNAME }}; do - echo "Waiting for PostgreSQL..."; - sleep 5; - done - - - name: Apply Django Migrations - run: | - python manage.py makemigrations - python manage.py migrate - - - name: Run Tests - run: | - pytest diff --git a/.github/workflows/testAndDeploy.yaml b/.github/workflows/testAndDeploy.yaml new file mode 100644 index 0000000..9b8b99b --- /dev/null +++ b/.github/workflows/testAndDeploy.yaml @@ -0,0 +1,124 @@ +name: Django CI & Deploy + +on: + push: + branches: + - '*' # Rodar para qualquer push em qualquer branch + pull_request: + branches: + - main # Não é necessário, mas se quiser pode manter para PRs para 'main' + +jobs: + # Job para rodar os testes + build: + runs-on: ubuntu-latest + strategy: + max-parallel: 4 + matrix: + python-version: [3.7, 3.8, 3.9] + + services: + postgres: + image: postgres:13 + env: + POSTGRES_USER: ${{ secrets.DB_USERNAME }} + POSTGRES_PASSWORD: ${{ secrets.DB_PASSWORD }} + POSTGRES_DB: ${{ secrets.DB_NAME }} + ports: + - 5432:5432 + options: >- + --health-cmd "pg_isready -U ${{ secrets.DB_USERNAME }}" + --health-interval 10s + --health-timeout 5s + --health-retries 5 + + env: + DB_USERNAME: ${{ secrets.DB_USERNAME }} + DB_PASSWORD: ${{ secrets.DB_PASSWORD }} + DB_NAME: ${{ secrets.DB_NAME }} + DB_HOST: localhost + DB_PORT: 5432 + + 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.txt + + - name: Wait for PostgreSQL + run: | + until pg_isready -h ${{ env.DB_HOST }} -p ${{ env.DB_PORT }} -U ${{ secrets.DB_USERNAME }}; do + echo "Waiting for PostgreSQL..."; + sleep 5; + done + + - name: Apply Django Migrations + run: | + python manage.py makemigrations + python manage.py migrate + + - name: Run Tests + run: | + pytest + + # Job para fazer o deploy no AWS EC2, só é executado se o job de build for bem-sucedido + deploy: + runs-on: ubuntu-24.04 + needs: build # Este job só vai rodar se o job build for bem-sucedido + if: github.ref == 'refs/heads/main' # Executa apenas no push para a branch main + 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@v2 + + - 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