Create Pipeline To Build & Deploy Python Web Application To Azure
In, this blog article we are going to learn how to create a pipeline to build and deploy our python web application to Azure using Azure DevOps.
Firstly, let’s start with our web application. The app.py file contains the main code that will be executed by the Python interpreter to run the Flask web application. Here, we run our application as a single module, thus initialize a new Flask instance with the argument __name__ to let Flask know that it can find the HTML template folder (templates) in the same directory where it is located. Next, we are using the route decorator (@app.route(‘/’)) to specify the URL that should trigger the execution of the home function. We used the POST method to transport the form data to the server in the message body. Also, we request values of height and weight from our index.html. Our home function simply rendered the index.html HTML file, which is located in the templates folder. Here is the complete code of our app.py and index.html file:
To run our application from our local machine we use flask run command. This is how it looks after running from our local machine:
Also, use pip freeze > requiremnets.txt command to create the requirements file. This is the folder structure:
The next step is to go to Azure Developers Portal sign in to your account if you dont have then create one. Once, it is done go to Create a resource.
Here, we’ll select Web App. In Create Web App fill out all details.
Once, it is created and deployment is completed you’ll get a link that will take you to a sample page.
Now, go to Azure DevOps Portal sign in to your account. We need to create a project. Here, I have created a project called BMI Project. In our project, we are only concerned with Repos and Pipelines section.
Go to your project and into the Repos section. You’ll see you have a default repo link created for you.
We need to push our code to Azure Repo. So, copy the repo link and go to your bash terminal on your local machine. As I have already have committed to my repo, I’ll only change the URL of the repo and push everything into our Azure repo.
Once, you have done with this you’ll see all your files pushed to Azure Repo.
Now, that we have our code in the Repo, we are now basically going to deploy this python web application in Azure. In order, to do that we are going to use pipelines. Go to Pipelines > Create a Pipeline > Azure Repo Git and select your repo. For me, it’s BMI Project. Then, select Python to Linux Web App on Azure. Select the subscription, web app and then configure and validate.
A .yml file would be generated. Review the .yml file and Save And run. You get a .yml file in your repo. Similar to this:
Once, the building job is done it will proceed to the deployment stage. After, completion of both the stages you’ll get green ticks:
After getting the green ticks, go to the original URL of our Azure web app. You’ll see your web app has been successfully deployed on Azure.
Hence, we created a pipeline to deploy our python web application using Microsoft Azure.
Here is the GitHub Repo link of Code: https://github.com/sharur7/BMI-Calculator.git
Hope you like it!
Thank You for reading!
Connect with me on:
Twitter: https://twitter.com/aree_yarr_sharu
Linkedin: https://t.co/g0A8rcvcYo?amp=1