QrRapido/.github/workflows/deploy.yml
Ricardo Carneiro afd1b53354
Some checks failed
Deploy QR Rapido / test (push) Successful in 1m5s
Deploy QR Rapido / build-and-push (push) Failing after 1m20s
Deploy QR Rapido / deploy-staging (push) Has been skipped
Deploy QR Rapido / deploy-production (push) Has been skipped
feat: deploy to production
2025-08-05 21:13:21 -03:00

203 lines
6.2 KiB
YAML

name: Deploy QR Rapido
on:
push:
branches: [ main, develop ]
pull_request:
branches: [ main ]
env:
REGISTRY: registry.redecarneir.us
IMAGE_NAME: qrrapido
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' || github.ref == 'refs/heads/develop'
steps:
- name: Checkout
uses: actions/checkout@v4
- 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
# 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
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/develop'
steps:
- 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
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main'
environment: production
steps:
- 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
# 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"'