How to automate Python/Flask deployment to Azure App Service

How to automate Python/Flask deployment to Azure App Service

Flask is a simple Python-based web framework with a small core and no ORM. As such it is devoid of advanced features like url routing or template engine. Combined with its Pythonic structure, Flask is very easy to get started with.

This guide will focus on setting a Continuous Delivery pipeline for a Python Flask application. Enabling CI/CD in the delivery process unlocks a series of benefits:

  • deployment automation lets you immediately preview changes and reduces the risk of manual error
  • unit and integration tests allows for a rapid response to any issues
  • releasing sofware in shorter circles lets you get more feedback and faster introduce new features
Success
Upon reading this guide you will be able to automatically build a Flask project and deploy it to the Azure App service with Kudu and Buddy CI/CD.
Hint
For the purposes of this tutorial, we'll use a simple Flask project hosted on GitHub. You can fork and clone the repository here.

Deployment automation alone is worth the effort, so, without further ado, let's get down to business.

Step 1: Configuring Azure App Service

We will begin by connecting to the Azure App Service through the command-line-interface.

  1. Sign in to the Azure portal and open the Azure CLI by selecting the Cloud Shell button on the portal's toolbar
  2. In the shell, clone the repository and change the directories in the repository using the following commands:
bash
git clone https://github.com/<your-alias>/python-sample-vscode-flask-tutorial$
bash
cd python-sample-vscode-flask-tutorial$
  1. Use Azure webapp up to create an App Service and make the initial deployment of your app:
bash
az webapp up -n <your-appservice>$
Hint
your-appservice must be unique across Azure.
  1. Set the config for the App Service to use a custom startup command for the Flask Application using gunicorn:
bash
az webapp config set -g <your-resource-group> -n <your-appservice> --startup-file <your-startup-file-or-command>$
Hint
The startup file in our case will be startup.txt.
  1. Wait for the service to be up and running at http://<your-appservice>.azurewebsites.net.

Step 2: Connecting Buddy to GitHub

  1. Sign up to Buddy with your GitHub account at https://app.buddy.works/
  2. Create a new project and select the Flask project from the list.
Hint
Of course you can select a different provider than GitHub. Buddy natively supports Bitbucket and GitLab, or any private Git repository.

Image loading...Creating a new project

Getting Details from Azure App Service

  1. Go to the Azure App and click on the Deployment Center blade.
  2. Select the Local Git option and click continue:

Image loading...Getting details

  1. Select App Service build service and click the button to proceed:

Image loading...Getting details

  1. Switch to the Deployment Credentials tab and copy the Git Clone URL. We will use the URL in later steps.

Image loading...Getting Git clone URL

  1. Click Get Publish Profile to download the credentials required to authenticate Buddy in Azure.

Step 3: Connecting Buddy to Azure

In this step, we'll configure a pipeline that will push files to the App Service.

  1. Add a New Pipeline

Image loading...Adding a new pipeline

  1. Fill in the pipeline details:
  • Name: A telling name of the pipeline, e.g. 'Deploy to Azure App Service'
  • Trigger Mode: Select whether you want the pipeline to execute automatically on push, recurrently on a time interval, or manually on click
  • Branch or Tag: Enter the branch, tag, or wildcard that you want to deploy

When ready, click Add Pipeline:

Image loading...Pipeline configuration

Hint
The above configuration, will deploy to Azure App Service on every push to the master branch of our repository.
  1. Select Azure App Service from the action roster:

Image loading...Adding a new action

  1. Here you need to set up the deployment credentials that you downloaded from your App Service:
  • URL: The Git URL copied before`
  • Login: userName
  • Password: userPWD

Image loading...Action configuration

When done, click the button at the bottom to finish the configuration. Well done – Buddy is now connected to your Azure application and can take care of deployments!

Step 4: Testing deployment

With everything configured, we can now check if everything is working as expected. Depending on the selected trigger mode, make a push to your repository or click Run pipeline to deploy the application.

Image loading...Pipeline view

  1. Clicking the pipeline manually will bring up the execution box. Click Run Now to proceed:

Image loading...Running your first execution

  1. And that's it! You have successfully automated deployment to your Azure App Service.

Image loading...Successful execution

Jarek Dylewski

Jarek Dylewski

Customer Support

A journalist and an SEO specialist trying to find himself in the unforgiving world of coders. Gamer, a non-fiction literature fan and obsessive carnivore. Jarek uses his talents to convert the programming lingo into a cohesive and approachable narration.