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.
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:
cd backend
.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!
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 totrue
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
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
: