From cbda3db3f31fb06b8f2f2248c7610c709ff5e1d5 Mon Sep 17 00:00:00 2001 From: Ricardo Carneiro <71648276+ricarneiro@users.noreply.github.com> Date: Mon, 2 Jun 2025 21:41:13 -0300 Subject: [PATCH] feat: pipeline --- .dockerignore | 30 +++++ .gitea/workflows/deploy.yml | 106 ++++++++++++++++++ OnlyOneAccessTemplate.sln | 7 +- OnlyOneAccessTemplate/Dockerfile | 30 +++++ .../OnlyOneAccessTemplate.csproj | 3 + .../Properties/launchSettings.json | 67 ++++++----- 6 files changed, 213 insertions(+), 30 deletions(-) create mode 100644 .dockerignore create mode 100644 .gitea/workflows/deploy.yml create mode 100644 OnlyOneAccessTemplate/Dockerfile diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..fe1152b --- /dev/null +++ b/.dockerignore @@ -0,0 +1,30 @@ +**/.classpath +**/.dockerignore +**/.env +**/.git +**/.gitignore +**/.project +**/.settings +**/.toolstarget +**/.vs +**/.vscode +**/*.*proj.user +**/*.dbmdl +**/*.jfm +**/azds.yaml +**/bin +**/charts +**/docker-compose* +**/Dockerfile* +**/node_modules +**/npm-debug.log +**/obj +**/secrets.dev.yaml +**/values.dev.yaml +LICENSE +README.md +!**/.gitignore +!.git/HEAD +!.git/config +!.git/packed-refs +!.git/refs/heads/** \ No newline at end of file diff --git a/.gitea/workflows/deploy.yml b/.gitea/workflows/deploy.yml new file mode 100644 index 0000000..7fddca0 --- /dev/null +++ b/.gitea/workflows/deploy.yml @@ -0,0 +1,106 @@ +name: Deploy ASP.NET MVC to OCI + +on: + push: + branches: [ main, master ] + 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: Login to Docker Registry + uses: docker/login-action@v3 + with: + registry: registry.redecarneir.us + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + + - name: Build and push Docker image + uses: docker/build-push-action@v5 + with: + context: . + platforms: linux/arm64 + push: true + tags: | + registry.redecarneir.us/onlyoneaccesstemplate:latest + registry.redecarneir.us/onlyoneaccesstemplate:${{ github.sha }} + cache-from: type=gha + cache-to: type=gha,mode=max + + - name: Deploy to OCI Server + uses: appleboy/ssh-action@v1.0.3 + with: + host: 129.146.116.218 + username: ${{ secrets.SSH_USERNAME }} + key: ${{ secrets.SSH_PRIVATE_KEY }} + port: 22 + script: | + # Login no registry Docker + echo "${{ secrets.DOCKER_PASSWORD }}" | docker login registry.redecarneir.us -u "${{ secrets.DOCKER_USERNAME }}" --password-stdin + + # Para qualquer container que esteja usando a porta 80 + echo "=== Verificando containers na porta 80 ===" + CONTAINERS_PORT_80=$(docker ps --format "table {{.ID}}\t{{.Names}}\t{{.Ports}}" | grep ':80->' | awk '{print $2}' || true) + if [ ! -z "$CONTAINERS_PORT_80" ]; then + echo "Parando containers na porta 80: $CONTAINERS_PORT_80" + echo "$CONTAINERS_PORT_80" | xargs -r docker stop + echo "$CONTAINERS_PORT_80" | xargs -r docker rm + else + echo "Nenhum container encontrado na porta 80" + fi + + # Para o container anterior da aplicação (se existir) + docker stop onlyoneaccesstemplate || true + docker rm onlyoneaccesstemplate || true + + # Remove imagem antiga + docker rmi registry.redecarneir.us/onlyoneaccesstemplate:latest || true + + # Puxa nova imagem + docker pull registry.redecarneir.us/onlyoneaccesstemplate:latest + + # Executa o novo container na porta 80 + docker run -d \ + --name onlyoneaccesstemplate \ + --restart unless-stopped \ + -p 80:8080 \ + -p 443:8081 \ + -e ASPNETCORE_ENVIRONMENT=Production \ + -e ASPNETCORE_URLS="http://+:8080;https://+:8081" \ + registry.redecarneir.us/onlyoneaccesstemplate:latest + + # Limpa imagens não utilizadas + docker image prune -f + + # Verifica se o container está rodando + docker ps | grep onlyoneaccesstemplate + + # Testa se a aplicação está respondendo na porta 80 + sleep 10 + curl -f http://localhost:80 || echo "Aplicação pode estar inicializando..." + + - name: Verify deployment + uses: appleboy/ssh-action@v1.0.3 + with: + host: 129.146.116.218 + username: ${{ secrets.SSH_USERNAME }} + key: ${{ secrets.SSH_PRIVATE_KEY }} + port: 22 + script: | + echo "=== Status dos containers ===" + docker ps -a | grep onlyoneaccesstemplate + + echo "=== Logs da aplicação (últimas 20 linhas) ===" + docker logs onlyoneaccesstemplate --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 diff --git a/OnlyOneAccessTemplate.sln b/OnlyOneAccessTemplate.sln index 20f4619..b980e6a 100644 --- a/OnlyOneAccessTemplate.sln +++ b/OnlyOneAccessTemplate.sln @@ -3,7 +3,12 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.10.35122.118 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OnlyOneAccessTemplate", "OnlyOneAccessTemplate\OnlyOneAccessTemplate.csproj", "{2A672B8D-D16E-452E-A975-A3E19625453B}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OnlyOneAccessTemplate", "OnlyOneAccessTemplate\OnlyOneAccessTemplate.csproj", "{2A672B8D-D16E-452E-A975-A3E19625453B}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "pipeline", "pipeline", "{21A583BA-1BDC-42C4-BE42-DD13267BDA20}" + ProjectSection(SolutionItems) = preProject + .gitea\workflows\deploy.yml = .gitea\workflows\deploy.yml + EndProjectSection EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/OnlyOneAccessTemplate/Dockerfile b/OnlyOneAccessTemplate/Dockerfile new file mode 100644 index 0000000..35c49c4 --- /dev/null +++ b/OnlyOneAccessTemplate/Dockerfile @@ -0,0 +1,30 @@ +# See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging. + +FROM --platform=linux/arm64 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 +ARG BUILD_CONFIGURATION=Release +WORKDIR /src +COPY ["OnlyOneAccessTemplate/OnlyOneAccessTemplate.csproj", "OnlyOneAccessTemplate/"] +RUN dotnet restore "./OnlyOneAccessTemplate/OnlyOneAccessTemplate.csproj" +COPY . . +WORKDIR "/src/OnlyOneAccessTemplate" +RUN dotnet build "./OnlyOneAccessTemplate.csproj" -c $BUILD_CONFIGURATION -o /app/build + +FROM build AS publish +ARG BUILD_CONFIGURATION=Release +RUN dotnet publish "./OnlyOneAccessTemplate.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false + +FROM base AS final +WORKDIR /app +COPY --from=publish /app/publish . + +# Healthcheck (adicional - opcional mas recomendado) +HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \ + CMD curl -f http://localhost:8080/health || exit 1 + +ENTRYPOINT ["dotnet", "OnlyOneAccessTemplate.dll"] \ No newline at end of file diff --git a/OnlyOneAccessTemplate/OnlyOneAccessTemplate.csproj b/OnlyOneAccessTemplate/OnlyOneAccessTemplate.csproj index 199a6a1..6f60062 100644 --- a/OnlyOneAccessTemplate/OnlyOneAccessTemplate.csproj +++ b/OnlyOneAccessTemplate/OnlyOneAccessTemplate.csproj @@ -4,6 +4,8 @@ net8.0 enable enable + c25bc394-1044-45e1-85e6-111f7f46b232 + Linux @@ -16,6 +18,7 @@ + diff --git a/OnlyOneAccessTemplate/Properties/launchSettings.json b/OnlyOneAccessTemplate/Properties/launchSettings.json index e1f135f..cf711f3 100644 --- a/OnlyOneAccessTemplate/Properties/launchSettings.json +++ b/OnlyOneAccessTemplate/Properties/launchSettings.json @@ -1,4 +1,40 @@ -{ +{ + "profiles": { + "http": { + "commandName": "Project", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + }, + "dotnetRunMessages": true, + "applicationUrl": "http://localhost:5201" + }, + "https": { + "commandName": "Project", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + }, + "dotnetRunMessages": true, + "applicationUrl": "https://localhost:7299;http://localhost:5201" + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "Container (Dockerfile)": { + "commandName": "Docker", + "launchBrowser": true, + "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}", + "environmentVariables": { + "ASPNETCORE_HTTP_PORTS": "8080" + }, + "publishAllPorts": true + } + }, "$schema": "http://json.schemastore.org/launchsettings.json", "iisSettings": { "windowsAuthentication": false, @@ -7,32 +43,5 @@ "applicationUrl": "http://localhost:65221", "sslPort": 0 } - }, - "profiles": { - "http": { - "commandName": "Project", - "dotnetRunMessages": true, - "launchBrowser": true, - "applicationUrl": "http://localhost:5201", - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" - } - }, - "https": { - "commandName": "Project", - "dotnetRunMessages": true, - "launchBrowser": true, - "applicationUrl": "https://localhost:7299;http://localhost:5201", - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" - } - }, - "IIS Express": { - "commandName": "IISExpress", - "launchBrowser": true, - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" - } - } } -} +} \ No newline at end of file