feat: deploy to production
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

This commit is contained in:
Ricardo Carneiro 2025-08-05 21:13:21 -03:00
parent c2c89af91b
commit afd1b53354
2 changed files with 140 additions and 54 deletions

View File

@ -1,5 +1,4 @@
name: Deploy QR Rapido name: Deploy QR Rapido
on: on:
push: push:
branches: [ main, develop ] branches: [ main, develop ]
@ -7,8 +6,8 @@ on:
branches: [ main ] branches: [ main ]
env: env:
REGISTRY: ghcr.io REGISTRY: registry.redecarneir.us
IMAGE_NAME: ${{ github.repository }} IMAGE_NAME: qrrapido
jobs: jobs:
test: test:
@ -47,43 +46,31 @@ jobs:
build-and-push: build-and-push:
needs: test needs: test
runs-on: ubuntu-latest runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main' if: github.ref == 'refs/heads/main' || github.ref == 'refs/heads/develop'
permissions:
contents: read
packages: write
steps: steps:
- name: Checkout - name: Checkout
uses: actions/checkout@v4 uses: actions/checkout@v4
- name: Login to Container Registry - name: Set up Docker Buildx
uses: docker/login-action@v3 uses: docker/setup-buildx-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata - name: Build and push to registry
id: meta run: |
uses: docker/metadata-action@v5 # Determina a tag baseada na branch
with: if [ "${{ github.ref }}" = "refs/heads/main" ]; then
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} TAG="latest"
tags: | else
type=ref,event=branch TAG="develop"
type=ref,event=pr fi
type=sha,prefix={{branch}}-
type=raw,value=latest,enable={{is_default_branch}}
- name: Build and push # Build da imagem
uses: docker/build-push-action@v5 docker build -t ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:$TAG .
with:
context: . # Push para o registry (sem autenticação conforme mencionado)
push: true docker push ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:$TAG
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }} echo "IMAGE_TAG=$TAG" >> $GITHUB_ENV
cache-from: type=gha
cache-to: type=gha,mode=max
deploy-staging: deploy-staging:
needs: build-and-push needs: build-and-push
@ -91,12 +78,54 @@ jobs:
if: github.ref == 'refs/heads/develop' if: github.ref == 'refs/heads/develop'
steps: steps:
- name: Deploy to Staging - name: Deploy to Staging Servers
uses: azure/webapps-deploy@v2 run: |
with: # Configura SSH known_hosts
app-name: 'qrrapido-staging' mkdir -p ~/.ssh
publish-profile: ${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE_STAGING }} ssh-keyscan -H 141.148.162.114 >> ~/.ssh/known_hosts
images: '${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:develop' 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: deploy-production:
needs: build-and-push needs: build-and-push
@ -105,17 +134,69 @@ jobs:
environment: production environment: production
steps: steps:
- name: Deploy to Production - name: Deploy to Production Servers
uses: azure/webapps-deploy@v2 run: |
with: # Configura SSH known_hosts
app-name: 'qrrapido-production' mkdir -p ~/.ssh
publish-profile: ${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE_PROD }} ssh-keyscan -H 141.148.162.114 >> ~/.ssh/known_hosts
images: '${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest' 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"'
- name: Notify Slack
uses: 8398a7/action-slack@v3
with:
status: ${{ job.status }}
channel: '#deployments'
webhook_url: ${{ secrets.SLACK_WEBHOOK }}
text: "QR Rapido deployed to production! 🚀"

View File

@ -1,10 +1,15 @@
 
Microsoft Visual Studio Solution File, Format Version 12.00 Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17 # Visual Studio Version 17
VisualStudioVersion = 17.13.35818.85 d17.13 VisualStudioVersion = 17.13.35818.85
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QRRapidoApp", "QRRapidoApp.csproj", "{8AF92774-40E8-830E-08B3-67F0A0B91DDE}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QRRapidoApp", "QRRapidoApp.csproj", "{8AF92774-40E8-830E-08B3-67F0A0B91DDE}"
EndProject EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "RootFolder", "RootFolder", "{02EA681E-C7D8-13C7-8484-4AC65E1B71E8}"
ProjectSection(SolutionItems) = preProject
.github\workflows\deploy.yml = .github\workflows\deploy.yml
EndProjectSection
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU