From a530a40b4b84bd0756ab1d6c7fa34ff63f4af74b Mon Sep 17 00:00:00 2001 From: Ricardo Carneiro Date: Sat, 13 Sep 2025 23:47:20 -0300 Subject: [PATCH] fix: ajustes de build --- .dockerignore | 16 +++- .gitea/workflows/deploy.yml | 149 ++++++++++++++++++++++++++++-------- Dockerfile | 33 ++++---- 3 files changed, 147 insertions(+), 51 deletions(-) diff --git a/.dockerignore b/.dockerignore index f3254c2..4d41520 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,13 +1,27 @@ -bin/ +bin/ obj/ .git/ +.gitea/ .gitignore *.md README.md +CLAUDE.md tests/ docs/ .vs/ .vscode/ **/.DS_Store **/Thumbs.db +**/node_modules/ +**/*.log +**/*.tmp +**/temp/ +**/*.bak +Properties/launchSettings.json +*.user +*.suo +*.cache +.env +.env.* +!.env.example diff --git a/.gitea/workflows/deploy.yml b/.gitea/workflows/deploy.yml index e14f5d3..a0ae256 100644 --- a/.gitea/workflows/deploy.yml +++ b/.gitea/workflows/deploy.yml @@ -1,64 +1,78 @@ -name: Deploy ASP.NET MVC to OCI +name: Deploy ASP.NET MVC to OCI on: push: - branches: [ main, master ] + branches: [ main, master, 'release/*' ] pull_request: branches: [ main, master ] jobs: build-and-deploy: runs-on: ubuntu-latest - + steps: - name: Checkout code uses: actions/checkout@v4 - + - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 + - name: Set build variables + id: vars + run: | + if [[ ${{ github.ref }} == refs/heads/main ]] || [[ ${{ github.ref }} == refs/heads/master ]]; then + echo "PLATFORM=linux/arm64" >> $GITHUB_OUTPUT + echo "TARGET_ARCH=arm64" >> $GITHUB_OUTPUT + echo "TAG_SUFFIX=" >> $GITHUB_OUTPUT + echo "DEPLOY_ENV=production" >> $GITHUB_OUTPUT + elif [[ ${{ github.ref }} == refs/heads/release/* ]]; then + echo "PLATFORM=linux/amd64" >> $GITHUB_OUTPUT + echo "TARGET_ARCH=amd64" >> $GITHUB_OUTPUT + echo "TAG_SUFFIX=-staging" >> $GITHUB_OUTPUT + echo "DEPLOY_ENV=staging" >> $GITHUB_OUTPUT + else + echo "PLATFORM=linux/amd64" >> $GITHUB_OUTPUT + echo "TARGET_ARCH=amd64" >> $GITHUB_OUTPUT + echo "TAG_SUFFIX=-dev" >> $GITHUB_OUTPUT + echo "DEPLOY_ENV=development" >> $GITHUB_OUTPUT + fi + - name: Cache Docker layers - uses: actions/cache@v3 + uses: actions/cache@v4 with: path: /tmp/.buildx-cache - key: ${{ runner.os }}-buildx-${{ github.sha }} + key: ${{ runner.os }}-buildx-${{ steps.vars.outputs.TARGET_ARCH }}-${{ github.sha }} restore-keys: | + ${{ runner.os }}-buildx-${{ steps.vars.outputs.TARGET_ARCH }}- ${{ runner.os }}-buildx- - - name: Debug - List files - run: | - echo "=== Arquivos na raiz ===" - ls -la - echo "=== Procurando Dockerfile ===" - find . -name "Dockerfile" -o -name "dockerfile" -type f - echo "=== Estrutura do projeto ===" - tree -L 3 || find . -type d -name "convertit" - - name: Build and push Docker image uses: docker/build-push-action@v5 with: context: . file: ./Dockerfile - platforms: linux/arm64 + platforms: ${{ steps.vars.outputs.PLATFORM }} push: true tags: | - registry.redecarneir.us/convertit:latest - registry.redecarneir.us/convertit:${{ github.sha }} + registry.redecarneir.us/convertit:latest${{ steps.vars.outputs.TAG_SUFFIX }} + registry.redecarneir.us/convertit:${{ github.sha }}${{ steps.vars.outputs.TAG_SUFFIX }} cache-from: | type=local,src=/tmp/.buildx-cache - type=registry,ref=registry.redecarneir.us/convertit:cache + type=registry,ref=registry.redecarneir.us/convertit:cache-${{ steps.vars.outputs.TARGET_ARCH }} cache-to: | type=local,dest=/tmp/.buildx-cache-new,mode=max - type=registry,ref=registry.redecarneir.us/convertit:cache,mode=max + type=registry,ref=registry.redecarneir.us/convertit:cache-${{ steps.vars.outputs.TARGET_ARCH }},mode=max build-args: | BUILDKIT_INLINE_CACHE=1 - + TARGETARCH=${{ steps.vars.outputs.TARGET_ARCH }} + - name: Move cache run: | rm -rf /tmp/.buildx-cache mv /tmp/.buildx-cache-new /tmp/.buildx-cache - - - name: Deploy to OCI Server + + - name: Deploy to Production (ARM64) + if: github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master' uses: appleboy/ssh-action@v1.0.3 with: host: 129.146.116.218 @@ -70,13 +84,13 @@ jobs: # Para o container anterior da aplicação (se existir) docker stop convertit || true docker rm convertit || true - + # Remove imagem antiga docker rmi registry.redecarneir.us/convertit:latest || true - - # Puxa nova imagem + + # Puxa nova imagem ARM64 docker pull registry.redecarneir.us/convertit:latest - + # Executa o novo container na porta 80 docker run -d \ --name convertit \ @@ -98,18 +112,69 @@ jobs: -e DOTNET_EnableDiagnostics=0 \ -e DOTNET_RUNNING_IN_CONTAINER=true \ registry.redecarneir.us/convertit:latest - + # Limpa imagens não utilizadas docker image prune -f - + # Verifica se o container está rodando docker ps | grep convertit - + # Testa se a aplicação está respondendo na porta 80 sleep 10 curl -f http://localhost:80 || echo "Aplicação pode estar inicializando..." + - name: Deploy to Staging (x86_64) + if: startsWith(github.ref, 'refs/heads/release/') + uses: appleboy/ssh-action@v1.0.3 + with: + host: ${{ secrets.STAGING_HOST }} + username: ${{ secrets.STAGING_SSH_USERNAME }} + key: ${{ secrets.STAGING_SSH_PRIVATE_KEY }} + port: 22 + timeout: 60s + script: | + # Para o container anterior da aplicação staging (se existir) + docker stop convertit-staging || true + docker rm convertit-staging || true + + # Remove imagem antiga + docker rmi registry.redecarneir.us/convertit:latest-staging || true + + # Puxa nova imagem x86_64 + docker pull registry.redecarneir.us/convertit:latest-staging + + # Executa o novo container staging na porta 8080 + docker run -d \ + --name convertit-staging \ + --restart unless-stopped \ + -p 8080:8080 \ + --memory=1g \ + --cpus=1.0 \ + --health-cmd="curl -f http://localhost:8080/health || exit 1" \ + --health-interval=30s \ + --health-timeout=10s \ + --health-retries=3 \ + --health-start-period=60s \ + -e ASPNETCORE_ENVIRONMENT=Staging \ + -e ASPNETCORE_URLS="http://+:8080" \ + -e DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=false \ + -e DOTNET_USE_POLLING_FILE_WATCHER=true \ + -e DOTNET_EnableDiagnostics=0 \ + -e DOTNET_RUNNING_IN_CONTAINER=true \ + registry.redecarneir.us/convertit:latest-staging + + # Limpa imagens não utilizadas + docker image prune -f + + # Verifica se o container está rodando + docker ps | grep convertit-staging + + # Testa se a aplicação está respondendo na porta 8080 + sleep 10 + curl -f http://localhost:8080 || echo "Aplicação staging pode estar inicializando..." + - name: Verify deployment + if: github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master' uses: appleboy/ssh-action@v1.0.3 with: host: 129.146.116.218 @@ -119,9 +184,27 @@ jobs: script: | echo "=== Status dos containers ===" docker ps -a | grep convertit - + echo "=== Logs da aplicação (últimas 20 linhas) ===" docker logs convertit --tail 20 - + echo "=== Teste de conectividade ===" - curl -I http://localhost:80 || echo "Aplicação ainda não está acessível" \ No newline at end of file + curl -I http://localhost:80 || echo "Aplicação ainda não está acessível" + + - name: Verify staging deployment + if: startsWith(github.ref, 'refs/heads/release/') + uses: appleboy/ssh-action@v1.0.3 + with: + host: ${{ secrets.STAGING_HOST }} + username: ${{ secrets.STAGING_SSH_USERNAME }} + key: ${{ secrets.STAGING_SSH_PRIVATE_KEY }} + port: 22 + script: | + echo "=== Status dos containers staging ===" + docker ps -a | grep convertit-staging + + echo "=== Logs da aplicação staging (últimas 20 linhas) ===" + docker logs convertit-staging --tail 20 + + echo "=== Teste de conectividade staging ===" + curl -I http://localhost:8080 || echo "Aplicação staging ainda não está acessível" \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 82dd465..5df3d9e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,51 +1,50 @@ -# Dockerfile self-contained para m�xima performance -FROM --platform=linux/arm64 mcr.microsoft.com/dotnet/aspnet:8.0 AS base +# Dockerfile otimizado para multi-arquitetura +ARG TARGETARCH=arm64 +FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base USER app WORKDIR /app EXPOSE 8080 EXPOSE 8081 -FROM --platform=linux/arm64 mcr.microsoft.com/dotnet/sdk:8.0 AS build +FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build ARG BUILD_CONFIGURATION=Release +ARG TARGETARCH WORKDIR /src # Copiar apenas arquivos de projeto primeiro (melhor cache) COPY ["Convert-It.csproj", "./"] -# Restore com configura��es otimizadas para ARM64 +# Restore com cache habilitado RUN dotnet restore "./Convert-It.csproj" \ - --runtime linux-arm64 \ - --no-cache + --runtime linux-${TARGETARCH} -# Copiar c�digo fonte +# Copiar código fonte COPY . . WORKDIR "/src" -# Build otimizado +# Build otimizado framework-dependent (mais rápido) RUN dotnet build "./Convert-It.csproj" \ -c $BUILD_CONFIGURATION \ -o /app/build \ - --runtime linux-arm64 \ --no-restore FROM build AS publish ARG BUILD_CONFIGURATION=Release +ARG TARGETARCH -# Publish self-contained para eliminar cold start +# Publish framework-dependent (mais rápido que self-contained) RUN dotnet publish "./Convert-It.csproj" \ -c $BUILD_CONFIGURATION \ -o /app/publish \ - --runtime linux-arm64 \ + --runtime linux-${TARGETARCH} \ --no-restore \ - --self-contained true \ - /p:PublishTrimmed=true \ - /p:PublishSingleFile=false + --self-contained false -FROM mcr.microsoft.com/dotnet/runtime-deps:8.0 AS final +FROM base AS final WORKDIR /app COPY --from=publish /app/publish . -# Vari�veis de ambiente otimizadas para produ��o +# Variáveis de ambiente otimizadas para produção ENV DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=false ENV DOTNET_USE_POLLING_FILE_WATCHER=true ENV ASPNETCORE_ENVIRONMENT=Production @@ -55,4 +54,4 @@ ENV DOTNET_EnableDiagnostics=0 HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \ CMD curl -f http://localhost:8080/health || exit 1 -ENTRYPOINT ["./Convert-It"] \ No newline at end of file +ENTRYPOINT ["dotnet", "Convert-It.dll"] \ No newline at end of file