Introduction
CI/CD stands for Continuous Integration and Continuous Deployment. It helps teams automatically build, test, and deploy applications whenever code changes are pushed to a repository. For .NET Core projects, GitHub Actions provides an easy and powerful way to create CI/CD pipelines directly inside GitHub.
In simple words, GitHub Actions helps you automate your .NET Core project workflow so you spend less time doing manual work and more time writing code. In this article, you will learn how to set up a complete CI/CD pipeline for a .NET Core project using GitHub Actions, step by step.
What Is GitHub Actions?
GitHub Actions is a CI/CD tool built into GitHub. It allows you to create workflows that run automatically based on events such as pushing code, creating a pull request, or merging changes.
Key benefits:
No separate CI tool needed
Easy integration with GitHub repositories
Supports build, test, and deployment
GitHub Actions uses YAML files to define workflows.
Prerequisites
Before setting up the pipeline, make sure you have:
A GitHub repository with a .NET Core project
Basic understanding of Git and GitHub
.NET SDK installed locally for development
Once these are ready, you can start creating the pipeline.
Understanding the GitHub Actions Workflow Structure
GitHub Actions workflows are stored inside the repository.
Path:
.github/workflows/
Each workflow is a YAML file that defines:
Creating a Basic CI Workflow for .NET Core
Create a new file named dotnet-ci.yml inside .github/workflows.
Example workflow:
name: .NET Core CI
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: '8.0.x'
- name: Restore dependencies
run: dotnet restore
- name: Build project
run: dotnet build --no-restore
- name: Run tests
run: dotnet test --no-build --verbosity normal
Explanation:
The workflow runs on every push and pull request to main
It restores packages, builds the project, and runs tests
Running Unit Tests Automatically
Running tests in CI ensures that new changes do not break existing functionality.
- name: Run tests
run: dotnet test
If tests fail, the pipeline stops and reports an error. This helps catch issues early.
Adding Code Quality Checks
You can add simple code quality steps like formatting or analyzers.
Example:
- name: Check formatting
run: dotnet format --verify-no-changes
This ensures consistent code style across the team.
Setting Up Continuous Deployment (CD)
After CI passes, you may want to deploy the application automatically.
Common deployment targets:
Azure App Service
Linux or Windows server
Docker container
Deployment usually happens only after a successful build.
Using Environment Variables and Secrets
Sensitive values such as connection strings or API keys should never be hardcoded.
Steps:
Example usage in workflow:
- name: Deploy
run: echo "Deploying using secret"
env:
CONNECTION_STRING: ${{ secrets.CONNECTION_STRING }}
Secrets are securely stored and not visible in logs.
Deploying a .NET Core App (Example)
Below is a simple example of publishing a .NET Core app.
- name: Publish app
run: dotnet publish -c Release -o out
The output folder can then be deployed to a server or cloud platform.
CI/CD for Pull Requests
CI pipelines are especially useful for pull requests.
Benefits:
The same workflow can run automatically for pull requests.
Common Mistakes to Avoid
Many beginners face these issues:
Forgetting to restore packages
Not running tests in CI
Hardcoding secrets in workflows
Using incorrect .NET version
Avoiding these mistakes makes pipelines more reliable.
Best Practices for .NET CI/CD Pipelines
Follow these best practices:
Keep workflows simple and readable
Run tests on every push
Use secrets for sensitive data
Fail fast when errors occur
Separate CI and CD if needed
These practices help maintain stable and secure pipelines.
Summary
Setting up a CI/CD pipeline using GitHub Actions for a .NET Core project is simple and powerful. By automating restore, build, test, and deployment steps, you can ensure faster feedback, better code quality, and reliable releases. With GitHub Actions, .NET Core developers can implement DevOps best practices directly within their repositories and scale their workflows as projects grow.