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" }}.
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.
-
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).
-
-
You will need a Visual Studio Team Services Account. If you do not have one, you can sign up for free here{:target="_blank"}
-
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." %}
-
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.
-
Once the project is provisioned, click the URL to navigate to the project.
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.
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." %}
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." %}
-
Go to Releases under Build and Release tab, Select release definition Python and click Edit
-
Go to Tasks and select Dev environment.
-
Under Azure Resource Group Deployment task, update Azure subscription and Location.
-
Under Install Python Extension task, update Azure subscription.
-
Under Azure App Service Deploy task, update Azure subscription and click Save.
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.
-
Go to Code tab and navigate to the below path to edit the file.
{% include tip.html content= "python/app/templates/app/index.html" %}
-
Go to line number 32, modify Continuous Delivery to Continuous Delivery for Python and commit the code.
-
Go to Builds tab under Build and Release tab to see the build in progress.
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 -
The build generates artifact which is used for deployment to Azure.
-
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.
-
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.
-
Select the App Service and from the Overview tab, click Browse to see the application deployed.
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.