Select Sidearea

Populate the sidearea with useful widgets. It’s simple to add images, categories, latest post, social media icon links, tag clouds, and more.

Continuous deployment on Azure App services

Continuous deployment on Azure App services

Continuous deployment on Azure App services

Some time ago, we started working for a client who didn’t have continuous integration. Who cares, would you say. We at Broscorp care a lot about the speed of delivery. Our main goal is to analyze the business need, build the solution, and deliver it with the highest possible quality.
Usually, software development companies care a lot about “building the solution” phase. Yes, it’s exciting, but business pays only for something that brings benefits. A built solution is not a benefit. Built, tested, and delivered on time solution is a benefit. That’s why when we realized that our new client doesn’t have CI/CD we planned all the activities needed to achieve it.

This is what we had:

  • A set of Java Spring-based web applications packaged into wars;
  • A set of Java Spring-based backend applications packaged into jars that should run on a schedule or continuously;
  • We had a Bitbucket with pipelines;
  • We had MS Azure as a cloud provider.

Now I would like to list a set of steps needed to achieve our goal:

  • Collect the Azure App Service variables;
  • Configure the Azure App Service;
  • Setup the Bitbucket Pipelines;
  • Run Builds and Deployments;
    Check After Deployment.

1. Collect the Azure App Service variables 

The very first, we need to collect credentials and variables from Azure App Service we need for Bitbucket pipelines.

  • APP_NAME
  • RESOURCE_GROUP
  • AZURE_APP_ID
  • AZURE_PASSWORD
  • AZURE_TENANT_ID
  • AZURE_PB_USERNAME
  • AZURE_PB_PASSWORD

Now let’s go and figure out all the values for these variables:

  1. Go to the Azure portal https://portal.azure.com/#home
  2. Select the App Services.
  3. Select your app by name. 
  4. On the sidebar select Settings -> Overview.

RESOURCE_GROUP and APP_NAME (title in the top left corner) you can find on your Azure App Services app Overview page.

5. Select the Cloud Shell button on the menu bar at the upper right.

  1. Follow the instructions to open the Azure CLI.
  2. After CLI opened and loaded enter: 

az login

  1. Follow the instructions, use a web browser to open the page https://microsoft.com/devicelogin and enter the code ******** to authenticate.

After that you are logged in.

How to Get a Publishing Credentials with Azure CLI

  1. To get the AZURE_PB_USERNAME and AZURE_PB_PASSWORD you need to use the following Azure CLI command: 

az webapp deployment list-publishing-profiles –n <APP_NAME> –r <RESOURCE_GROUP>

Pick profile with publishMethod MSDeploy. Use userName and userPWD for AZURE_PB_USERNAME and AZURE_PB_PASSWORD.

How to Create a Service Principal with Azure CLI

  1. Use next CLI command in order to create the service principal:

az ad sp create-for-rbac –name

Notice that only users with Azure access role Owner can create the service principal.

Please don’t forget to save the service principal credentials shown after.

For AZURE_APP_ID, AZURE_PASSWORD and AZURE_TENANT_ID use appId, password and tenant values accordingly.

2. Configure the Azure App Service

  1. On the sidebar select Settings -> Configuration.
  2. Move to the General settings tab.

6. Set the Java version, Java container (Java SE for jar or Tomcat for war apps) and other Stack settings according to your project. You might need to change some Platform settings too. Platform settings Always on flag must be On.

  1. Switch back to the Additional settings tab.

  1. In our case we used a Spring based application with profiles. So in order to specify the correct profile we should use JAVA_OPTS variable name.

9. You might need to restart your App Service. Select the Overview on the sidebar and click on the Restart button on the top panel to restart.

3. Setup the Bitbucket Pipelines

Now let’s get back to the bitbucket and set up the pipelines. Bitbucket pipelines are powerful thing to automate everything related to deployment. You can read about pipelines here: https://support.atlassian.com/bitbucket-cloud/docs/get-started-with-bitbucket-pipelines/

