/cloud_devops

An azure cloud devops repository

Primary LanguageShell

Overview

CI/CD Pipeline for A Simple Flask App on Azure

Project Plan

<TODO: Project Plan

Demo

<TODO: Screencast on YouTube> https://youtu.be/0-1XYuVfR04

Instructions

<TODO:

  • Architectural Diagram (Shows how key parts of the system work)>project_architecture

<TODO: Instructions for running the Python project. How could a user with no context run this project without asking you for any help. Include screenshots with explicit steps to create that work. Be sure to at least include the following screenshots:

Create a repository for the app code The quickest way to create an App Service instance is to use the Azure command-line interface (CLI) through the interactive Azure Cloud Shell. In the following steps, you use az webapp up to both provision the App Service and perform your first deployment of the microservice API.

  1. Sign in to the Azure portal at https://portal.azure.com.

  2. Open the Azure CLI by selecting the Cloud Shell button on the portal's toolbar: azure-cloud-shell-button

  3. The Cloud Shell appears along the bottom of the browser. Select Bash from the dropdown: azure-cloud-shell-interface

  4. In the Cloud Shell, clone this repository using git clone. As in:

git clone https://github.com//cloud_devops.git Note: To use tab indentation on Azure cloud bash shell, use ctrl+] clone or setup with ssh before clone

Provision the target Azure App Service

  1. In the Cloud Shell, change directories into the repository folder that has the Flask app, so the az webapp up command will recognize the app as Python. Then execute:

    cd cicd-for-ml-microservice-api-on-azure
  2. In the Cloud Shell, run az webapp up to create an App Service and initially deploy the API.

    Bash:

    az webapp up -n [your-appservice]

    Change [your-appservice] to a name for your app service that's unique across Azure. Typically, you use a personal or company name along with an app identifier, such as [your-name]-flaskpipelines. The app URL becomes [your-appservice].azurewebsites.net.

    When the command completes, it shows JSON output in the Cloud Shell.

    Tip

    If you encounter a "Permission denied" error with a .zip file, you may have tried to run the command from a folder that doesn't contain a Python app. The az webapp up command then tries to create a Windows app service plan, and fails.

    From the first line of output from the previous az webapp up command, copy the name of your resource group, which is similar to:

    {your-name}_rg_Linux_{your-region}
    

    Enter the following command, using your resource group name, your app service name, and your startup file or command:

    Bash

    az webapp config set -g <your-resource-group> -n <your-appservice> 

    Again, when the command completes, it shows JSON output in the Cloud Shell.

    To see the running app, open a browser and go to http://[your-appservice].azurewebsites.net. If you see a generic page, wait a few seconds for the App Service to start, and refresh the page.

Bash: Run app in a virtual environment within Azure Cloud Shell or Linux

cd ~
sudo apt-get install python3-venv  # If needed; not needed in Azure Cloud Shell
git clone https://github.com/<your-alias>/cicd-for-ml-microservice-api-on-azure
cd cicd-for-ml-microservice-api-on-azure
python3 -m venv ~/.cicd-for-ml-microservice-api-on-azure
source ~/.cicd-for-ml-microservice-api-on-azure/bin/activate
make all
az webapp up -n <your-desired-name-for-the-appservice>
az webapp config set -g <your-resource-group> -n <your-appservice-name>
export set FLASK_APP=app.py
python3 -m flask run
./make_prediction_on_azure.sh   # Make a prediction by running this script.
# To see the running app, open a browser and go to:
# http://<your-appservice-name>.azurewebsites.net

Open a browser and navigate to http://localhost:5000 to view the app. When you are finished, close the browser, and stop the Flask server with Ctrl+C.

Create an Azure DevOps project and connect to Azure

To deploy to Azure App Service from Azure Pipelines, you need to establish a service connection between the two services.

In a browser, go to dev.azure.com. If you don't yet have an account on Azure DevOps, select Start free and get a free account. If you have an account already, select Sign in to Azure DevOps.

Important

To simplify the service connection, use the same email address for Azure DevOps as you use for Azure.

Once you sign in, the browser displays your Azure DevOps dashboard, at the URL https://dev.azure.com/<your-organization-name>. An Azure DevOps account can belong to one or more organizations, which are listed on the left side of the Azure DevOps dashboard. If more than one organization is listed, select the one you want to use for this walkthrough. By default, Azure DevOps creates a new organization using the email alias you used to sign in.

A project is a grouping for boards, repositories, pipelines, and other aspects of Azure DevOps. If your organization doesn't have any projects, enter the project name Flask Pipelines under Create a project to get started, and then select Create project.

