You are currently viewing Release pipeline for .NET Core and EF Core application in Azure DevOps

Release pipeline for .NET Core and EF Core application in Azure DevOps

In this series of Azure DevOps, we have tried below things so far:

In this article, let’s try to setup the release pipeline in Azure DevOps. This Release pipeline will take artifacts generated by CI build. Then application will be deployed on Azure. It will also apply the database migration scripts to Azure SQL database.

Before we begin

We need to make sure that all the required Azure resources are already created. I have created a resource group with name “Samples”. Under this resource group, you can find below resources:

  • App Service Plan
  • App Service
  • Application Insights
  • SQL Database, empty database
  • SQL Server, serverless option

Please make sure you create these resources before we begin on setting up CD. You can manually create these resources or you can create ARM template which creates the resources for you. ARM templates will be very handy if you want to set up multiple environments of the same application.

New CD Pipeline

Login to Azure DevOps repository and click the menu option Releases under Pipelines. You will see a page as shown below. Now, click on New pipeline button to create new CD pipeline.

The next screen will ask you to select a predefined template of stages and tasks. Select Azure App Service deployment template as it is what we are going to do.

Next, you will be asked to enter the name of deployment stage. I named it as Deploy web app, but you can enter any name you want and then close the panel by clicking close button at top right corner.

Set up artifacts

The source is a place from where you will get the application bits to deploy. If you remember, when you set up a CI pipeline, the last step was to publish the artifacts. These artifacts are used as input to the release pipeline.

When you click on Add an artifact button, it opens a panel on right side.

Select Build as source type. Then you are also asked to enter the project name and CI pipeline name you want to use.

Let the rest of two inputs to default. The default version input is to tell that latest artifacts should be picked up for deployment. The Source alias is actually used to refer the artifacts, during different stages of the deployment.

Task to deploy web app

Once artifacts setup is done, you can see the Deploy web app stage. It will also show how many tasks are there inside the stage box as hyperlink. Click on that hyperlink.

The below screen will open. You need to select the appropriate Azure subscription under which you want to deploy the application.

TIP: Ideally, the account which you used for setting up Azure DevOps, should have an Azure subscription associated with it. If the accounts are not same for Azure DevOps and Azure, then there are some additional configurations which you will have to do, which are not discussed in this article.

Once you select Azure subscription, you will see an Authorize button on the right side. When you click on this button, the connection between your Azure DevOps organization and Azure subscription would be set up automatically.

You cannot see Authorize button in below snapshot because I already did it before setting up this pipeline.

Then you can select app type to be Web app on windows. In the next box, you can select the app service name where you want to deploy the web application.

Then click on Save button near top right corner to save the changes.

So, that’s what is required to deploy a web application as Azure App Service.

Task to deploy database

Now, let’s see how can we deploy the EF Core migrations to Azure SQL database.

On the same screen click on Add (+) button to add a new step with name Azure SQL Database deployment.

On that step you need to provide below details:

  • Name, you can customize its display name
  • Azure service connection type, set it to Azure Resource Manager
  • Azure subscription, you need to select the Azure subscription under which the database is located.
  • SQL Database details
    • Authentication type, set this to SQL Server authentication
    • Database, the name of Sql database
    • Login, the username which provided during creation of Server
    • Password, provided during creation of database server
  • Deployment package details:
    • Deploy type, set it to SQL Script
    • SQL Script, browse the migrationScripts.sql file in the migration artifacts published by CI.

Once these inputs are provided, you can save the pipeline.

Enable Continuous Delievery

We completed the steps to deploy web app and EF Core database. But we have not enabled continuous delivery yet. So click on trigger icon near the artifacts box (highlighted in yellow box on left side).

It will open right side panel. You have to enable Continuous Deployment trigger. When you enable it, the release pipeline will be automatically triggered after completion of build.

Also, you need to make sure that you deploy the contents only from your master branch and not from feature branches or merge branches.

So, under Branch filters, add new filter and set it to Include master branch build types as shown below.

Do not enable Pull Request trigger as it will lead to deployment of pull request builds, which may not be desirable for you.

Transforming Connection String

What we have done till now is sufficient to deploy a web application with database. But note, it is just deploying.

Every real world application has certain configuration settings. This configuration settings are set to some values in DEV environment. The configuration values in UAT or Production environment are different from those in DEV environment

E.g. connection string of database, connection string of cache server, connection string to FTP server, etc.

So, if these connection strings are different per environment, will you want to change it manually everytime you deploy the application ?

Certainly NOT.

In Release pipeline, you can transform such configuration settings to the desired values.

You can go to deploy webapp task and you can find JSON variable substitution section as shown below. There you an specify the files you want to modify.

Either you can specify path of JSON file or you can specify wildchars as shown in below snapshot.

Ours is .NET core application which has appsettings.json file. We want to transform the connection string to point to appropriate SQL database.

After the filename is specified, you can then go to variables tab and then specify the JSON keys which are supposed to be transformed.

Key from every level is separated by dot character.

e.g. if you have schema like this, then the Name for transforming SqlConnectionString is ConnectionStrings.SqlConnectionString.

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "SqlConnectionString": "Server=.;Database=MyFitnessLog;Trusted_Connection=True;MultipleActiveResultSets=true"
  }
}

Once this is done save the pipeline to persist your modifications.

See it working

Go to the pipelines options and trigger the CI pipeline.

You can see that when CI pipeline is done, the Release pipeline will be automatically triggered. The Release pipeline will deploy the application to Azure.

So, we have seen how to deploy a web application and entity framework code first migrations on Azure. We also have seen how easy it is to transform the configuration values as a part of CD pipeline. I hope you enjoyed this article. Let me know your thoughts.

Leave a Reply

This Post Has 3 Comments