0

I'm using Github to deploy a flask app into an Azure web app. I configured CI/CD with GitHub Actions through Azure following these setup instructions through the deployment centre which automatically created a workflow for me in my repo.

I have my frontend deployed separately and only want to deploy the backend folder via GitHub Actions and to the Azure web app (basically just want to deploy a subdirectory of my repo).

My repo structure is organized like so:

my-repo
├── .github/workflows       # workflow file for GH actions
├── backend                 # my flask app that I want to deploy
│   ├── requirements.txt          
│   └── ...
└── ...                     # other files and frontend stuff I don't want deployed

Here is my workflow file:

# Docs for the Azure Web Apps Deploy action: https://github.com/Azure/webapps-deploy
# More GitHub Actions for Azure: https://github.com/Azure/actions
# More info on Python, GitHub Actions, and Azure App Service: https://aka.ms/python-webapps-actions

name: Build and deploy Python app to Azure Web App - xxxxxxxxxxxxxxxx

on:
  push:
    branches:
      - main
  workflow_dispatch:

jobs:

  build:
    runs-on: ubuntu-latest
    env:
      working_directory: "backend/"

    steps:
      - uses: actions/checkout@v2

      - name: Set up Python version
        uses: actions/setup-python@v1
        with:
          python-version: '3.11'

      - name: Create and start virtual environment
        run: |
          python -m venv venv
          source venv/bin/activate
        working-directory: "backend/"
      
      - name: Install dependencies
        run: pip install -r requirements.txt
        working-directory: "backend/"
        
      # Optional: Add step to run tests here (PyTest, Django test suites, etc.)
      
      - name: Upload artifact for deployment jobs
        uses: actions/upload-artifact@v3
        with:
          working_directory: "backend/"
          name: python-app
          path: |
            ./backend/
            !venv/

  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@v2
        with:
          name: python-app
          path: ./backend/
          
      - name: 'Deploy to Azure Web App'
        uses: azure/webapps-deploy@v2
        id: deploy-to-webapp
        with:
          app-name: 'xxxxxxxxxxxxxxxx'
          slot-name: 'Production'
          publish-profile: ${{ secrets.AZUREAPPSERVICE_PUBLISHPROFILE_xxxxxxxxxxxxxxxxxxxxxxxxxxx }}

Upon deployment via GitHub Actions I am successfully able to build and deploy but I get No framework detected; using default app from /opt/defaultsite and Generating gunicorn command for 'application:app' on my Azure web app deployment logs, and then the app does not work (backend is not properly deployed). When I deploy manually through VS code and the Azure App Service extension this does not happen and instead I get Detected an app based on Flask and Generating gunicorn command for 'app:app' which is correct and then my app works. In this manual version I'm able to manually select which folder to upload for deployment.

Azure web app logs

I suspect it is something with uploading artifacts where it's unable to find my backend folder (the subdirectory). What's confusing is that I turned debug mode on for GitHub actions deployment and it's able to find the requirements.txt and seems to be able to find my backend files when uploading artifacts. Here is a snippet from the debug logs:

##[debug]File:/home/runner/work/xxxxxxxxxxxxxxxx/xxxxxxxxxxxxxxxx/backend/venv/lib/python3.11/site-packages/pip/_internal/commands/__pycache__/hash.cpython-311.pyc was found using the provided searchPath

I've tried a couple different ways to set the working-directory as backend/ like:

  1. Setting it at each step to use the path or working-directory specified
  2. Adding in a step at the beginning of all steps to cd backend
  3. Adding a .deployment file into the root of my repo with this content:
[config]
project = /backend

But all trials have resulted in the same issue (no framework detected).

Would appreciate any ideas and help!

2
  • Not sure if it's directly related to your issue; but, in the build job, you need to run source venv/bin/activate again before pip install. Each run is a separate shell. An activated venv won't be activated in the next one automatically. You may combine both steps if that's fine with you. Commented Oct 4, 2023 at 3:49
  • 1
    @Azeem Gotcha! just added source venv/bin/activate && pip install -r requirements.txt to the Install dependencies step, made the build time a lot longer but unfortunately still had the same "No framework detected" log. It's weird that the Azure default workflow didn't have that step set at default. Commented Oct 4, 2023 at 15:25

1 Answer 1

1

If you are deploying app from a sub folder, you need to mention your path in the 'Deploy to Azure Web App' in the package section in your workflow.yml file.

As Mentioned in the MS Document in pacakage path is taken from the environemt. I have just added the path to ./backend.

Also you need to add this setting in the Environment variables of your web app. As mentioned in MS Doc.

As of October 2020, Linux web apps will need the app setting WEBSITE_WEBDEPLOY_USE_SCM set to true before downloading the publish profile. This requirement will be removed in the future.

My directory

my-repo
|--.github/workflows
|   |--main_flaskappbackend.yml
|--backend
    |--app.py
    |--requirements.txt
|--frontend

enter image description here

main_flaskappbackend.yml:

# Docs for the Azure Web Apps Deploy action: https://github.com/Azure/webapps-deploy
# More GitHub Actions for Azure: https://github.com/Azure/actions
# More info on Python, GitHub Actions, and Azure App Service: https://aka.ms/python-webapps-actions

name: Build and deploy Python app to Azure Web App - flaskappbackend

on:
  push:
    branches:
      - main
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2

      - name: Set up Python version
        uses: actions/setup-python@v1
        with:
          python-version: '3.11'

      - name: Create and start virtual environment and Install Dependencies
        run: |
          python -m venv venv
          source venv/bin/activate
        working-directory: ./backend

      - name: Install dependencies
        run: pip install -r requirements.txt
        working-directory: ./backend
      
      
      - name: Upload artifact for deployment jobs
        uses: actions/upload-artifact@v2
        with:
          name: python-app
          path: |
            ./backend 
            !venv/

  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@v2
        with:
          name: python-app
          path: ./backend


      - name: 'Deploy to Azure Web App'
        uses: azure/webapps-deploy@v2
        id: deploy-to-webapp
        with:
          app-name: 'flaskappbackend'
          slot-name: 'Production'
          publish-profile: ${{ secrets.AZUREAPPSERVICE_PUBLISHPROFILE_4A1C3ED9993843D4A87E0CCFAB61604D }}
          package: ./backend
          

app.py:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello():
  return "hello! this is a flask app"

if __name__=='__main__':
  app.run()

Output:

enter image description here

enter image description here

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! This worked perfectly!!! I think the special env variable and path to backend in my Deploy to Azure Web App step was what I was missing! :) I did however have to update my path and working directory to backend/ instead of what you had ./backend. When I had ./backend I got a deployment error of Deployment Failed, Package deployment using ZIP Deploy failed. Refer logs for more details.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.