name: Deploy BCards on: push: branches: [ main, 'release/*' ] pull_request: branches: [ main ] env: REGISTRY: registry.redecarneir.us IMAGE_NAME: bcards jobs: test: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: Setup .NET uses: actions/setup-dotnet@v4 with: dotnet-version: 8.0.x - name: Cache dependencies uses: actions/cache@v3 with: path: ~/.nuget/packages key: ${{ runner.os }}-nuget-${{ hashFiles('**/*.csproj') }} restore-keys: | ${{ runner.os }}-nuget- - name: Restore dependencies run: dotnet restore - name: Build run: dotnet build --no-restore --configuration Release - name: Test run: dotnet test --no-build --configuration Release --verbosity normal --collect:"XPlat Code Coverage" - name: Upload coverage uses: codecov/codecov-action@v3 with: files: coverage.cobertura.xml build-and-push: needs: test runs-on: ubuntu-latest if: github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/heads/release/') steps: - name: Checkout uses: actions/checkout@v4 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 - name: Determine build settings run: | if [ "${{ github.ref }}" = "refs/heads/main" ]; then echo "TAG=latest" >> $GITHUB_ENV echo "PLATFORM=linux/arm64" >> $GITHUB_ENV echo "ENVIRONMENT=Production" >> $GITHUB_ENV elif [[ "${{ github.ref }}" == refs/heads/release/* ]]; then # Extrai versão da branch (release/v1.0.0 -> v1.0.0) VERSION=$(echo "${{ github.ref }}" | sed 's/refs\/heads\/release\///') echo "TAG=${VERSION}" >> $GITHUB_ENV echo "PLATFORM=linux/amd64" >> $GITHUB_ENV echo "ENVIRONMENT=Staging" >> $GITHUB_ENV fi echo "IMAGE_TAG=$TAG" >> $GITHUB_ENV - name: Build and push to registry run: | # Build da imagem para a plataforma correta docker buildx build \ --platform ${{ env.PLATFORM }} \ --tag ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ env.TAG }} \ --push \ . deploy-production: needs: build-and-push runs-on: ubuntu-latest if: github.ref == 'refs/heads/main' environment: production steps: - name: Deploy to Production Servers (ARM - OCI) run: | # Configura SSH mkdir -p ~/.ssh echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa chmod 600 ~/.ssh/id_rsa # Deploy no Servidor 1 (ARM - OCI) ssh -o StrictHostKeyChecking=no ubuntu@141.148.162.114 << 'EOF' # Remove containers bcards-infrastructure se existirem docker stop bcards-infrastructure || true docker rm bcards-infrastructure || true docker stop bcards-test-app || true docker rm bcards-test-app || true # Para o container BCards atual se existir docker stop bcards-prod || true docker rm bcards-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 BCards docker run -d \ --name bcards-prod \ --restart unless-stopped \ -p 5002:8080 \ -e ASPNETCORE_ENVIRONMENT=Production \ -e ASPNETCORE_URLS=http://+:8080 \ ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest # Recarrega NGINX sudo nginx -t && sudo systemctl reload nginx EOF # Deploy no Servidor 2 (ARM - OCI) ssh -o StrictHostKeyChecking=no ubuntu@129.146.116.218 << 'EOF' # Remove containers bcards-infrastructure se existirem docker stop bcards-infrastructure || true docker rm bcards-infrastructure || true docker stop bcards-test-app || true docker rm bcards-test-app || true # Para o container BCards atual se existir docker stop bcards-prod || true docker rm bcards-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 BCards docker run -d \ --name bcards-prod \ --restart unless-stopped \ -p 5002:8080 \ -e ASPNETCORE_ENVIRONMENT=Production \ -e ASPNETCORE_URLS=http://+:8080 \ ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest EOF - name: Health Check Production run: | # Aguarda containers subirem sleep 30 # Verifica se os serviços estão respondendo echo "Verificando Servidor 1 (ARM)..." ssh -o StrictHostKeyChecking=no ubuntu@141.148.162.114 'curl -f http://localhost:5002/health || echo "Servidor 1 pode não estar respondendo"' echo "Verificando Servidor 2 (ARM)..." ssh -o StrictHostKeyChecking=no ubuntu@129.146.116.218 'curl -f http://localhost:5002/health || echo "Servidor 2 pode não estar respondendo"' deploy-staging: needs: build-and-push runs-on: ubuntu-latest if: startsWith(github.ref, 'refs/heads/release/') steps: - name: Deploy to Staging Server (x86 - Local) run: | # Configura SSH mkdir -p ~/.ssh echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa chmod 600 ~/.ssh/id_rsa # Extrai versão da branch VERSION=$(echo "${{ github.ref }}" | sed 's/refs\/heads\/release\///') # Deploy no Servidor Local x86 ssh -o StrictHostKeyChecking=no ubuntu@192.168.0.100 << EOF # Remove containers bcards-infrastructure se existirem docker stop bcards-infrastructure || true docker rm bcards-infrastructure || true docker stop bcards-test-app || true docker rm bcards-test-app || true # Para o container BCards atual se existir docker stop bcards-staging || true docker rm bcards-staging || true # Remove imagem antiga docker rmi ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${VERSION} || true # Puxa nova imagem docker pull ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${VERSION} # Executa novo container BCards docker run -d \ --name bcards-staging \ --restart unless-stopped \ -p 5002:8080 \ -e ASPNETCORE_ENVIRONMENT=Staging \ -e ASPNETCORE_URLS=http://+:8080 \ ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${VERSION} EOF - name: Health Check Staging run: | # Aguarda container subir sleep 30 # Verifica se o serviço está respondendo echo "Verificando Servidor Staging (x86)..." ssh -o StrictHostKeyChecking=no ubuntu@192.168.0.100 'curl -f http://localhost:5002/health || echo "Servidor staging pode não estar respondendo"' cleanup: needs: [deploy-production, deploy-staging] runs-on: ubuntu-latest if: always() && (github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/heads/release/')) steps: - name: Cleanup old containers and images run: | # Configura SSH mkdir -p ~/.ssh echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa chmod 600 ~/.ssh/id_rsa # Lista de servidores baseada na branch if [ "${{ github.ref }}" = "refs/heads/main" ]; then SERVERS=("141.148.162.114" "129.146.116.218") else SERVERS=("192.168.0.100") fi # Limpeza em cada servidor for server in "${SERVERS[@]}"; do echo "Limpando servidor $server..." ssh -o StrictHostKeyChecking=no ubuntu@$server << 'EOF' # Remove containers parados docker container prune -f # Remove imagens não utilizadas (mantém as mais recentes) docker image prune -f # Remove redes não utilizadas docker network prune -f EOF done