Deploy Web API to Azure API Management via Azure Devops CI/CD pipelines — Part 3

Marvin Heng
5 min readMar 7, 2022

This series of tutorial, which consists of 3 following parts, will be demonstrating on how to deploy ASP.NET Web API endpoints at the same time update definition in Azure API Management through Azure Devops CI/CD pipelines:

Part 1: Configuring Web API project

Part 2: Create an instance of API Management and Web API on Azure

Part 3 : Create CI/CD Pipeline on Devops

Part 3: Create CI/CD Pipeline on Devops

In the previous lessons, we learnt how to create & configure the Web API Project in order to generate swagger.json file, followed by creating instances in Azure. In this part of lesson, we will setup CI/CD pipelines on Azure Devops to deploy to Web App on Azure and at the same time update the definition in API Management.

Step 1 — Create a Build/Continuous Integration(CI) Pipeline

Step 2 — Select “Use the classic editor”

Step 3 —Choose the repository that contains your code, also choose the branch that you want to build from.

Step 4 — Now, choose ‘Empty Job’ when you are asked to select a template

Step 5 — Add all 3 following jobs to the agents and the configuration details will be mentioned in the steps 6–8.

  1. Use .NET Core
  2. .NET Core
  3. Publish build artifacts

Step 6 —Use .NET Core sdk 6.x

Step 7 — dotnet publish

Step 8 — Publish Artifact: drop

Step 9 — Enable CI Trigger to allow auto build

Step 10 — Just click to “Save & Queue” to save and start to build.

Until this point of time, we have finished setting up CI pipeline. Next, we will setup CD pipeline.

Step 11— Create Continuous Deployment (CD) on Devops: Click “New Pipeline” to start create

Step 12— Select template “Azure App Service deployment”

Step 13— Configure the Deploy Azure App Service job with the following settings

App Service Name: <select the instance we just created in part 2>
Package or folders: $(System.DefaultWorkingDirectory)/**/*.zip
XML variable substitution: true
JSON variable substitution: **/appsettings.json

Step 14 — Add PowerShell Script with the following configuration

Display name: PowerShell Script
Type: Inline
Script: New-Item -Path $(System.DefaultWorkingDirectory)/APIMDemo/drop/swagger -ItemType directory

Step 15 — Add Extract Files with the following configuration

Display name: Extract files
Archive file patterns: **/*.zip
Destination Folder: $(System.DefaultWorkingDirectory)/APIMDemo/drop/swagger
Clean destination folder before extracting: true
Overwrite existing files: true

Step 16 — Add another powershell script with the following configuration. To set the right path to retrieve swagger.json file.

Display name: PowerShell Script
Type: Inline
Script: ((Get-Content -path $(REFv1SpecificationFilePath) -Raw) -replace ‘/api/ref’,’/’) | Set-Content -Path $(REFv1SpecificationFilePath)
((Get-Content -path $(REFv1SpecificationFilePath) -Raw) -replace ‘//’,’/’) | Set-Content -Path $(REFv1SpecificationFilePath)

Step 17 — Add Azure CLI with the following configuration. The inline script to be run is to update the definition to API Management.

Display name: PowerShell Script
Script Type: PowerShell
Script Location: Inline script
Inline Script: az apim api import — path “/” — resource-group $(ResourceGroup) — specification-format OpenApi — specification-path $(REFv1SpecificationFilePath) — api-id apimdemo-api — display-name “apimdemo-api” — service-name $(ServiceName) — service-url $(ServiceUrl)

Step 17 — Set the variables with what we had created in Part 1 & 2.

REFv1SpecificationFilePath: $(System.DefaultWorkingDirectory)/APIMDemo/drop/swagger/refv1.json
ResourceGroup: apimdemo
ServiceName: apimdemo-m
ServiceUrl: https://apimdemo-m.azurewebsites.net

Step 18 — Back to Pipeline tab > Add an artifact > Add

Source (build pipeline): APIMDemo-CI
Source alias: _APIMDemo-CI

Step 18 — Click the “Lightning” icon and enable Continuous Deployment trigger

Step 19 — Click “Save” and “Create release”

Step 20 — Congratulation! Once release process is completed, lets go to Azure API Management instance > API blade > you should be able to see the new updated definition.

Conclusion

Congratulation, we have run through the steps to configure resources and CI/CD pipeline. In Part 1, we configure the Web API to generate swagger.json (rev1.json). Part 2, we have created web app instance and API Management instance. Lastly, we have learnt to setup CI/CD pipeline to deploy Web API to Azure and update the definition in API Management.

Let me know what you thought or should you have any questions!

--

--

Marvin Heng

An tech specialist also a Microsoft MVP who loves to try new things and publishes sample codes on github.