diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index a8e5a27..5fe570a 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -1,5 +1,4 @@ name: Deploy QR Rapido - on: push: branches: [ main, develop ] @@ -7,8 +6,8 @@ on: branches: [ main ] env: - REGISTRY: ghcr.io - IMAGE_NAME: ${{ github.repository }} + REGISTRY: registry.redecarneir.us + IMAGE_NAME: qrrapido jobs: test: @@ -47,43 +46,31 @@ jobs: build-and-push: needs: test runs-on: ubuntu-latest - if: github.ref == 'refs/heads/main' + if: github.ref == 'refs/heads/main' || github.ref == 'refs/heads/develop' - permissions: - contents: read - packages: write - steps: - name: Checkout uses: actions/checkout@v4 - - name: Login to Container Registry - uses: docker/login-action@v3 - with: - registry: ${{ env.REGISTRY }} - username: ${{ github.actor }} - password: ${{ secrets.GITHUB_TOKEN }} + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Build and push to registry + run: | + # Determina a tag baseada na branch + if [ "${{ github.ref }}" = "refs/heads/main" ]; then + TAG="latest" + else + TAG="develop" + fi - - name: Extract metadata - id: meta - uses: docker/metadata-action@v5 - with: - images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} - tags: | - type=ref,event=branch - type=ref,event=pr - type=sha,prefix={{branch}}- - type=raw,value=latest,enable={{is_default_branch}} - - - name: Build and push - uses: docker/build-push-action@v5 - with: - context: . - push: true - tags: ${{ steps.meta.outputs.tags }} - labels: ${{ steps.meta.outputs.labels }} - cache-from: type=gha - cache-to: type=gha,mode=max + # Build da imagem + docker build -t ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:$TAG . + + # Push para o registry (sem autenticação conforme mencionado) + docker push ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:$TAG + + echo "IMAGE_TAG=$TAG" >> $GITHUB_ENV deploy-staging: needs: build-and-push @@ -91,12 +78,54 @@ jobs: if: github.ref == 'refs/heads/develop' steps: - - name: Deploy to Staging - uses: azure/webapps-deploy@v2 - with: - app-name: 'qrrapido-staging' - publish-profile: ${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE_STAGING }} - images: '${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:develop' + - name: Deploy to Staging Servers + run: | + # Configura SSH known_hosts + mkdir -p ~/.ssh + ssh-keyscan -H 141.148.162.114 >> ~/.ssh/known_hosts + ssh-keyscan -H 129.146.116.218 >> ~/.ssh/known_hosts + + # Deploy no Servidor 1 + ssh ubuntu@141.148.162.114 << 'EOF' + # Para o container atual se existir + docker stop qrrapido-staging || true + docker rm qrrapido-staging || true + + # Remove imagem antiga + docker rmi ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:develop || true + + # Puxa nova imagem + docker pull ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:develop + + # Executa novo container + docker run -d \ + --name qrrapido-staging \ + --restart unless-stopped \ + -p 5000:8080 \ + -e ASPNETCORE_ENVIRONMENT=Staging \ + ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:develop + EOF + + # Deploy no Servidor 2 + ssh ubuntu@129.146.116.218 << 'EOF' + # Para o container atual se existir + docker stop qrrapido-staging || true + docker rm qrrapido-staging || true + + # Remove imagem antiga + docker rmi ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:develop || true + + # Puxa nova imagem + docker pull ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:develop + + # Executa novo container + docker run -d \ + --name qrrapido-staging \ + --restart unless-stopped \ + -p 5000:8080 \ + -e ASPNETCORE_ENVIRONMENT=Staging \ + ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:develop + EOF deploy-production: needs: build-and-push @@ -105,17 +134,69 @@ jobs: environment: production steps: - - name: Deploy to Production - uses: azure/webapps-deploy@v2 - with: - app-name: 'qrrapido-production' - publish-profile: ${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE_PROD }} - images: '${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest' + - name: Deploy to Production Servers + run: | + # Configura SSH known_hosts + mkdir -p ~/.ssh + ssh-keyscan -H 141.148.162.114 >> ~/.ssh/known_hosts + ssh-keyscan -H 129.146.116.218 >> ~/.ssh/known_hosts - - name: Notify Slack - uses: 8398a7/action-slack@v3 - with: - status: ${{ job.status }} - channel: '#deployments' - webhook_url: ${{ secrets.SLACK_WEBHOOK }} - text: "QR Rapido deployed to production! 🚀" \ No newline at end of file + # Deploy no Servidor 1 (com NGINX) + ssh ubuntu@141.148.162.114 << 'EOF' + # Para o container atual se existir + docker stop qrrapido-prod || true + docker rm qrrapido-prod || true + + # Remove imagem antiga + docker rmi ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest || true + + # Puxa nova imagem + docker pull ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest + + # Executa novo container + docker run -d \ + --name qrrapido-prod \ + --restart unless-stopped \ + -p 5001:8080 \ + -e ASPNETCORE_ENVIRONMENT=Production \ + -e ASPNETCORE_URLS=http://+:8080 \ + ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest + + # Recarrega NGINX para garantir que está apontando para o novo container + sudo nginx -t && sudo systemctl reload nginx + EOF + + # Deploy no Servidor 2 + ssh ubuntu@129.146.116.218 << 'EOF' + # Para o container atual se existir + docker stop qrrapido-prod || true + docker rm qrrapido-prod || true + + # Remove imagem antiga + docker rmi ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest || true + + # Puxa nova imagem + docker pull ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest + + # Executa novo container + docker run -d \ + --name qrrapido-prod \ + --restart unless-stopped \ + -p 5001:8080 \ + -e ASPNETCORE_ENVIRONMENT=Production \ + -e ASPNETCORE_URLS=http://+:8080 \ + ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest + EOF + + - name: Health Check + run: | + # Aguarda um pouco para os containers subirem + sleep 30 + + # Verifica se os serviços estão respondendo + echo "Verificando Servidor 1..." + ssh ubuntu@141.148.162.114 'curl -f http://localhost:5001/health || echo "Servidor 1 pode não estar respondendo"' + + echo "Verificando Servidor 2..." + ssh ubuntu@129.146.116.218 'curl -f http://localhost:5001/health || echo "Servidor 2 pode não estar respondendo"' + diff --git a/QRRapidoApp.sln b/QRRapidoApp.sln index 8089a49..efd3580 100644 --- a/QRRapidoApp.sln +++ b/QRRapidoApp.sln @@ -1,10 +1,15 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 -VisualStudioVersion = 17.13.35818.85 d17.13 +VisualStudioVersion = 17.13.35818.85 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QRRapidoApp", "QRRapidoApp.csproj", "{8AF92774-40E8-830E-08B3-67F0A0B91DDE}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "RootFolder", "RootFolder", "{02EA681E-C7D8-13C7-8484-4AC65E1B71E8}" + ProjectSection(SolutionItems) = preProject + .github\workflows\deploy.yml = .github\workflows\deploy.yml + EndProjectSection +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU