Deploying .NET 8 Minimal APIs to Azure with CI/CD: A Step-by-Step Guide
In this post, we'll explore how to deploy your .NET 8 Minimal APIs to Azure using CI/CD pipelines. Leveraging automated deployment tools like GitHub Actions or Azure Pipelines can streamline your release process, reduce manual errors, and allow you to focus on building great applications. Table of Contents Introduction Prerequisites Preparing Your .NET 8 Minimal API Project Setting Up CI/CD Pipelines Using GitHub Actions Using Azure Pipelines Best Practices for CI/CD Deployments Conclusion Introduction Deploying your .NET 8 Minimal APIs to Azure is a critical step in delivering your application to users. Automating this process with CI/CD pipelines can significantly enhance your development workflow. In this guide, we will walk through setting up a CI/CD pipeline using both GitHub Actions and Azure Pipelines, providing code examples and best practices along the way. Prerequisites Before getting started, ensure you have the following: A working .NET 8 Minimal API project. An active Azure account. An Azure App Service for hosting your API. A GitHub repository for your project (if using GitHub Actions) or Azure DevOps account (if using Azure Pipelines). Preparing Your .NET 8 Minimal API Project For demonstration, let's assume your project structure is similar to the following: MyMinimalApi/ ├── Program.cs ├── MyMinimalApi.csproj └── appsettings.json In your Program.cs, you might have something like: var builder = WebApplication.CreateBuilder(args); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); var app = builder.Build(); if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(); } app.MapGet("/", () => "Hello from .NET 8 Minimal API!"); app.Run(); Make sure your API is ready for production by configuring settings such as environment variables, logging, and error handling. Setting Up CI/CD Pipelines Using GitHub Actions GitHub Actions provides an easy way to automate your deployment process. Create a workflow file in your repository at .github/workflows/azure-deploy.yml: name: Deploy to Azure on: push: branches: - main jobs: build-and-deploy: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v3 - name: Setup .NET uses: actions/setup-dotnet@v3 with: dotnet-version: '8.0.x' - name: Restore dependencies run: dotnet restore - name: Build run: dotnet build --configuration Release --no-restore - name: Publish run: dotnet publish -c Release -o ./publish --no-build - name: Deploy to Azure Web App uses: azure/webapps-deploy@v2 with: app-name: YOUR_AZURE_APP_SERVICE_NAME publish-profile: ${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE }} package: ./publish Explanation: This workflow triggers on pushes to the main branch. It checks out your repository, sets up the .NET environment, restores dependencies, builds, and publishes your project. Finally, it deploys the published files to your Azure Web App using a publish profile stored in GitHub Secrets. Using Azure Pipelines If you prefer using Azure Pipelines, create a file named azure-pipelines.yml in the root of your repository: trigger: branches: include: - main pool: vmImage: 'ubuntu-latest' variables: buildConfiguration: 'Release' steps: - task: UseDotNet@2 inputs: packageType: 'sdk' version: '8.0.x' installationPath: $(Agent.ToolsDirectory)/dotnet - script: | dotnet restore displayName: 'Restore Dependencies' - script: | dotnet build --configuration $(buildConfiguration) --no-restore displayName: 'Build Project' - script: | dotnet publish --configuration $(buildConfiguration) -o $(Build.ArtifactStagingDirectory) --no-build displayName: 'Publish Project' - task: PublishBuildArtifacts@1 inputs: PathtoPublish: '$(Build.ArtifactStagingDirectory)' ArtifactName: 'drop' - task: AzureWebApp@1 inputs: azureSubscription: 'YOUR_AZURE_SUBSCRIPTION' appType: 'webApp' appName: 'YOUR_AZURE_APP_SERVICE_NAME' package: '$(Build.ArtifactStagingDirectory)' Explanation: This pipeline is triggered on the main branch. It uses a hosted Ubuntu agent, restores dependencies, builds, and publishes your project. The built artifacts are then deployed to your Azure Web App. Best Practices for CI/CD Deployments Automate Testing: Integrate automated tests in your pipeline to catch issues before deployment. Use Secrets Management: Store sensitive information such as API keys and publish profiles in secure vaults or CI/CD secret stores. Implement Rollbacks: Consider strategies to automatically rollback deployments if errors occur. Monitor Deployments: Utilize Azure Monitor and Application Insights to track the health and

