Artigos

DevOps

Pipeline CI/CD para .NET que realmente funciona

Um template de GitHub Actions para projetos .NET com testes, analise de codigo, e deploy automatico. Copie e adapte.

·8 min de leitura

O que um bom pipeline precisa ter?

  1. Rapido o suficiente para nao atrapalhar
  2. Confiavel o suficiente para confiar
  3. Informativo o suficiente para debugar

O template

name: CI/CD Pipeline

on:
  push:
    branches: [main, develop]
  pull_request:
    branches: [main]

env:
  DOTNET_VERSION: '8.0.x'

jobs:
  build-and-test:
    runs-on: ubuntu-latest
    
    steps:
    - uses: actions/checkout@v4
    
    - name: Setup .NET
      uses: actions/setup-dotnet@v4
      with:
        dotnet-version: ${{ env.DOTNET_VERSION }}
    
    - name: Restore
      run: dotnet restore
    
    - name: Build
      run: dotnet build --no-restore --configuration Release
    
    - name: Test
      run: dotnet test --no-build --configuration Release --collect:"XPlat Code Coverage"
    
    - name: Upload coverage
      uses: codecov/codecov-action@v4
      with:
        token: ${{ secrets.CODECOV_TOKEN }}

  analyze:
    runs-on: ubuntu-latest
    needs: build-and-test
    
    steps:
    - uses: actions/checkout@v4
    
    - name: SonarCloud Scan
      uses: SonarSource/sonarcloud-github-action@master
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}

  deploy:
    if: github.ref == 'refs/heads/main'
    runs-on: ubuntu-latest
    needs: [build-and-test, analyze]
    
    steps:
    - name: Deploy to Azure
      uses: azure/webapps-deploy@v3
      with:
        app-name: ${{ secrets.AZURE_APP_NAME }}
        publish-profile: ${{ secrets.AZURE_PUBLISH_PROFILE }}

Otimizacoes que fazem diferenca

Cache de pacotes NuGet

- name: Cache NuGet
  uses: actions/cache@v4
  with:
    path: ~/.nuget/packages
    key: nuget-${{ hashFiles('**/*.csproj') }}

Economia: 30-60 segundos por build.

Testes em paralelo

- name: Test
  run: dotnet test --configuration Release -- RunConfiguration.MaxCpuCount=0

Metricas do pipeline

MetricaTarget
Tempo total< 5 min
Taxa de sucesso> 95%
Flaky tests0

Um pipeline lento e um pipeline ignorado. Invista em velocidade.

#GitHub Actions#CI/CD#.NET#DevOps
T

Tiago Spana

Software Engineer & Architect

Engenheiro de software com foco em arquitetura de sistemas, cloud-native e DevOps. Construindo sistemas escalaveis em producao.

Gostou do conteudo?

Inscreva-se para receber novos artigos sobre engenharia de software.