ASP.NET Core MVC Advanced

CI/CD with GitHub Actions

Continuous integration builds and tests every change automatically, and continuous deployment ships the main branch without manual steps. This lesson writes a GitHub Actions workflow that builds, tests and deploys an ASP.NET Core app to Azure App Service using OpenID Connect instead of stored passwords.

From push to production
  1. StartPush or pull requestWorkflow starts
  2. Step 2Build and testRed build blocks the merge
  3. Step 3Publish artifactRelease output saved
  4. Step 4OIDC login to AzureNo stored password
  5. EndDeployApp Service updated, optionally after approval

What the pipeline does

  • On every push and pull request: restore, build and run the tests. A red build blocks merging.
  • On the main branch: publish the app, then deploy it to a staging slot or straight to production.
  • Secrets live in GitHub (or better, are replaced by OIDC federation), never in the workflow file.
  • Environments can require a reviewer to approve production deployments.

The workflow

Save this as .github/workflows/ci.yml. The deploy job uses azure/login with OpenID Connect: GitHub proves its identity to Azure, so no Azure password or publish profile is stored.

YAML
name: CI
on:
  push:
    branches: [main]
  pull_request:

permissions:
  contents: read
  id-token: write          # needed for OIDC login to Azure

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-dotnet@v4
        with:
          dotnet-version: 9.0.x
      - run: dotnet restore
      - run: dotnet build --no-restore -c Release
      - run: dotnet test --no-build -c Release
      - run: dotnet publish src/Shop.Web -c Release -o publish --no-build
      - uses: actions/upload-artifact@v4
        with:
          name: web
          path: publish

  deploy:
    if: github.ref == 'refs/heads/main'
    needs: build
    runs-on: ubuntu-latest
    environment: production        # can require approval
    steps:
      - uses: actions/download-artifact@v4
        with:
          name: web
          path: publish
      - uses: azure/login@v2
        with:
          client-id: ${{ secrets.AZURE_CLIENT_ID }}
          tenant-id: ${{ secrets.AZURE_TENANT_ID }}
          subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
      - uses: azure/webapps-deploy@v3
        with:
          app-name: myshop-web
          package: publish

Making it reliable

  • Run integration tests against a real database in the job (a SQL Server service container or Testcontainers).
  • Add a step that runs the EF Core migration bundle before deploying the app.
  • Deploy to a staging slot, run a smoke test against it, then swap.
  • Protect the main branch: require the CI check and a review before merging.
  • Pin action versions and grant the workflow only the permissions it needs.

Interview question

Why use OpenID Connect instead of a stored publish profile or password to deploy from GitHub Actions?

With OIDC, Azure trusts short-lived tokens that GitHub issues for a specific repository, branch or environment, so no long-lived secret exists that could leak or need rotating. Access can be limited precisely through the federated credential, and every login is tied to a workflow run, which is easier to audit.

Your progress is saved only in this browser.

Want the full details? Read the official Microsoft documentation for this topic.