199 lines
6.7 KiB
YAML
199 lines
6.7 KiB
YAML
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 "</dev/tcp/192.168.0.100/27017" && echo "MongoDB connection successful" || echo "MongoDB connection failed"
|
|
|
|
# Job para mostrar quando os testes foram pulados
|
|
test-skipped:
|
|
name: Tests Skipped
|
|
runs-on: ubuntu-latest
|
|
# Só executa se SKIP_TESTS for 'true'
|
|
if: ${{ vars.SKIP_TESTS == 'true' }}
|
|
|
|
steps:
|
|
- name: Show skip message
|
|
run: |
|
|
echo "⚠️ SKIP_TESTS = ${{ vars.SKIP_TESTS }}"
|
|
echo "⏭️ Testes foram PULADOS por configuração"
|
|
echo "🔧 Para ativar os testes, defina SKIP_TESTS=false ou remova a variável"
|
|
|
|
build-and-deploy:
|
|
name: Build Multi-Arch Image and Deploy
|
|
runs-on: ubuntu-latest
|
|
# Depende de qualquer um dos jobs de teste (executado ou pulado)
|
|
needs: [test, test-skipped]
|
|
# Só executa se pelo menos um dos jobs anteriores foi bem-sucedido ou pulado
|
|
if: always() && (needs.test.result == 'success' || needs.test.result == 'skipped' || needs.test-skipped.result == 'success')
|
|
|
|
steps:
|
|
- name: Show deployment status
|
|
run: |
|
|
echo "🚀 Iniciando deployment..."
|
|
echo "📊 Test job result: ${{ needs.test.result }}"
|
|
echo "📊 Test-skipped job result: ${{ needs.test-skipped.result }}"
|
|
|
|
- name: Checkout code
|
|
uses: actions/checkout@v4
|
|
|
|
- name: Set up Docker Buildx
|
|
uses: docker/setup-buildx-action@v3
|
|
with:
|
|
driver-opts: network=host
|
|
|
|
- name: Extract branch name and version
|
|
id: extract_branch
|
|
run: |
|
|
BRANCH_NAME=${GITHUB_REF#refs/heads/}
|
|
VERSION=${BRANCH_NAME#Release/}
|
|
COMMIT_SHA=${GITHUB_SHA::7}
|
|
echo "branch=$BRANCH_NAME" >> $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 <<EOF
|
|
IMAGE_TAG=${{ steps.extract_branch.outputs.tag }}
|
|
MONGODB_CONNECTION_STRING=mongodb://$MONGODB_HOST/BCardsDB
|
|
ASPNETCORE_ENVIRONMENT=Release
|
|
EOF
|
|
|
|
# Run deployment script
|
|
chmod +x scripts/deploy-release.sh
|
|
sudo ./scripts/deploy-release.sh ${{ steps.extract_branch.outputs.tag }}
|
|
|
|
- name: Health check and validation
|
|
run: |
|
|
echo "Running health checks..."
|
|
|
|
# Wait for application to start
|
|
sleep 30
|
|
|
|
# Test application health
|
|
for i in {1..10}; do
|
|
if curl -f http://localhost:8090/health > /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}" |