Setting Up Continuous Integration (CI) for ASP.NET Core 8 API with GitHub Actions

Setting Up Continuous Integration (CI) for ASP.NET Core 8 API with GitHub Actions

Continuous Integration (CI) is a key practice in modern software development. It ensures that every change made to the codebase is automatically built and tested, catching issues early before they reach production. In this article, I’ll walk you through setting up a CI workflow for an ASP.NET Core 8 API using GitHub Actions.


Why CI?

  • Automatically builds your project on every commit.

  • Ensures code compiles and dependencies are correctly restored.

  • Reduces integration issues for teams.

  • Prepares the groundwork for Continuous Deployment (CD).


GitHub Actions Workflow for CI

Here’s the workflow I created for my SimpleAPI project. The workflow automatically triggers on pushes and pull requests to the main branch.

 

name: ASP.NET Core 8 API CI/CD
on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

jobs:
  build:
    runs-on: ubuntu-latest
    env:
      SOLUTION_PATH: SimpleAPI/SimpleAPI.sln

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

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

      - name: Cache NuGet Packages
        uses: actions/cache@v3
        with:
          path: ~/.nuget/packages
          key: ${{ runner.os }}-nuget-${{ hashFiles('**/*.csproj') }}
          restore-keys: |
            ${{ runner.os }}-nuget-

      - name: Restore dependencies
        run: dotnet restore $SOLUTION_PATH

      - name: Build solution
        run: dotnet build $SOLUTION_PATH --no-restore --configuration Release


Step-by-Step Explanation

1. Trigger the Workflow

 
on:
  push:
    branches: [main]
  pull_request:
    branches: [main]
  • The workflow runs whenever code is pushed to main or a pull request targeting main is opened.

  • This ensures that all changes are verified before merging into the main branch.


2. Configure the Job

 
jobs:
  build:
    runs-on: ubuntu-latest
    env:
      SOLUTION_PATH: SimpleAPI/SimpleAPI.sln
  • The job is named build and runs on the latest Ubuntu runner provided by GitHub.

  • The SOLUTION_PATH environment variable points to the .sln file for easy reuse in commands.


3. Checkout the Code

 
- name: Checkout code
  uses: actions/checkout@v3
  • Uses GitHub's official checkout action to clone your repository on the runner.

  • Essential for the runner to access your code.


4. Setup .NET SDK

 
- name: Setup .NET 8 SDK
  uses: actions/setup-dotnet@v3
  with:
    dotnet-version: '8.0.x'
  • Installs .NET 8 SDK on the runner.

  • Ensures the workflow uses the same .NET version as your local development environment.


5. Cache NuGet Packages

 
- name: Cache NuGet Packages
  uses: actions/cache@v3
  with:
    path: ~/.nuget/packages
    key: ${{ runner.os }}-nuget-${{ hashFiles('**/*.csproj') }}
    restore-keys: |
      ${{ runner.os }}-nuget-
  • Caches NuGet packages to speed up subsequent builds.

  • The cache key changes only when .csproj files change, ensuring updated dependencies are restored.


6. Restore Dependencies

 
- name: Restore dependencies
  run: dotnet restore $SOLUTION_PATH
  • Runs dotnet restore to download and install all dependencies defined in your solution.


7. Build the Solution

 
- name: Build solution
  run: dotnet build $SOLUTION_PATH --no-restore --configuration Release
  • Builds the project in Release mode.

  • Skips restore because dependencies are already restored in the previous step.


Conclusion

This GitHub Actions workflow sets up a robust CI pipeline for your ASP.NET Core 8 API:

  • Automatically builds on every push or pull request to main.

  • Restores dependencies efficiently using caching.

  • Builds the solution in Release mode to ensure production readiness.

Download full working code from GitHub

The next step is Continuous Deployment (CD), where we can deploy the API automatically to a hosting platform like Azure or Render.


Thanks, for reading the blog, I hope it helps you. Please share this link on your social media accounts so that others can read our valuable content. Share your queries with our expert team and get Free Expert Advice for Your Business today.


About Writer

Ravinder Singh

Full Stack Developer
I have 15+ years of experience in commercial software development. I write this blog as a kind of knowledge base for myself. When I read about something interesting or learn anything I will write about it. I think when writing about a topic you concentrate more and therefore have better study results. The second reason why I write this blog is, that I love teaching and I hope that people find their way on here and can benefit from my content.

Hire me on Linkedin

My portfolio

Ravinder Singh Full Stack Developer