In this post, we'll explore how to deploy your .NET 8 Minimal APIs to Azure using CI/CD pipelines. Leveraging automated deployment tools like GitHub Actions or Azure Pipelines can streamline your release process, reduce manual errors, and allow you to focus on building great applications.
Table of Contents
- Introduction
- Prerequisites
- Preparing Your .NET 8 Minimal API Project
-
Setting Up CI/CD Pipelines
- Using GitHub Actions
- Using Azure Pipelines
- Best Practices for CI/CD Deployments
- Conclusion
Introduction
Deploying your .NET 8 Minimal APIs to Azure is a critical step in delivering your application to users. Automating this process with CI/CD pipelines can significantly enhance your development workflow. In this guide, we will walk through setting up a CI/CD pipeline using both GitHub Actions and Azure Pipelines, providing code examples and best practices along the way.
Prerequisites
Before getting started, ensure you have the following:
- A working .NET 8 Minimal API project.
- An active Azure account.
- An Azure App Service for hosting your API.
- A GitHub repository for your project (if using GitHub Actions) or Azure DevOps account (if using Azure Pipelines).
Preparing Your .NET 8 Minimal API Project
For demonstration, let's assume your project structure is similar to the following:
MyMinimalApi/
├── Program.cs
├── MyMinimalApi.csproj
└── appsettings.json
In your Program.cs
, you might have something like:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.MapGet("/", () => "Hello from .NET 8 Minimal API!");
app.Run();
Make sure your API is ready for production by configuring settings such as environment variables, logging, and error handling.
Setting Up CI/CD Pipelines
Using GitHub Actions
GitHub Actions provides an easy way to automate your deployment process. Create a workflow file in your repository at .github/workflows/azure-deploy.yml
:
name: Deploy to Azure
on:
push:
branches:
- main
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: '8.0.x'
- name: Restore dependencies
run: dotnet restore
- name: Build
run: dotnet build --configuration Release --no-restore
- name: Publish
run: dotnet publish -c Release -o ./publish --no-build
- name: Deploy to Azure Web App
uses: azure/webapps-deploy@v2
with:
app-name: YOUR_AZURE_APP_SERVICE_NAME
publish-profile: ${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE }}
package: ./publish
Explanation:
- This workflow triggers on pushes to the
main
branch. - It checks out your repository, sets up the .NET environment, restores dependencies, builds, and publishes your project.
- Finally, it deploys the published files to your Azure Web App using a publish profile stored in GitHub Secrets.
Using Azure Pipelines
If you prefer using Azure Pipelines, create a file named azure-pipelines.yml
in the root of your repository:
trigger:
branches:
include:
- main
pool:
vmImage: 'ubuntu-latest'
variables:
buildConfiguration: 'Release'
steps:
- task: UseDotNet@2
inputs:
packageType: 'sdk'
version: '8.0.x'
installationPath: $(Agent.ToolsDirectory)/dotnet
- script: |
dotnet restore
displayName: 'Restore Dependencies'
- script: |
dotnet build --configuration $(buildConfiguration) --no-restore
displayName: 'Build Project'
- script: |
dotnet publish --configuration $(buildConfiguration) -o $(Build.ArtifactStagingDirectory) --no-build
displayName: 'Publish Project'
- task: PublishBuildArtifacts@1
inputs:
PathtoPublish: '$(Build.ArtifactStagingDirectory)'
ArtifactName: 'drop'
- task: AzureWebApp@1
inputs:
azureSubscription: 'YOUR_AZURE_SUBSCRIPTION'
appType: 'webApp'
appName: 'YOUR_AZURE_APP_SERVICE_NAME'
package: '$(Build.ArtifactStagingDirectory)'
Explanation:
- This pipeline is triggered on the
main
branch. - It uses a hosted Ubuntu agent, restores dependencies, builds, and publishes your project.
- The built artifacts are then deployed to your Azure Web App.
Best Practices for CI/CD Deployments
- Automate Testing: Integrate automated tests in your pipeline to catch issues before deployment.
- Use Secrets Management: Store sensitive information such as API keys and publish profiles in secure vaults or CI/CD secret stores.
- Implement Rollbacks: Consider strategies to automatically rollback deployments if errors occur.
- Monitor Deployments: Utilize Azure Monitor and Application Insights to track the health and performance of your deployed application.
- Version Control: Tag your releases and maintain version history to facilitate easier deployments and troubleshooting.
Conclusion
Automating the deployment of your .NET 8 Minimal APIs with CI/CD pipelines on Azure can significantly enhance your development workflow and ensure reliable delivery of your applications. Whether you choose GitHub Actions or Azure Pipelines, using the best practices from this guide will help you build a robust and secure deployment process.
Happy coding, and feel free to share your thoughts or questions in the comments below!