A good pipeline gives every change the same fast, repeatable checks: does it build, do the tests pass, can it be packaged. GitHub Actions makes this straightforward for .NET projects. This tutorial builds a workflow in stages, from a basic build to a gated deployment.

Step 1: Build and test on every push

Create .github/workflows/ci.yml:

name: ci

on:
  push:
    branches: [main]
  pull_request:

jobs:
  build-test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-dotnet@v4
        with:
          dotnet-version: '10.0.x'

      - name: Restore
        run: dotnet restore

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

      - name: Test
        run: dotnet test --no-build --configuration Release --logger trx

This runs for pull requests and for pushes to main. Make the build-test job a required status check in your branch protection rules so broken code cannot be merged.

Step 2: Speed it up with caching

The setup-dotnet action can cache NuGet packages. It uses lock files for the cache key, so first enable them in your project:

<PropertyGroup>
  <RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
</PropertyGroup>

Commit the generated packages.lock.json files, then turn caching on:

      - uses: actions/setup-dotnet@v4
        with:
          dotnet-version: '10.0.x'
          cache: true
          cache-dependency-path: '**/packages.lock.json'

Step 3: Publish a build artifact

Add a second job that runs only on main and produces a deployable package:

  publish:
    needs: build-test
    if: github.ref == 'refs/heads/main'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-dotnet@v4
        with:
          dotnet-version: '10.0.x'
      - run: dotnet publish src/MyApp/MyApp.csproj -c Release -o publish
      - uses: actions/upload-artifact@v4
        with:
          name: app
          path: publish

Replace src/MyApp/MyApp.csproj with your project path. Building once and deploying the same artifact to every environment avoids "it worked in staging" surprises.

Step 4: Deploy behind an approval gate

GitHub environments let you require manual approval and scope secrets to production. Create an environment named production in the repository settings, add required reviewers, then add a deploy job:

  deploy:
    needs: publish
    runs-on: ubuntu-latest
    environment: production
    steps:
      - uses: actions/download-artifact@v4
        with:
          name: app
          path: publish
      - name: Deploy
        run: ./deploy.sh publish
        env:
          DEPLOY_KEY: ${{ secrets.DEPLOY_KEY }}

The deploy.sh step stands in for whatever your target needs: an Azure Web App action, an SSH and rsync deployment, a container push or an AWS CLI command. The pattern stays the same: the job waits for approval, then uses secrets that only exist in that environment.

Good practices

  • Keep secrets out of the repository and out of logs. Use repository or environment secrets.
  • Pin third-party actions to a version, or to a commit SHA for stricter supply chain control.
  • Give the workflow minimal permissions with a top-level permissions: contents: read.
  • Fail fast. Run unit tests before slower integration tests.
  • Scan dependencies with dotnet list package --vulnerable, as covered in our OWASP Top 10:2025 guide.

Targeting the newest framework? See what is new in ASP.NET Core 10.

References