name: Release Deployment Pipeline on: push: branches: - 'Release/*' env: REGISTRY: registry.redecarneir.us IMAGE_NAME: bcards MONGODB_HOST: 192.168.0.100:27017 # Variável para controlar execução dos testes SKIP_TESTS: ${{ vars.SKIP_TESTS || 'false' }} jobs: test: name: Run Tests runs-on: ubuntu-latest # Só executa se SKIP_TESTS não for 'true' if: ${{ vars.SKIP_TESTS != 'true' }} steps: - name: Show test status run: | echo "🧪 SKIP_TESTS = ${{ vars.SKIP_TESTS }}" echo "✅ Executando testes porque SKIP_TESTS != 'true'" - name: Checkout code uses: actions/checkout@v4 - name: Setup .NET 8 uses: actions/setup-dotnet@v4 with: dotnet-version: '8.0.x' - name: Restore dependencies run: dotnet restore - name: Build solution run: dotnet build --no-restore --configuration Release - name: Run unit tests run: dotnet test --no-build --configuration Release --verbosity normal --collect:"XPlat Code Coverage" - name: Test MongoDB connection run: | echo "Testing MongoDB connection to $MONGODB_HOST" timeout 10 bash -c "> $GITHUB_OUTPUT echo "version=$VERSION" >> $GITHUB_OUTPUT echo "commit=$COMMIT_SHA" >> $GITHUB_OUTPUT echo "tag=$VERSION-$COMMIT_SHA" >> $GITHUB_OUTPUT - name: Build and push multi-arch Docker image run: | echo "Building multi-arch image for platforms: linux/amd64,linux/arm64" docker buildx build \ --platform linux/amd64,linux/arm64 \ --file Dockerfile.release \ --tag $REGISTRY/$IMAGE_NAME:${{ steps.extract_branch.outputs.tag }} \ --tag $REGISTRY/$IMAGE_NAME:${{ steps.extract_branch.outputs.version }}-latest \ --tag $REGISTRY/$IMAGE_NAME:release-latest \ --push \ --build-arg BUILDPLATFORM=linux/amd64 \ --build-arg VERSION=${{ steps.extract_branch.outputs.version }} \ --build-arg COMMIT=${{ steps.extract_branch.outputs.commit }} \ . - name: Deploy to staging environment run: | echo "Deploying to staging environment..." # Create deployment directory sudo mkdir -p /opt/bcards-staging # Copy docker-compose file sudo cp docker-compose.staging.yml /opt/bcards-staging/ # Set environment variables sudo tee /opt/bcards-staging/.env > /dev/null < /dev/null 2>&1; then echo "Application health check passed" break fi echo "Health check attempt $i failed, retrying in 10 seconds..." sleep 10 if [ $i -eq 10 ]; then echo "Health check failed after 10 attempts" exit 1 fi done # Test MongoDB connectivity from application chmod +x scripts/test-mongodb-connection.sh ./scripts/test-mongodb-connection.sh - name: Deployment notification run: | echo "🚀 Deployment Status: SUCCESS" echo "📦 Image: $REGISTRY/$IMAGE_NAME:${{ steps.extract_branch.outputs.tag }}" echo "🌐 Environment: Staging" echo "🔗 MongoDB: $MONGODB_HOST" echo "🏗️ Architecture: Multi-arch (linux/amd64, linux/arm64)" echo "📋 Branch: ${{ steps.extract_branch.outputs.branch }}" echo "🆔 Commit: ${{ steps.extract_branch.outputs.commit }}" echo "🧪 Tests: ${{ vars.SKIP_TESTS == 'true' && 'SKIPPED' || 'PASSED' }}" rollback: name: Rollback on Failure runs-on: ubuntu-latest needs: [test, build-and-deploy] if: failure() && needs.test.result != 'skipped' steps: - name: Rollback deployment run: | echo "🚨 Deployment failed, initiating rollback..." # Stop current containers cd /opt/bcards-staging sudo docker-compose down # Restore previous version if exists if [ -f .env.backup ]; then sudo mv .env.backup .env sudo docker-compose up -d echo "✅ Rollback completed to previous version" else echo "❌ No previous version found for rollback" fi - name: Failure notification run: | echo "❌ Deployment Status: FAILED" echo "🔄 Rollback: Initiated" echo "📋 Branch: ${GITHUB_REF#refs/heads/}" echo "🆔 Commit: ${GITHUB_SHA::7}"