How do you build and deploy a .NET app with Azure DevOps Pipelines?

IntermediateHands-on.NET builds (dotnet CLI)
Answer

Short answer

A YAML pipeline with stages: Build (restore, build, test, publish or build a Docker image), then Deploy stages per environment using environments with approvals — deploying to Azure App Service, AKS or VMs.

Example

trigger: [main]
pool: { vmImage: ubuntu-latest }
stages:
- stage: Build
  jobs:
  - job: build
    steps:
    - task: UseDotNet@2
      inputs: { version: 8.0.x }
    - script: dotnet restore
    - script: dotnet build -c Release --no-restore
    - script: dotnet test -c Release --no-build --logger trx
    - task: PublishTestResults@2
      inputs: { testResultsFormat: VSTest, testResultsFiles: '**/*.trx' }
    - script: dotnet publish src/Shop.Api -c Release -o $(Build.ArtifactStagingDirectory) --no-build
    - publish: $(Build.ArtifactStagingDirectory)
      artifact: drop
- stage: DeployStaging
  dependsOn: Build
  jobs:
  - deployment: staging
    environment: shop-staging
    strategy:
      runOnce:
        deploy:
          steps:
          - task: AzureWebApp@1
            inputs: { azureSubscription: shop-sc, appName: shop-api-stg, package: $(Pipeline.Workspace)/drop }

Production is another stage whose environment has an approval check.

What to say in the interview

"Build stage restores, builds, tests with published results and publishes an artifact; deployment jobs target environments like shop-staging and shop-prod, where prod has approvals. The target is App Service via AzureWebApp, or AKS by building an image to ACR and updating the manifests."

All Build Tools (Java, Node.js, Python, .NET) questions Roadmap: .NET apps: dotnet build & publish

More on .NET builds (dotnet CLI)