Deploy a Python (Django or Flask) web app to Azure App Service Using Azure DevOps CI/CD

To deploy a Python web application (Django or Flask) to Azure App Service using Azure DevOps CI/CD, you can follow these steps. Below is a complete, end-to-end example using Azure DevOps.

Here is the general architecture diagram that we will be deploying.


Prerequisites:

  • Azure account with access to App Service and Azure DevOps
  • Azure DevOps account with a project and repository to store your code
  • Python web application (Flask/Django) ready for deployment
  • Azure CLI installed and logged in

Step 1: Prepare Your Python Web Application

Make sure your Flask or Django application is ready with the following essentials:

  1. requirements.txt: List of all Python dependencies.

    • Example for a Flask app:
      flask
      gunicorn
  2. Procfile: This tells Azure how to run your application.

    • For Flask:
      web: gunicorn app:app
  3. app.py (for Flask) or manage.py (for Django) to run the application locally.

  4. runtime.txt: Define the Python version (optional, but recommended).

    • Example:
      python-3.8

Step 2: Create Azure App Service

  1. Log into the Azure portal.
  2. Navigate to App Services and create a new App Service instance.
  3. Configure the App Service with the necessary settings (e.g., name, region, and resource group).
  4. Once the App Service is created, take note of the App Service name and resource group.

Step 3: Set Up Azure DevOps Repository

  1. Create a new Azure DevOps project if you don't have one already.
  2. Push your Python web app code (Flask/Django) to the Azure DevOps repository.

Step 4: Set Up Continuous Integration (CI) Pipeline

  1. In Azure DevOps, go to your Project > Pipelines > New Pipeline.
  2. Choose Azure Repos Git as the source for your repository.
  3. Select the appropriate repository.
  4. Choose Python as the template. You can either start with the YAML configuration or the classic editor.
  5. In the pipeline YAML (azure-pipelines.yml), define the steps to:
    • Install dependencies
    • Run tests (optional)
    • Build the app
    • Deploy to Azure

Example azure-pipelines.yml for Flask:

trigger:
  branches:
    include:
      - main

pool:
  vmImage: 'ubuntu-latest'

steps:
  - task: UsePythonVersion@0
    inputs:
      versionSpec: '3.x'
      addToPath: true

  - checkout: self

  - script: |
      python -m venv venv
      source venv/bin/activate
      pip install -r requirements.txt
    displayName: 'Install dependencies'

  - script: |
      python -m pytest
    displayName: 'Run tests'

  - task: AzureWebApp@1
    inputs:
      azureSubscription: '<your-service-connection>'
      appName: '<your-app-service-name>'
      package: $(System.DefaultWorkingDirectory)/
    displayName: 'Deploy to Azure App Service'

Step 5: Set Up Continuous Deployment (CD) Pipeline

  1. Configure the Azure DevOps Service Connection:

    • In Azure DevOps, navigate to Project Settings > Service connections.
    • Add a new Azure Resource Manager service connection, providing authentication details (use service principal).
  2. Deploy to Azure:

    • In your CI pipeline, add the AzureWebApp@1 task to deploy the application to Azure App Service.
    • Make sure the azureSubscription is set to the Azure DevOps service connection you just created.
    • Set appName to the name of your App Service.

Step 6: Commit and Push Code to Trigger CI/CD

  1. Commit your changes and push them to the repository (e.g., pushing to the main branch).
  2. The pipeline will automatically trigger based on the defined trigger conditions.
  3. After the pipeline finishes building and testing, it will deploy your app to Azure App Service.

Step 7: Monitor the Deployment

  1. Once deployed, go to the Azure portal, navigate to App Services, and select your App Service.
  2. You can monitor logs, view the deployed app URL, and check for errors.

Step 8: Verify the Deployment

  • After the deployment is complete, navigate to the App Service's URL (e.g., https://<your-app-name>.azurewebsites.net).
  • Your Python web app should be live!

Troubleshooting

  • If you encounter deployment issues, check the Azure DevOps pipeline logs.
  • Ensure all dependencies are correctly listed in requirements.txt.
  • Make sure the correct Python version is being used in both the local environment and Azure.

This is a complete process for deploying a Python web app (Django or Flask) to Azure App Service using Azure DevOps CI/CD. You can modify this pipeline to add more features like testing, linting, or notifications as needed.

🌟 Master Microsoft Azure with Microsoft Azure in Action! ðŸŒŸ

Dive into the world of cloud computing and supercharge your skills! This practical guide is packed with step-by-step tutorials, real-world use cases, and the latest Azure features to help you build, deploy, and manage scalable cloud apps like a pro. 🚀

🔥 Exclusive Deal Alert! Unlock amazing savings of 34.04% today! 🎉 Don’t miss this chance to learn Azure while saving big.

👇 Click now to claim your discount and start your Azure journey! ðŸ‘‡
👉 Grab Your 34% Discount Now!

Hurry—this offer won't last forever! ⏳

Comments

Popular posts from this blog

Spring Boot OpenAI Integration: Step-by-Step Guide

Orchestration-Based Saga Architecture and Spring Boot Microservices Implementation Guide

Spring Boot 3 + Angular 15 + Material - Full Stack CRUD Application Example