192 lines
6.0 KiB
JSON
192 lines
6.0 KiB
JSON
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 para ARM64 (servidores Ampere OCI)
|
|
docker buildx build \
|
|
--platform linux/arm64 \
|
|
--tag ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:$TAG \
|
|
--push \
|
|
.
|
|
|
|
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: |
|
|
# Deploy no Servidor 1
|
|
ssh -o StrictHostKeyChecking=no 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 -o StrictHostKeyChecking=no 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: |
|
|
# Deploy no Servidor 1 (com NGINX)
|
|
ssh -o StrictHostKeyChecking=no 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 -o StrictHostKeyChecking=no 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 -o StrictHostKeyChecking=no ubuntu@141.148.162.114 'curl -f http://localhost:5001/health || echo "Servidor 1 pode não estar respondendo"'
|
|
|
|
echo "Verificando Servidor 2..."
|
|
ssh -o StrictHostKeyChecking=no ubuntu@129.146.116.218 'curl -f http://localhost:5001/health || echo "Servidor 2 pode não estar respondendo"' |