Introduction: Why CI/CD is Critical for C# (.NET) Projects
Modern software development is impossible without automation. Manual build, testing, and deployment processes are not only slow but also error-prone. For C# developers working with the .NET ecosystem, implementing CI/CD (Continuous Integration / Continuous Deployment) practices has become an industry standard.
GitHub Actions is a powerful and flexible tool for automating workflows, built directly into GitHub. It allows you to create pipelines (workflows) that respond to events in your repository: pushes, Pull Request creation, releases, and much more. In this article, we will walk through how to set up a complete CI/CD pipeline for a C# project based on .NET using GitHub Actions.
You will learn how to:
- Set up automatic project builds.
- Run unit tests (e.g., xUnit or NUnit).
- Publish build artifacts.
- Automatically deploy the application (e.g., to Azure, Docker Hub, or a server).
1. GitHub Actions Basics: Workflow, Jobs, and Steps
Before writing code, let's understand the basic concepts of GitHub Actions.
- Workflow: This is an automated process that you define in a YAML file inside the
.github/workflowsfolder. A workflow is triggered by an event (e.g., a push to the main branch). - Job: A set of steps that execute on a single runner (virtual machine). Jobs can run sequentially or in parallel.
- Step: An individual command or action. This could be running a script, installing dependencies, or using a pre-built action from the marketplace.
- Runner: The server that executes your workflow. GitHub provides free runners (Ubuntu, Windows, macOS), or you can host your own.
For .NET projects, we will use ready-made actions such as actions/checkout (for cloning the repository) and actions/setup-dotnet (for installing the .NET SDK).
2. Setting Up a Basic CI Pipeline: Build and Test
Let's create our first workflow. To do this, create a folder named .github/workflows in the root of your repository, and inside it, create a file named dotnet-build.yml.
Here is a minimal YAML file for building and testing a C# project:
name: .NET Build and Test
on: push: branches: [ main, develop ] pull_request: branches: [ main ]
jobs: build-and-test: runs-on: ubuntu-latest
steps: - name: Checkout repository uses: actions/checkout@v4
- name: Setup .NET uses: actions/setup-dotnet@v4 with: dotnet-version: '8.0.x' # Specify your .NET version
- name: Restore dependencies run: dotnet restore
- name: Build run: dotnet build --no-restore --configuration Release
- name: Run tests run: dotnet test --no-build --configuration Release --verbosity normalCode Breakdown:
- name: The name of the workflow, which will be displayed in GitHub.
- on: Defines the triggers. In our case, it's pushes and pull requests to the main and develop branches.
- jobs: Contains a single job called
build-and-test, which runs onubuntu-latest. - steps: A sequence of actions: cloning the code, installing .NET 8.0, restoring NuGet packages, building in Release configuration, and running tests.
After pushing this file to the repository, GitHub Actions will automatically run the workflow. You will see a green checkmark or a red cross next to