4 min read

Tutorial: Jenkins CI/CD to deploy an ASP.NET Core application to Azure Web App service

In this blogpost, we will show you how to provision a Jenkins VM and setup a CI/CD pipeline to build an ASP.NET Core application stored in Github and deploy the application to the Azure Web App service.

We start from the solution template in Azure Marketplace since that’s the fastest and easiest path to get Jenkins up and running in Azure. You can follow the steps using your existing Jenkins server, regardless of whether it’s run on premises or in the cloud. The deployment target is the Azure Web App service. It is a platform-as-a-service (PaaS), which allows publishing web apps running on multiple frameworks and written in different programming languages (.NET, node.js, PHP, Python and Java). It is fully managed by Microsoft and easy to scale. We will demo how to use the Web App Jenkins Plugin to deploy the output binaries into it.

This is the flow implemented in this post:

  • Developers commit code change into GitHub
  • Jenkins builds the application
  • Jenkins deploys the application into Azure web app

Pre-configurations

  • Provision Azure web app service for customersapi service.
  • Provision Azure web app service for customersmvc application.
  • Provision Azure Application Insights service.
  • Configure application settings of customersapi web app. Set ApplicationInsights__InstrumentationKey to the instrumentation key of the Application Insights service.
  • Configure application settings of customersmvc web app. Set ApplicationInsights__InstrumentationKey to the instrumentation key of the Application Insights service and CustomersAPIService__Url to the URL of customersapi service.

Deploy Jenkins server

In the Azure portal, select Create a resource and search for Jenkins. Select the Jenkins offering with a publisher of Microsoft and select Create.

Enter the following information on the basics form and click OK when done.

  • Name – name for the Jenkins deployment.
  • User name – this user name is used as the admin user for the Jenkins virtual machine.
  • Authentication type – SSH public key is recommended. If selected, copy in an SSH public key to be used when logging into the Jenkins virtual machine.
  • Subscription – select an Azure subscription.
  • Resource group – create a new or select an existing resource group.
  • Location – select a location for the Jenkins server.

On the additional settings form, complete the following items:

  • Size – Select the appropriate sizing option for your Jenkins virtual machine.
  • VM disk type – Specify either HDD (hard-disk drive) or SSD (solid-state drive) for the Jenkins server.
  • Virtual network – (Optional) Select Virtual network to modify the default settings.
  • Subnets – Select Subnets, verify the information, and select OK.
  • Public IP address – Selecting the Public IP address allows you to give it a custom name, configure SKU, and assignment method.
  • Domain name label – Specify a value to create a fully qualified URL to the Jenkins virtual machine.
  • Jenkins release type – Select the desired release type from the options: LTS, Weekly build, or Azure Verified.

For Integration Settings, select No to use Jenkins host to build the jobs.

Once done with the integration settings, click OK, and then OK again on the validation summary. Click Create on the Terms of use summary. The Jenkins server takes a few minutes to deploy.

Configure Jenkins

In the Azure portal, browse to the Jenkins Resource Group, select the Jenkins virtual machine, and take note of the DNS name.

Browser to the DNS name of the Jenkins VM and copy the returned SSH string.

Open up a terminal session on your development system, and paste in the SSH string from the last step. Update ‘username’ to the username specified when deploying the Jenkins server.
Once connected, run the following command to retrieve the initial admin password.

sudo cat /var/lib/jenkins/secrets/initialAdminPassword

Install .net core 2.1

Leave the SSH session and tunnel running, and navigate to http://localhost:8080 in a browser. Paste the initial admin password into the field as seen in the following image. Select Continue when done.

Select Install suggested plugins to install all recommended Jenkins plugins.

Create a new admin user account. This account is used for logging into and working with your Jenkins instance.

Select Save and Finish when done, and then Start using Jenkins to complete the configuration.
Jenkins is now configured and ready to build and deploy code. For this example, a simple asp.net core application is used to demonstrate a Jenkins build. It can be found at https://github.com/mjrousos/AspNetCore-Sample.

Create Azure service principal

  • Go to Credentials -> System -> Global credentials -> Add Credentials
  • In Kind, select Microsoft Azure Service Principal and input your subscription information.

Create Pipeline build job and deploy to Azure web app

Now create a Jenkins build job. Select New Item, give the build project a name such as aspnetcore-webapp, select Pipeline, and click OK.

Define parameters “git_repo”, “res_group”, “customersapiapp”, “customersmvcapp”.

Define parameter “azure_cred_id”, choose Credential type Microsoft Azure Service Principal and link it to the service principal credential you just created above.

Under Pipeline, set Definition to Pipeline script and input code.

node {
    stage('Checkout git repo') {
      git branch: 'master', url: params.git_repo
    }
    stage('build and publish') {
        sh(script: "dotnet publish CustomersDemoClean-2017.sln -c Release ", returnStdout: true)
    }
    stage('deploy') {
        azureWebAppPublish azureCredentialsId: params.azure_cred_id,
            resourceGroup: params.res_group, appName: params.customersapiapp, sourceDirectory: "src/CustomersAPI/bin/Release/netcoreapp2.1/publish/"
        azureWebAppPublish azureCredentialsId: params.azure_cred_id,
            resourceGroup: params.res_group, appName: params.customersmvcapp, sourceDirectory: "src/CustomersMVC/bin/Release/netcoreapp2.1/publish/"
    }
}

Run the build job

To test the build job, manually start a build.
Select Build Now to start a build job. It takes a few seconds for the job to start, when running, you should see status similar to the following images.

Here you go…

Questions or feedback? Let us know in the comments below.