DEV Community

Cristopher Coronado
Cristopher Coronado

Posted on • Updated on

Deploy .NET API to Azure App Service through Azure DevOps

In this article we are going to learn how to implement continuous integration and continuous deployment to a .NET API through Azure DevOps.

Prerequisites:

1. Create Azure SQL Database

Click in the sidebar icon and go to Resource groups.
Image description

Click on Create.
Image description

Type a name for the resource group, then click on Review + create / Create.

Image description

For this example, I used Demo-StoreCleanArchitecture for the resource group.

Now, from the side bar, go to SQL databases.
Image description

Click on Create.

Image description

Type the database name and create a server if you haven't one.

Image description

Let's create a server. Follow these steps:

  • Type a unique server name
  • From Authentication method, select Use SQL authentication
  • Type Server admin login and password, then click on Ok

Image description

From Compute + storage, click on Configure database.

Image description

As you can see, the default service tier has a lot of estimated cost per month (231.28 USD). Let's select other service tier with less cost.

Image description

Select Basic, this is for less demanding workloads. And now the cost is too low (4.99 USD).

Image description

Then, click on Review + create.

Image description

You can see a summary of all the features you are going to implement in this resource. Click on Create.

Image description

Let's wait until the database is created. Then click on set server firewall.

Image description

Let's add a rule to allow connections to this database only from our IP address. Click on Add client IP. Also enable Allow Azure services and resources to access this server. Then click on Save.

Image description

To test if we are allowed to connect to this database. Open SQL Server Management Studio. From Object Explorer, click on Connect / Database Engine.

Image description

Enter the credentials and click on Connect.

Image description

As you can see, the connection was successful.

Image description

2. Create Azure App Service

From the side bar, select App Services.

Image description

Click on Create.

Image description

Follow these steps:

  • Select the Resource group
  • Name: specify an unique name
  • Runtime stack: .NET 6 (LTS)
  • Operating System: Linux

Image description

If it's a new App Service Plan, we can change the Sku and size, clicking on Change size.

Image description

Select F1 plan from Dev/Test and click on Apply.

Image description

Then, click on Review + create.

Image description

You can see a summary with all the features of our Web App. Click on Create.

Image description

After the Web App is created. Go to the resource, in Settings section, click on Configuration.

Image description

Go to Connection strings, click on New connection string.

Image description

In the dialog, enter DefaultConnection in Name, select SQLAzure in Type and add the connection string in Value. Then click on Ok.

Server=<server_name>.database.windows.net;Database=<database_name>;User ID=<username>;Password=<password>

Image description

As you can see, the connection string was added. Click on Save.

Image description

3. Create project on Azure DevOps and create Build and Release pipelines

3.1. Update .NET application

Before to go to Azure DevOps Services, first we need to do a change in the .NET application. In the Infrasctructure project, in DependencyInjection.cs file, add the following below services.AddScoped<IProductRepository, ProductRepository>();.

var serviceProvider = services.BuildServiceProvider();
try
{
  var dbContext = serviceProvider.GetRequiredService<StoreContext>();
  dbContext.Database.Migrate();
}
catch
{
}
Enter fullscreen mode Exit fullscreen mode

This will apply all the pending migrations to the database.
Commit the changes.

3.2. Create Build Pipeline

Go to Azure DevOps Services. At the right, click on New project.

Image description

Specify a project name. Select the visibility you want. Click on Create.

Image description

Go to Pipelines, click on Pipelines.

Image description

Click on Create Pipeline.

Image description

Click on Use the classic editor.

Image description

Select GitHub. Type a connection name, then click on Authorize using OAuth.

Image description

Sign in with the credentials from the account you have the repository.

Image description

Once you authorized to connect to your GitHub account. You can link your repository.

Image description

Search the repository and click on Select.

Image description

Once the repository and branch are selected, click on Continue.

Image description

Select the ASP.NET Core template and click on Apply.

Image description

Set the pipeline name as CI and the agent specification in windows-2022.

Image description

You can change the name for the agent job as well. Click on the plus icon from Agent Job.

Image description

Select Use .NET Core and click on Add.

Image description

This will add a new task to the final. Move it to the top and set version as 6.x.

Image description

Click on the Publish task. Uncheck Publish web projects, click on Path to project(s) / Unlink.

Image description

Type **/src/Store.WebApi/*.csproj in Path to project(s).

Image description

Click on Save & queue.

Image description

You can specify a save comment, then click on Save and run.

Image description

Now, the job is running. Click on the job.

Image description

Let's wait until the job is finalized. As you can see it was successful.

Image description

If we go to the GitHub repository, we can see there is a green check mark in the last commit.

Image description

Image description

Go to the list of pipelines, click on the three dots to the right. edit.

Image description

Go to Triggers section. Enable continuous integration and click on Save.

Image description

3.3. Create Release Pipeline

From Pipelines section, click on Releases.

Image description

Click on New pipeline.

Image description

Select Azure App Service deployment template and click on Apply.

Image description

Follow these steps:

  1. Change pipeline name to Deploy to Azure
  2. Change stage name to Production
  3. Click on Add an artifact

Image description

It should be selected Build as Source type. In Source, select CI and click on Add.

Image description

Once the Artifact is added, click on the button with the lightning icon.

Image description

Enable Continuous deployment trigger.

Image description

Go to the Production stage and click on 1 job, 1 task.

Image description

Select one of your Azure subscriptions and click on Authorize.

Image description

Then, select Web App on Linux in App type. Select the app service name you created in section 2 and in startup command type dotnet Store.WebApi.dll. Click on Run on Agent.

Image description

Select windows-2022 in Agent Specification.

Image description

Scroll down and in Artifact download you can see it's selected _CI artifact and there is a folder named drop with Store.WebApi.zip file. Click on Save.

Image description

Click on Ok.

Image description

Finally. Lest's create our first release. Click on Create release.

Image description

Click on Create.

Image description

Let's wait until the Pipeline finalizes.

Image description

Image description

As you can see the Production stage was succeeded.

Image description

Let's test the api. Since we used F1 App Service plan. We have to wait some seconds for the first request of the day.

Image description

3.4. Create dashboard

We are going to create a dashboard to display the statistics for build and release pipelines.
Go to Overview section and click on Dashboards.

Image description

Click on Add a widget.

Image description

Select Deployment status and drop it to the dashboard.

Image description

Configure the widget.

Image description

Select the build pipeline, release pipeline and last column. Click on Save.

Image description

Add Build Health Details widget.

Image description

Configure the widget.

Image description

In Definition, select the build pipeline (CI).

Image description

Add Build History widget.

Image description

Configure the widget.

Image description

Select the build pipeline (CI).

Image description

Add Build Health Overview widget.

Image description

In the configuration, select the build pipeline (CI).

Image description

Once you added the four widgets, click on Done Editing at the top of the dashboard.

Image description

This is the final result. You can add all the widgets you want, but for this example, these ones are enough.

Image description

You can find the source code here.

You can watch the webinar in Spanish here.

Thanks for reading

Thank you very much for reading, I hope you found this article interesting and may be useful in the future. If you have any questions or ideas that you need to discuss, it will be a pleasure to be able to collaborate and exchange knowledge together.

Discussion (0)