DevOps  

How to Set Up CI/CD Pipeline Using GitHub Actions for .NET Projects

Introduction

CI/CD (Continuous Integration and Continuous Deployment) is an essential practice in modern software development. It helps developers automatically build, test, and deploy applications whenever code changes are made.

GitHub Actions is a powerful and easy-to-use tool that allows you to automate your CI/CD pipelines directly from your GitHub repository. For .NET developers, it provides seamless integration to build and deploy ASP.NET Core applications.

In this article, you will learn how to set up a CI/CD pipeline using GitHub Actions for .NET projects step by step using simple language and practical examples.

What is CI/CD?

CI/CD stands for:

  • Continuous Integration → Automatically build and test code on every change

  • Continuous Deployment → Automatically deploy code to production or server

Benefits:

  • Faster development cycle

  • Fewer bugs in production

  • Automated workflows

  • Better collaboration

What is GitHub Actions?

GitHub Actions is a CI/CD tool built into GitHub that allows you to create workflows to automate your development process.

Key features:

  • Runs on every push or pull request

  • Supports multiple environments

  • Easy YAML-based configuration

  • Supports .NET projects natively

Prerequisites

Before starting, make sure you have:

  • GitHub account

  • .NET project pushed to GitHub repository

  • Basic understanding of Git and .NET CLI

Understanding GitHub Actions Workflow

A GitHub Actions workflow is defined using a YAML file.

Key components:

  • Workflow → The automation process

  • Job → A group of steps

  • Step → Individual task (build, test, deploy)

Workflow location:

.github/workflows/ci-cd.yml

Step 1: Create Workflow File

Inside your project, create folder:

.github/workflows

Create file:

ci-cd.yml

Step 2: Basic CI Pipeline (Build + Test)

name: .NET CI Pipeline

on:
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout code
      uses: actions/checkout@v3

    - name: Setup .NET
      uses: actions/setup-dotnet@v3
      with:
        dotnet-version: '8.0.x'

    - name: Restore dependencies
      run: dotnet restore

    - name: Build project
      run: dotnet build --no-restore --configuration Release

    - name: Run tests
      run: dotnet test --no-build --verbosity normal

This pipeline will:

  • Run on every push

  • Restore packages

  • Build application

  • Execute tests

Step 3: Add Publish Step

- name: Publish project
  run: dotnet publish -c Release -o ./publish

This generates deployment-ready files.

Step 4: Upload Build Artifacts

- name: Upload artifact
  uses: actions/upload-artifact@v3
  with:
    name: app
    path: ./publish

Artifacts help store build output for deployment.

Step 5: Setup CD (Deployment)

You can deploy to different environments:

  • AWS EC2

  • Azure App Service

  • Docker containers

Example: Deploy to AWS EC2 using SSH

Add secrets in GitHub:

  • EC2_HOST

  • EC2_USER

  • EC2_SSH_KEY

Add step:

- name: Deploy to EC2
  uses: appleboy/[email protected]
  with:
    host: ${{ secrets.EC2_HOST }}
    username: ${{ secrets.EC2_USER }}
    key: ${{ secrets.EC2_SSH_KEY }}
    script: |
      cd /home/ec2-user/app
      git pull
      dotnet publish -c Release -o ./publish
      sudo systemctl restart myapp

Step 6: Full CI/CD Workflow Example

name: .NET CI/CD Pipeline

on:
  push:
    branches: [ "main" ]

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v3

    - uses: actions/setup-dotnet@v3
      with:
        dotnet-version: '8.0.x'

    - run: dotnet restore
    - run: dotnet build --configuration Release
    - run: dotnet test

    - run: dotnet publish -c Release -o ./publish

    - uses: appleboy/[email protected]
      with:
        host: ${{ secrets.EC2_HOST }}
        username: ${{ secrets.EC2_USER }}
        key: ${{ secrets.EC2_SSH_KEY }}
        script: |
          cd /home/ec2-user/app
          git pull
          sudo systemctl restart myapp

Step 7: Using Environment Variables and Secrets

GitHub provides secure storage for secrets.

Example:

env:
  DOTNET_ENVIRONMENT: Production

Secrets are accessed using:

${{ secrets.SECRET_NAME }}

Step 8: Monitoring Pipeline

Go to GitHub → Actions tab

You can:

  • View workflow runs

  • Debug failures

  • Check logs

Best Practices for CI/CD in .NET

  • Keep pipeline fast and simple

  • Use caching for dependencies

  • Separate build and deploy jobs

  • Use environment-specific configs

  • Secure secrets properly

Common Mistakes to Avoid

  • Hardcoding credentials

  • Skipping tests

  • Large build times

  • Not using artifacts

Real-World CI/CD Flow

  • Developer pushes code

  • GitHub Actions triggers workflow

  • Code is built and tested

  • Artifacts are generated

  • Deployment happens automatically

Summary

Setting up a CI/CD pipeline using GitHub Actions for .NET projects helps automate your development lifecycle. By building, testing, and deploying your application automatically, you can improve productivity, reduce errors, and deliver features faster. With simple YAML configuration and powerful integrations, GitHub Actions makes CI/CD easy and efficient for modern .NET applications.