What is the best way to set up CI/CD for ASP.NET Core apps targeting Azure App Service using GitHub Actions?

Asked 1 hour ago 11 views

0

Automating Deployments to App Service

Manual zip-and-drop deployments do not scale. A GitHub Actions workflow can build, test, and push your ASP.NET Core project straight to an Azure App Service slot, giving you zero-downtime releases.

Workflow Structure

The pipeline needs an Azure login action, a build step that restores NuGet packages and runs your test suite, and a deployment action targeting the correct slot. Environment variables for connection strings should live in the repository secrets, not the workflow file.

# .github/workflows/deploy.yml
name: Deploy to Azure App Service

on:
  push:
    branches: [ main ]

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    steps:
      # Authenticate against Azure using a service principal stored in secrets.
      - name: Azure Login
        uses: azure/login@v1
        with:
          creds: ${{ secrets.AZURE_CREDENTIALS }}

      # Restore dependencies and build the web project.
      - name: Setup .NET
        uses: actions/setup-dotnet@v3
        with:
          dotnet-version: '8.0.x'
      - run: dotnet restore
      - run: dotnet build --no-restore

      # Publish the app and slot it into the staging environment.
      - name: Deploy to App Service
        uses: Azure/webapps-deploy@v3
        with:
          app-name: 'my-aspnet-app'
          slot-name: 'staging'
          package: 'publish/my-aspnet-app.zip'

Always promote the staging slot to production only after health checks pass. Skipping this step means a broken build can instantly replace live traffic.

0 Answers


Write Your Answer