Building a CI/CD Pipeline Using GitHub Actions for .NET Applications in Azure
Victor Karabedyants
MSDP in Software Engineering, CTO, MBA, Cloud Manager at Sitecore | AI Engineer | Azure Solutions Architect | Azure Administrator | Azure Security Engineer | Azure Developer | Azure Data Engineer and Devops| CKA
Introduction
In this guide, we will build a Continuous Integration and Continuous Deployment (CI/CD) pipeline from scratch using GitHub Actions. This pipeline will be used to deploy an ASP.NET Core 9 Web API application to Microsoft Azure. Follow the steps below to implement a fully automated deployment process.
Step 1: Setting Up the ASP.NET Core 9 Web API
The application we are deploying is a simple ASP.NET Core 9 Web API with a single GET endpoint. This endpoint returns an object containing the current UTC time and the respective time zone.
Additionally, we will expose an OpenAPI specification using the Swashbuckle UI. Running the application locally and navigating to localhost/swagger will display the API documentation.
Step 2: Creating an Azure App Service
To deploy our application, we need to create an Azure App Service. Follow these steps:
?
Select Code deployment and choose .NET 9 as the runtime stack.
Select a pricing tier. The Free Tier (F1) is suitable for demonstrations, but the Basic B1 tier ensures continuous uptime.
Step 3: Enabling Basic Publishing Credentials
Since we will use a Publish Profile for deployment:
Under General Settings, enable SCM_BASIC_AUTH_PUBLISHING_CREDENTIALS.
Ensure this file is kept secure, as it contains sensitive credentials.
Step 4: Setting Up GitHub Actions for CI/CD
To define a CI/CD pipeline in GitHub Actions:
Define the following workflow:
Workflow Structure
name: Time Service CI/CD
?
on:
? push:
??? branches:
????? - main
? workflow_dispatch:
?
env:
? AZURE_WEBAPP_NAME: "timeservice"
? DOTNET_VERSION: "9.0"
? PUBLISH_DIR: "timeAPI/publish"
?
jobs:
? build-and-test:
??? runs-on: ubuntu-latest
??? steps:
????? - name: Checkout Repository
??????? uses: actions/checkout@v4
?????
????? - name: Setup .NET
??????? uses: actions/setup-dotnet@v3
??????? with:
????????? dotnet-version: ${{ env.DOTNET_VERSION }}
?????
????? - name: Restore Dependencies
??????? run: dotnet restore timeAPI.sln
?????
????? - name: Build Application
??????? run: dotnet build timeAPI.sln --configuration Release --no-restore
?????
????? - name: Run Tests
??????? run: dotnet test timeAPI.sln --configuration Release --no-restore --no-build
?????
????? - name: Publish Application
??????? run: dotnet publish timeAPI -c Release -o ${{ env.PUBLISH_DIR }} --no-build
?????
????? - name: Upload Artifact
??????? uses: actions/upload-artifact@v4
??????? with:
????????? name: webapp
????????? path: ${{ env.PUBLISH_DIR }}
?
? deploy:
??? runs-on: ubuntu-latest
??? needs: build-and-test
??? steps:
????? - name: Download Artifact
??????? uses: actions/download-artifact@v4
??????? with:
????????? name: webapp
????????? path: ${{ env.PUBLISH_DIR }}
?????
????? - name: Deploy to Azure Web App
??????? uses: azure/webapps-deploy@v2
??????? with:
????????? app-name: ${{ env.AZURE_WEBAPP_NAME }}
????????? publish-profile: ${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE }}
????????? package: ${{ env.PUBLISH_DIR }}
Step 5: Adding Secrets in GitHub
Click Add secret.
Step 6: Running the CI/CD Pipeline
Conclusion
You have successfully built and deployed an ASP.NET Core 9 Web API to Azure using GitHub Actions. This CI/CD pipeline automates the deployment process, ensuring a seamless integration and delivery workflow.
For further improvements, consider adding:
To learn more about deploying .NET applications using containers, check out our next guide!
?