MSDN Community

Getting Started with .NET CI/CD

Welcome to the essential guide for integrating Continuous Integration and Continuous Delivery (CI/CD) into your .NET development workflow. This topic provides a foundational understanding and practical steps to automate your build, test, and deployment processes.

What is CI/CD?

CI/CD is a set of practices that automates and streamlines the software development lifecycle. It focuses on frequent integration of code changes and automated delivery to production environments.

Key Benefits for .NET Developers

Essential Components of a .NET CI/CD Pipeline

A typical .NET CI/CD pipeline consists of several stages:

  1. Source Control: Using Git (e.g., GitHub, Azure Repos, GitLab) to manage your code.
  2. Build: Compiling your .NET code, running unit tests, and creating artifacts (e.g., DLLs, executables, Docker images).
  3. Test: Executing various types of tests, including unit tests, integration tests, and UI tests.
  4. Deploy: Releasing your application to different environments like development, staging, or production.

Getting Started with Azure DevOps

Azure DevOps is a comprehensive suite of development tools that provides robust support for CI/CD. Here's a basic outline to set up your first pipeline:

1. Set up your Project in Azure DevOps

If you haven't already, create a project in Azure DevOps and link your Git repository.

2. Create a new Pipeline

Navigate to Pipelines -> Pipelines and click "New pipeline".

3. Configure your Pipeline

Azure DevOps can automatically detect your .NET project. You'll typically choose the "Starter pipeline" option and configure the YAML file.

# Azure Pipelines YAML multi-stage pipeline trigger: - main # Trigger the pipeline on commits to the main branch pool: vmImage: 'ubuntu-latest' # Use a Microsoft-hosted agent variables: buildConfiguration: 'Release' projectName: 'YourProjectName' # Replace with your project name stages: - stage: Build displayName: Build and Test jobs: - job: BuildAndTest displayName: Build and Unit Test steps: - task: DotNetCoreCLI@2 displayName: 'Restore NuGet packages' inputs: command: 'restore' projects: '**/*.csproj' - task: DotNetCoreCLI@2 displayName: 'Build Project' inputs: command: 'build' projects: '**/*.csproj' arguments: '--configuration $(buildConfiguration)' - task: DotNetCoreCLI@2 displayName: 'Run Unit Tests' inputs: command: 'test' projects: '**/*[Tt]ests/*.csproj' # Adjust for your test project naming arguments: '--configuration $(buildConfiguration)' - task: DotNetCoreCLI@2 displayName: 'Publish Artifacts' inputs: command: 'publish' publishWebProjects: false projects: '$(projectName)/$(projectName).csproj' # Adjust path if needed arguments: '--configuration $(buildConfiguration) --output $(Build.ArtifactStagingDirectory)' zipAfterPublish: true - publish: $(Build.ArtifactStagingDirectory) artifact: drop - stage: Deploy displayName: Deploy to Staging dependsOn: Build condition: succeeded() # Only deploy if build stage succeeded jobs: - deployment: DeployWebApp displayName: Deploy Web App environment: 'Staging' # Define your Azure DevOps environment strategy: runOnce: deploy: steps: - download: current # Download artifacts from the current pipeline run artifact: drop - task: AzureWebApp@1 displayName: 'Deploy to Azure App Service' inputs: azureSubscription: 'YourAzureServiceConnection' # Replace with your Azure Service Connection name appName: 'your-staging-app-service-name' # Replace with your App Service name package: '$(Pipeline.Workspace)/drop/**/*.zip'

Getting Started with GitHub Actions

GitHub Actions offers a flexible way to automate your .NET CI/CD directly from your GitHub repository.

1. Create a Workflow File

In your repository, create a `.github/workflows/dotnet-ci.yml` file.

name: .NET CI on: push: branches: [ main ] pull_request: branches: [ main ] jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Setup .NET uses: actions/setup-dotnet@v3 with: dotnet-version: '7.0.x' # Specify your .NET SDK version - name: Restore dependencies run: dotnet restore - name: Build run: dotnet build --no-restore - name: Test run: dotnet test --no-build --verbosity normal - name: Publish run: dotnet publish -c Release -o ./publish - name: Upload Artifact uses: actions/upload-artifact@v3 with: name: dotnet-app path: ./publish
"CI/CD is not just about automation; it's about building a culture of quality and rapid feedback."

Next Steps