Users Pricing

blog

Home Blogs Set Up CI/CD for ASP.NET Core with GitHub Actions – MindStick
Set Up CI/CD for ASP.NET Core with GitHub Actions

Set Up CI/CD for ASP.NET Core with GitHub Actions

Poulami Sen 93 24 Nov 2026

If deploying your ASP.NET Core app involves a developer building on their laptop, zipping the output and copying it to a server over Remote Desktop, you're one tired Friday evening away from deploying the wrong build.

CI/CD (continuous integration and continuous delivery) automates that process. Every push is built and tested automatically, and deployment becomes a repeatable button press, or happens automatically when tests pass.

GitHub Actions is a natural fit if your code is already on GitHub. Here's how to set it up from scratch.

What we're building

1. On every pull request: restore, build and run tests. Block the merge if anything fails.
2. On every push to main: build, test, publish the app as an artefact.
3. Deploy the artefact to a staging environment, then to production after approval.

Step 1: The basic CI workflow

A few notes:

- ubuntu-latest is faster and cheaper than Windows runners. Modern ASP.NET Core builds fine on Linux even if you deploy to Windows. Use windows-latest only if your build needs Windows-specific tools.
- Caching NuGet packages speeds up builds considerably. The cache option relies on lock files. Enable them by adding RestorePackagesWithLockFiletrueRestorePackagesWithLockFile to your project (or Directory.Build.props) and committing the generated packages.lock.json. If you'd rather not use lock files, remove the cache lines and --locked-mode.
- --no-restore and --no-build avoid repeating work from earlier steps.
- if: always() uploads test results even when tests fail, which is exactly when you need them.

Step 2: Protect the main branch

In your repository settings, under Branches, add a rule for main:

- Require a pull request before merging.
- Require status checks to pass, and select the build-test job.

Now nobody, including you, can merge code that doesn't build or fails tests.

Step 3: Publish a deployable artefact

Add a publish step and upload the output.

The key principle: build once, deploy the same artefact everywhere. Staging and production should receive identical files, not separate builds.

Step 4: Deploy with environments

GitHub environments let you store secrets per environment and require approval before a deployment runs.

In repository settings, create two environments:

- staging: no approval required
- production: add required reviewers (for example, the tech lead)

The production job waits for a reviewer to click Approve in GitHub. Each environment has its own AZURE_PUBLISH_PROFILE secret.

For better security on Azure, consider OpenID Connect (OIDC) with azure/login instead of publish profiles. It avoids storing long-lived credentials in GitHub.

Deploying to your own IIS server

Many teams deploy to Windows Server with IIS rather than Azure. You have two common options.

Option A: A self-hosted runner on the server

Install a GitHub Actions self-hosted runner on the server (or on a machine in the same network) as a Windows service.

Some details worth understanding:

- app_offline.htm makes the ASP.NET Core Module shut down the app and release locked DLLs, so files can be replaced. Keep the file in your repo (here in a deploy folder).
- robocopy /MIR mirrors the folder. We exclude production settings, the offline file and the logs folder so they aren't deleted.
- robocopy exit codes below 8 mean success, so we translate them for GitHub Actions.
- Self-hosted runners execute workflow code on your server. Only use them with private repositories, and give the runner's service account only the permissions it needs.

Option B: Web Deploy (MSDeploy)

If Web Deploy is installed on IIS, you can push from a GitHub-hosted Windows runner over HTTPS. This avoids installing a runner on the server but requires opening the Web Deploy port to GitHub's runners or using a secure tunnel. Many teams prefer Option A for servers inside private networks.

Step 5: Database migrations

If you use Entity Framework Core, don't run migrations automatically on app start in production. A cleaner approach is to generate an idempotent SQL script in CI and apply it as a deliberate step:

- name: Generate migration script
run: |
dotnet tool install --global dotnet-ef
dotnet ef migrations script --idempotent --output migrate.sql --project src/MyApp.Data

Review and apply the script before or during deployment. Better still, use migration bundles (dotnet ef migrations bundle) for a self-contained executable.

Good habits

- Keep secrets in GitHub secrets or a vault, never in the repository or workflow files.
- Pin action versions (@v4, or full commit SHAs for stricter security).
- Keep workflows fast. Developers stop waiting for slow pipelines. Cache packages and run tests in parallel.
- Add a smoke test after deployment: call a /health endpoint and fail the job if it doesn't respond.
- Make rollback easy. Keep previous artefacts so you can redeploy a known-good version quickly.
- Use deployment slots on Azure App Service to deploy to a staging slot and swap with zero downtime.

Wrapping up

With one workflow file, every pull request is built and tested, the main branch stays deployable, and releases follow the same path every time: build once, deploy to staging, approve, deploy to production.

Start with the CI job today. Add the deployment jobs once your tests are reliable. After a few weeks, manual deployments will feel as outdated as emailing zip files around.


Poulami Sen

Software Developer at UST

I am a Software Developer at UST, with 12 years of experience in Java and Spring Boot web applications. I completed my BCA from the University of Calcutta and MCA from Jadavpur University, Kolkata. I am originally from Kolkata, West Bengal. Outside work I enjoy reading novels and gardening.


Markdown for AI

A clean, structured version of this page for AI assistants and LLMs.

Open .md