How to automate Python/Flask deployment to Azure App Service

Last updated October 15, 2020

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

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.

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:
git clone https://github.com/<your-alias>/python-sample-vscode-flask-tutorial
$
cd python-sample-vscode-flask-tutorial
$
  1. Use Azure webapp up to create an App Service and make the initial deployment of your app:
az webapp up -n <your-appservice>
$

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:
az webapp config set -g <your-resource-group> -n <your-appservice> --startup-file <your-startup-file-or-command>
$

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 http://app.buddy.works/
  2. Create a new project and select the Flask project from the list.

Of course you can select a different provider than GitHub. Buddy natively supports Bitbucket and GitLab, or any private Git repository.

Creating a new projectCreating 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:

Getting detailsGetting details

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

Getting detailsGetting details

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

Getting Git clone URLGetting 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

Adding a new pipelineAdding a new pipeline

  1. Fill in the pipeline details:

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

When ready, click Add Pipeline:

Pipeline configurationPipeline configuration

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:

Adding a new actionAdding a new action

  1. Here you need to set up the deployment credentials that you downloaded from your App Service:

  2. URL: The Git URL copied before`
  3. Login: userName
  4. Password: userPWD

Action configurationAction 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.

Pipeline viewPipeline view

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

Running your first executionRunning your first execution

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

Successful executionSuccessful execution

Share

About the Author
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.

With Buddy even the most complicated CI/CD workflows take minutes to create

Sign up for Buddy CI/CD

Start a free trial

Trusted by customers from 170+ countries