How to deploy flask app with database to Azure web app? Deployment is failing.

Lauren Collins 20 Reputation points
2024-07-26T22:39:27.2033333+00:00

Directory Structure Image:

Screenshot 2024-07-27 at 6.33.55 AM

Yml file:

name``: Build and deploy Python app to Azure Web App - Book-bag

on``:

push``:

branches``:

- ``azure_deploy

workflow_dispatch``:

jobs``:

build``:

runs-on``: ubuntu-latest

steps``:

- ``uses: actions/checkout@v4

- ``name: Set up Python version

uses``: actions/setup-python@v5

with``:

python-version``: '3.12'

- ``name: Cache dependencies

uses``: actions/cache@v3

with``:

path``: ~/.cache/pip

key``: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}

restore-keys``: |

${{ runner.os }}-pip-

- ``name: Create and start virtual environment

run``: |

python -m venv venv

source venv/bin/activate

- ``name: Install dependencies

run``: |

source venv/bin/activate

pip install -r requirements.txt

- ``name: Initialize database

run``: |

source venv/bin/activate

cd BookBag/

flask db init

- ``name: Migrate database

run``: |

source venv/bin/activate

cd BookBag/

flask db migrate

- ``name: Upgrade database

run``: |

source venv/bin/activate

cd BookBag/

flask db upgrade

- ``name: Run tests

run``: |

source venv/bin/activate

cd BookBag/

pytest

- ``name: Zip artifact for deployment

run``: |

zip -r release.zip . -x "venv/*"

- ``name: Upload artifact for deployment jobs

uses``: actions/upload-artifact@v4

with``:

name``: python-app

path``: release.zip

deploy``:

runs-on``: ubuntu-latest

needs``: build

environment``:

name``: 'production'

url``: ${{ steps.deploy-to-webapp.outputs.webapp-url }}

steps``:

- ``name: Download artifact from build job

uses``: actions/download-artifact@v4

with``:

name``: python-app

- ``name: Unzip artifact for deployment

run``: unzip release.zip

- ``name: 'Azure Login'

uses``: azure/login@v1

with``:

creds``: ${{ secrets.AZURE_CREDENTIALS }}

- ``name: 'Deploy to Azure Web App'

uses``: azure/webapps-deploy@v3

id``: deploy-to-webapp

with``:

app-name``: 'Book-bag'

slot-name``: 'production'

publish-profile``: ${{ <REDACTED>}}

Run Logs at point of error:

Python Virtual Environment: antenv
Creating virtual environment...
Activating virtual environment...
Running pip install...
Azure SQL Database
Azure Static Web Apps
Azure Static Web Apps
An Azure service that provides streamlined full-stack web app development.
925 questions
0 comments No comments
{count} votes

Accepted answer
  1. Amira Bedhiafi 24,531 Reputation points
    2024-07-27T16:07:54.2366667+00:00

    Your YAML configuration has syntax issues due to the backticks (`) used instead of colons (:).

    
    name: Build and deploy Python app to Azure Web App - Book-bag
    
    on:
    
      push:
    
        branches:
    
          - azure_deploy
    
      workflow_dispatch:
    
    jobs:
    
      build:
    
        runs-on: ubuntu-latest
    
        steps:
    
          - uses: actions/checkout@v4
    
          - name: Set up Python version
    
            uses: actions/setup-python@v5
    
            with:
    
              python-version: '3.12'
    
          - name: Cache dependencies
    
            uses: actions/cache@v3
    
            with:
    
              path: ~/.cache/pip
    
              key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
    
              restore-keys: |
    
                ${{ runner.os }}-pip-
    
          - name: Create and start virtual environment
    
            run: |
    
              python -m venv venv
    
              source venv/bin/activate
    
          - name: Install dependencies
    
            run: |
    
              source venv/bin/activate
    
              pip install -r requirements.txt
    
          - name: Initialize database
    
            run: |
    
              source venv/bin/activate
    
              cd BookBag/
    
              flask db init
    
          - name: Migrate database
    
            run: |
    
              source venv/bin/activate
    
              cd BookBag/
    
              flask db migrate
    
          - name: Upgrade database
    
            run: |
    
              source venv/bin/activate
    
              cd BookBag/
    
              flask db upgrade
    
          - name: Run tests
    
            run: |
    
              source venv/bin/activate
    
              cd BookBag/
    
              pytest
    
          - name: Zip artifact for deployment
    
            run: |
    
              zip -r release.zip . -x "venv/*"
    
          - name: Upload artifact for deployment jobs
    
            uses: actions/upload-artifact@v4
    
            with:
    
              name: python-app
    
              path: release.zip
    
      deploy:
    
        runs-on: ubuntu-latest
    
        needs: build
    
        environment:
    
          name: 'production'
    
          url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}
    
        steps:
    
          - name: Download artifact from build job
    
            uses: actions/download-artifact@v4
    
            with:
    
              name: python-app
    
          - name: Unzip artifact for deployment
    
            run: unzip release.zip
    
          - name: 'Azure Login'
    
            uses: azure/login@v1
    
            with:
    
              creds: ${{ secrets.AZURE_CREDENTIALS }}
    
          - name: 'Deploy to Azure Web App'
    
            uses: azure/webapps-deploy@v3
    
            id: deploy-to-webapp
    
            with:
    
              app-name: 'Book-bag'
    
              slot-name: 'production'
    
              publish-profile: ${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE }}
    
    

    You might need to configure your Flask app to connect to the Azure SQL Database. Ensure you have the correct database connection settings in your config.py or equivalent configuration file.

    Review the deployment logs for specific errors. Some common issues include incorrect database configurations or missing dependencies.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.