If your organization already has projects, select New project on the organization page. In the Create new project dialog box, enter the project name Flask Pipelines, and select Create.

From the new project page, select Project settings from the left navigation.

On the Project Settings page, select Pipelines > Service connections, then select New service connection, and then select Azure Resource Manager from the dropdown.

In the Add an Azure Resource Manager service connection dialog box:

Give the connection a name. Make note of the name to use later in the pipeline. For Scope level, select Subscription. Select the subscription for your App Service from the Subscription drop-down list. Under Resource Group, select your resource group from the dropdown. Make sure the option Allow all pipelines to use this connection is selected, and then select OK.

The new connection appears in the Service connections list, and is ready for Azure Pipelines to use from the project.

Note

If you need to use an Azure subscription from a different email account, follow the instructions on Create an Azure Resource Manager service connection with an existing service principal.

Create a Python-specific pipeline to deploy to App Service

  1. From your project page left navigation, select Pipelines.

  2. Select New pipeline.

  3. On the Where is your code screen, select GitHub. You may be prompted to sign into GitHub.

  4. On the Select a repository screen, select the repository that contains your app, such as your fork of the Flask app.

  5. You may be prompted to enter your GitHub password again as a confirmation, and then GitHub prompts you to install the Azure Pipelines extension. On this screen, scroll down to the Repository access section, choose whether to install the extension on all repositories or only selected ones, and then select Approve and install.

  6. On the Configure your pipeline screen, select Python to Linux Web App on Azure. Your new pipeline appears. When prompted, select the Azure subscription in which you created your Web App.

    • Select the Web App
    • Select Validate and configure

    Azure Pipelines creates an azure-pipelines.yml file that defines your CI/CD pipeline as a series of stages, Jobs, and steps, where each step contains the details for different tasks and scripts. Take a look at the pipeline to see what it does. Make sure all the default inputs are appropriate for your code.

Run the pipeline

  1. Select Save at upper right in the editor, and in the pop-up window, add a commit message and select Save.

  2. Select Run on the pipeline editor, and select Run again in the Run pipeline dialog box. Azure Pipelines queues another pipeline run, acquires an available build agent, and has that build agent run the pipeline. The pipeline takes a few minutes to complete, especially the deployment steps. You should see green checkmarks next to each of the steps.

    If there's an error, you can quickly return to the YAML editor by selecting the vertical dots at upper right and selecting Edit pipeline.

  3. From the build page, select the Azure Web App task to display its output. To visit the deployed site, hold down the Ctrl key and select the URL after App Service Application URL.

Important

If your app fails because of a missing dependency, then your requirements.txt file was not processed during deployment. This behavior happens if you created the web app directly on the portal rather than using the az webapp up command as directed in this guide.

The az webapp up command specifically sets the build action SCM_DO_BUILD_DURING_DEPLOYMENT to true. If you provisioned the app service through the portal, however, this action is not automatically set.

The following steps set the action:

Open the Azure portal, select your App Service, then select Configuration. Under the Application Settings tab, select New Application Setting. In the popup that appears, set Name to SCM_DO_BUILD_DURING_DEPLOYMENT, set Value to true, and select OK. Select Save at the top of the Configuration page. Run the pipeline again. Your dependencies should be installed during deployment.

Clean up resources

To avoid incurring ongoing charges for any Azure resources you created in this walkthrough, such as a B1 App Service Plan, delete the resource group that contains the App Service and the App Service Plan. To delete the resource group from the Azure portal, select Resource groups in the left navigation. In the resource group list, select the ... to the right of the resource group you want to delete, select Delete resource group, and follow the prompts.

You can also use az group delete in the Cloud Shell to delete resource groups.

To delete the storage account that maintains the file system for Cloud Shell, which incurs a small monthly charge, delete the resource group that begins with cloud-shell-storage-.

Enhancements

The Machine Learning model used in building this API is pre-trained. Therefore, to further generalize the implementation of this Continous Integration & Continous Deployment process, we could integrate the dataset training stage in the pipeline by employing the Azure Kubernetes Service (AKS) while leveraging relevant tools such as TensorFlow and KubeFlow to simplify the operation. In this architecture, the models will run in AKS clusters backed by GPU-enabled virtual machines.

For further information on this, see Machine Learning model training with AKS by Microsoft.

References

  • Project running on Azure App Service

ScreenshScreenshot from 2022-08-04 03-38-13 ot from 2022-08-04 16-28-10

Python application test with Github Actions