Home / Deploying a CD pipeline for a Django-based Python app

Overview

Python is a server-side scripting language and a powerful tool for making dynamic and interactive web pages.

This lab shows how to deploy a Python application to Azure App Service using Azure DevOps. We will be using Django framework for deployment.

Prerequisites for the lab

  1. Refer the Getting Started page to know the prerequisites for this lab.

  2. Click the Azure DevOps Demo Generator link and follow the instructions in Getting Started page to provision the project to your Azure DevOps.

Exercise 1: Examine the source code

In this lab, you will use a simple Python web application built using Django framework. Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. In this exercise, you will examine the source code provisioned by Azure DevOps Demo Generator.

  1. Navigate to the project you created above using Azure DevOps Demo Generator.

  2. Select Repos. In this repository you have

    • Application folder which has a web application developed using Python and Django
    • unit_tests folder has few unit test cases which can be executed as part of your CI pipeline
    • functional_test folder which has a Selenium test case which can be executed as part of the CD pipeline after the application deployed.

    In the following exercises, we will Build this application using Azure DevOps CI pipeline and Deploy the application to Azure App service using Azure DevOps CD pipeline.

Exercise 2: Examine the CI pipeline

Python is an interpreted language, and hence compilation is not required. In this exercise, we will run unit tests and we will archive the application files to use in the release for deployment.

  1. Navigate to Pipelines –> Builds. Select Python-CI and click Edit.

  2. Your build pipeline will look like as below. This pipeline has tasks to install dependencies of application, run unit tests, archive& publish the application into a zip file (package) which can be deployed to a web application.

  3. Select Use Python Version task. This task is used in a build or release pipeline to select a version of Python to run on an agent, and optionally add it to PATH.

  4. Select Install dependencies task. In this task, we are using Command Line task to install dependencies of the application like Django framework version etc..

  5. Select pytest task. In this task also we are using Command Line task to run Unit tests using pytest and publish the test results to test-results.xml file.

  6. Select Publish Test Results. Using this task we will publish the test results form the previous task to Azure Pipelines. The published test results will be displayed in the Tests tab in a build or release summary.

  7. Select Archive application task. Using Archive files task we are creating a zip (package) file from the application folder to use in the release pipeline.

  8. Select Archive Tests task. We have few Selenium functional tests which need to be executed after the deployment in the release pipeline. Using Archive files task we are creating a zip (package) file from Tests folder to use in the release pipeline.

  9. Select Publish Artifacts task. This task is used to publish build artifacts (Application and Tests packages from previous tasks) to Azure Pipelines.

  10. Select Triggers and Enable continuous integration trigger. A continuous integration trigger on a build pipeline indicates that the system should automatically queue a new build whenever a code change is committed. Save changes.

Exercise 3: Configure Release pipeline

In this exercise, you will configure release (CD) pipeline to create Azure resources using Azure CLI as part of your deployment and deploy the Python application to the App service provisioned.

  1. Go to Releases under Pipelines tab, select release definition Python-CD and click Edit pipeline.

  2. Select Dev stage and click View stage tasks to view the pipeline tasks.

  3. You will see the tasks as below.

  4. Select the Azure CLI task. Select the Azure subscription from the drop-down list and click Authorize to configure Azure service connection.

    In this task we are using Azure CLI commands to create Azure Resources required to deploy Python web application.

    If you observe the commands you will see few variables like $(Location), $(ResourceGroup) etc.. These are the required values to deploy resources and the values are defined in Variables section. If required you can modify this variable values as desired.

  5. Select Azure App Service Manage task. Select the service connection for the Azure Subscription where the resources are created above. Using this task we are installing Python Extension for Azure App service created in previous task to support Python web application. Make sure App Service name is set to variable $(appservice-name)

  6. Select Azure App Service Deploy task. Make sure the settings are as shown below. This task is used to deploy Application package to Azure app service which is provisioned above.

  7. Select Run Functional Tests task. Here we are using Command line task to run Selenium tests after the application deployed to the Azure app service. And the test results will be published to release summary using Publish Test Results task.

  8. Let us enable Continuous deployment trigger. Select Pipelines and click **Continuous deployment trigger option. Enable the trigger and Save the changes.

Now you have configured a release pipeline to provision required Azure resources, Deploy the web application and Run functional tests.

Exercise 4: Trigger Build and Release pipeline with a code change

In the previous exercises, we have configured build and release pipelines and enabled CI-CD. Let us update the code to trigger CI-CD.

  1. Go to Repos tab and navigate to the below path to edit the file.

    Application/app/templates/app/index.html

  2. Click on Edit and go to line number 32, modify Continuous Delivery to Continuous Delivery for Python and Commit the code.

  3. Since we have enabled CI-CD triggers a build will be queued automatically. Go to Builds, under Pipelines tab to see the build in progress.

    You would be able to see the test results under Tests tab in build summary. Once the build is complete, it triggers the CD pipeline. You can notice the linked release is in progress by navigating to Releases under Pipelines. The release will provision the Azure Web app and deploy the zip file generated by the associated build.

  4. Now go to Releases, under Pipelines tab to see the release in progress.

  5. Once the Release succeeds, login to Azure Portal and go to the Resource Group with the name Python. You will see the resources App Service and App Service Plan. Select the App Service and from the Overview tab, click on Browse to see the application deployed.

Summary

This lab shows how to create a continuous integration(CI) and continuous deployment (CD) pipeline for Python code with Azure DevOps on Azure.