Skip to content

Latest commit

 

History

History
165 lines (105 loc) · 7.93 KB

readme.md

File metadata and controls

165 lines (105 loc) · 7.93 KB
title layout sidebar permalink folder
Deploy Python application to Azure App Service using VSTS
page
vsts2
/labs/vstsextend/python/
/labs/vstsextend/python/

Last updated : {{ "now" | date: "%b %d, %Y" }}.

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 from Visual Studio Team Services. We will use with Django{:target="_blank"} framework for deployment.

Prerequisites for the lab

  1. Microsoft Azure Account: You will need a valid and active Azure account for the Azure labs. If you do not have one, you can sign up for a free trial{:target="_blank"}

    • If you are an active Visual Studio Subscriber, you are entitled for a $50-$150 credit per month. You can refer to this link{:target="_blank"} to find out more information about this including how to activate and start using your monthly Azure credit.

    • If you are not a Visual Studio Subscriber, you can sign up for the FREE Visual Studio Dev Essentials{:target="_blank"} program to create a Azure free account (includes 1 year of free services, $200 for 1st month).

  2. You will need a Visual Studio Team Services Account. If you do not have one, you can sign up for free here{:target="_blank"}

  3. You will need a Personal Access Token to set up your project using the VSTS Demo Generator. Please see this article{:target="_blank"} for instructions to create your token.

    {% include note.html content= "You should treat Personal Access Tokens like passwords. It is recommended that you save them somewhere safe so that you can re-use them for future requests." %}

Setting Up the VSTS Project

  1. Use the VSTS Demo Generator{:target="_blank"} to provision a Python project on your VSTS account.

    VSTS Demo Generator helps you create team projects on your VSTS account with sample content that include source code, work items,iterations, service endpoints, build and release definitions based on the template you choose during the configuration.

    vsts_demo

  2. Once the project is provisioned, click the URL to navigate to the project.

    python_template

Exercise 1: Endpoint Creation

Since the connections are not established during project provisioning, we will manually create the endpoints.

In VSTS, navigate to Services by clicking the gear icon, and click + New Service Endpoint. Select Azure Resource Manager. Specify connection name, select your subscription from the drop down and click OK. We use this endpoint to connect VSTS with Azure.

service_endpoint

You will be prompted to authorize this connection with Azure credentials.

{% include note.html content= "Disable pop-up blocker in your browser if you see a blank screen after clicking OK, and retry the step." %}

Exercise 2: Configure Release

We will provision the resources on Azure using ARM template in the release definition.

{% include note.html content= "You will encounter an error - TFS.WebApi.Exception: Page not found for Azure tasks in the release definition. This is due to a recent change in the VSTS Release Management API. While we are working on updating VSTS Demo Generator to resolve this issue, you can fix this by typing a random text in the Azure Subscription field and click the Refresh icon next to it. Once the field is refreshed, you can select the endpoint from the drop down." %}

  1. Go to Releases under Build and Release tab, Select release definition Python and click Edit

    releaseedit

  2. Go to Tasks and select Dev environment.

    environment

  3. Under Azure Resource Group Deployment task, update Azure subscription and Location.

    azure_sub

  4. Under Install Python Extension task, update Azure subscription.

    python_sub

  5. Under Azure App Service Deploy task, update Azure subscription and click Save.

    deploy_app

    Tasks Usage
    Azure Resource Group Deployment This task will create a resource group with the name Python and provision an App service and App Service Plan
    Install Python Extension Installs the specific version of Python into Azure App Service
    Azure App Service Deploy The task is used to update Azure App Service to deploy Web Apps to azure.

Exercise 3: Trigger CI-CD with code change

Python is an interpreted language, and hence compilation is not required. We will archive the files in the build and use the package in the release for deployment. Update the code to trigger CI-CD using Hosted build agent.

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

    {% include tip.html content= "python/app/templates/app/index.html" %}

    code_tab

  2. Go to line number 32, modify Continuous Delivery to Continuous Delivery for Python and commit the code.

    commit_code

  3. Go to Builds tab under Build and Release tab to see the build in progress.

    build

    in_progress_build

    Let's explore the build definition while the build is in-progress. The tasks used are listed as shown.

    Tasks Usage
    Archive files creates zip file for deployment
    Copy Files copies ARM template which is used to provision resources on azure
    Publish Build Artifacts publishes the build artifacts
  4. The build generates artifact which is used for deployment to Azure.

    build_result

  5. Once the build is complete, it triggers the CD pipeline. You can notice the linked release is in progress by navigating to Releases under Build and Release. The release will provision the Azure Web app and deploy the zip file generated by the associated build.

    release_in_progress

    release_succesful

  6. Login to Azure Portal{:target="_blank"} and go to the Resource Group with the name Python. You will see the resources App Service and App Service Plan.

    azure_portal

  7. Select the App Service and from the Overview tab, click Browse to see the application deployed.

    pythonapp

Summary

This lab shows how to create a continuous integration(CI) and continuous deployment (CD) pipeline for Python code with Visual Studio Team Services (VSTS) on Azure.