Continuous Integration (CI) is a development practice where developers merge their code changes into a shared repository several times a day. Each integration is verified by an automated build and test process to detect integration errors as quickly as possible. In this tutorial, we will explore how to set up CI pipelines for C# projects using popular tools like GitHub Actions, Azure DevOps, and Jenkins.
The primary goal of CI is to ensure that the codebase remains stable and functional throughout the development lifecycle. By automating the build and test processes, developers can catch issues early, reducing the time and effort required to fix bugs later in the project.
GitHub Actions is a powerful automation tool that allows you to automate your software workflow. Here’s how you can set up a basic CI pipeline for a C# project using GitHub Actions.
Create a Workflow File:
Create a new file named .github/workflows/ci.yml in the root of your repository.
1name: C# CI23on:4push:5branches: [ main ]6pull_request:7branches: [ main ]89jobs:10build:1112runs-on: ubuntu-latest1314steps:15- uses: actions/checkout@v216- name: Setup .NET17uses: actions/setup-dotnet@v118with:19dotnet-version: '6.0.x'20- name: Build21run: dotnet build --configuration Release --no-restore22- name: Test23run: dotnet test
Commit and Push the Workflow File:
Add the .github/workflows/ci.yml file to your repository and commit it.
git add .github/workflows/ci.ymlgit commit -m "Add GitHub Actions CI workflow"git push origin main
Observe the Workflow Execution:
After pushing the changes, navigate to the "Actions" tab in your GitHub repository to see the workflow running.
Azure DevOps is another robust platform for setting up continuous integration pipelines. Here’s how you can set it up for a C# project.
Create a New Pipeline:
main).Configure the Pipeline:
Azure DevOps will provide a YAML template for your C# project. You can customize it as needed.
1trigger:2branches:3include:4- main56pool:7vmImage: 'ubuntu-latest'89steps:10- task: UseDotNet@211inputs:12packageType: 'sdk'13version: '6.0.x'14installationPath: $(Agent.ToolsDirectory)/dotnet1516- script: dotnet build --configuration Release17displayName: 'Build'1819- script: dotnet test20displayName: 'Test'
Save and Run the Pipeline:
Save the pipeline configuration and run it to see the CI process in action.
Jenkins is a widely used open-source automation server that can be configured for continuous integration.
Install Jenkins Plugins:
Create a New Job:
Configure Build Steps:
1stage('Build') {2steps {3bat 'msbuild /p:Configuration=Release'4}5}67stage('Test') {8steps {9bat 'vstest.console.exe ***Tests.dll'10}11}
Save and Run the Job:
Save the job configuration and run it to see the CI process in action.
After setting up continuous integration, you can explore continuous deployment (CD) to automate the release of your application. This will allow you to deploy your code changes automatically to staging or production environments once they pass all tests.
For more information on continuous deployment in C#, check out our next tutorial: Continuous Deployment in C#.
Info
Continuous integration is a crucial step in modern software development practices. It helps maintain code quality and ensures that your application remains stable as new features are added.