To make a long story short you have to create bitbucket-pipelines.yml file in the root of the repository branch.

The admin role for the repository is needed to perform the following actions.

  1. Go to the Bitbucket https://bitbucket.org/ and select the repository.
  2. On the sidebar click on the repository Settings.

Select Pipelines -> Deployments.

pict10

4. Set up the environment (env_name) and all variables you use in the bitbucket-pipelines.yml file for this environment:

AZURE_PB_USERNAME 

AZURE_PB_PASSWORD

RESOURCE_GROUP

APP_NAME

AZURE_APP_ID

AZURE_PASSWORD

AZURE_TENANT_ID



  1. Restrict the deploy permission to repository admin only and to specific branch only if needed. So only the admin can do deployments and only from specific branches (master,  develop etc.).


Notice: Deployment to the production is a very important thing which should be avoided to happen occasionally. So it can be a good idea to restrict deployment to admins only.

6. Set up your automated build and deployment configuration by creating  bitbucket-pipelines.yml file in the root of the repository.

Web Apps

The following command stands for web app deployment via Bitbucket microsoft/azure-web-apps-deploy:1.0.2 pipe:

– pipe: microsoft/azure-web-apps-deploy:1.0.2

              variables:

                AZURE_APP_ID: $AZURE_APP_ID

                AZURE_PASSWORD: $AZURE_PASSWORD

                AZURE_TENANT_ID: $AZURE_TENANT_ID

                AZURE_RESOURCE_GROUP: $RESOURCE_GROUP

                AZURE_APP_NAME: $APP_NAME

                ZIP_FILE: ‘/opt/atlassian/pipelines/agent/build/repo_name/target/repo_zipname.zip’

All web apps should be assembled into a zip file (repo_zipname.zip)during the build.

Web Jobs

There are two types, continuous and triggered web jobs on Azure. They should be deployed into continuous and triggered folders.

The following command stands for continuous web job deployment:

curl -u ${AZURE_PB_USERNAME}:${AZURE_PB_PASSWORD} -X PUT https://${APP_NAME}.scm.azurewebsites.net/api/zip/site/wwwroot/App_Data/jobs/continuous/ –data-binary @webjob_repo_name/target/webjob_zipname.zip


For the web job in the root of the web job subrepository  create the run.cmd file.Use JAVA_OPTS variable to override the predefined Spring active profile for jar.

4. Run Builds and Deployments

  1. Go to the Bitbucket https://bitbucket.org/ and open the repository.
  1. On the sidebar select the Pipelines.

  1. To run the pipeline, click on the Run Pipeline button in the upper right.

4. Select the branch and the pipeline. For deployment, use develop branch and select custom: <env_name> pipeline if you have no other specific instructions.

pict16

  1. Click the Run button. The pipeline will start right after.

6. Deployment is a manually triggered step, so you need to click the Deploy button when the step gets available in the pipeline.

  1. On the sidebar select the Deployments to check the last deployment details.

5. Check After Deployment

  1. Select the on the Azure portal home page https://portal.azure.com/#home.
  2. Select your app by name.

Web Apps

  1. You can visit Development Tools -> App Service Editor (on the sidebar) to check or backup your app files (before and after deployment with Bitbucket Pipelines).

Web Jobs

  1. On the sidebar select Settings -> WebJobs
  2. Select your deployed web job by name. After that buttons on the upper control panel are enabled. You can Delete, Stop or check your web job Logs.

  1. On the web job Logs page you can see that it was started by your run.cmd file.

Achieved results: 

  1. Delivery time has decreased dramatically from 1 week to 15 minutes. 
  2. The current process is very stable, once created – works all the time.
  3. The previous process was very error-prone. People make mistakes all the time. Scripts never do. 

In this fast-changing environment it becomes essential to deliver results as fast as possible. Such things like continuous delivery decreases the time needed to release a new product/features to the market. If you would like to be on time with your new feature ask Broscorp on how to achieve this!

Write a comment