跳到主要内容

如何自动化部署Python/Flask至Azure应用服务

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 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 details

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

Getting 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 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 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:

Pipeline 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 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

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.

Pipeline view

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

Running your first execution

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

Successful execution