actions-workflow-samples icon indicating copy to clipboard operation
actions-workflow-samples copied to clipboard

Deploy derived from template runs succesful, still ModuleNotFoundError on Azure Web App

Open erfannariman opened this issue 4 years ago • 6 comments

I use the given template for deploying to Azure Webapp through GH actions. Everything runs fine, but I still get ModuleNotFoundError when trying to access my website. My yaml:

name: My App
on:
  push:
    branches:
      - main
env:
  AZURE_WEBAPP_NAME: my-app
  AZURE_WEBAPP_PACKAGE_PATH: '.'
  PYTHON_VERSION: '3.8' # supports 3.6, 3.7, 3.8)
jobs:
  build-and-deploy-to-Azure:
    runs-on: ubuntu-latest
    steps:
    - name: 'Checkout GitHub Action'
      uses: actions/checkout@master
    - name: Setup Python ${{ env.PYTHON_VERSION }} Environment
      uses: actions/setup-python@v2
      with:
        python-version: ${{ env.PYTHON_VERSION }}
    - name: 'Install dependencies'
      run: |
        python -m pip install --upgrade pip
        git config --global url."https://${{ secrets.ACCESS_TOKEN }}@github".insteadOf https://github
        pip install -r requirements.txt
    - name: 'Deploy to Azure WebApp'
      uses: azure/webapps-deploy@v2
      with:
        app-name: ${{ env.AZURE_WEBAPP_NAME }}
        package: ${{ env.AZURE_WEBAPP_PACKAGE_PATH }}
        publish-profile: ${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE }}

But when I look in the log stream in our app, I see:

2021-02-15T16:51:47.899495269Z   File "/home/site/wwwroot/app.py", line 1, in <module>
2021-02-15T16:51:47.899499469Z     from sampling_app import app
2021-02-15T16:51:47.899503270Z   File "/home/site/wwwroot/sampling_app/__init__.py", line 2, in <module>
2021-02-15T16:51:47.899507570Z     from flask_login import LoginManager

But the module is definitely installed, when I look in the logs of GH actions:

Successfully installed Faker-6.1.1 Flask-1.1.2 Flask-Bcrypt-0.7.1 Flask-Bootstrap-3.3.7.1 Flask-Cors-3.0.10 Flask-Dropzone-1.5.4 Flask-Login-0.5.0 Flask-Mail-0.9.1 Flask-SQLAlchemy-2.4.4 Flask-WTF-0.14.3 Jinja2-2.11.3 MarkupSafe-1.1.1 SQLAlchemy-1.3.23 Six-1.15.0 WTForms-2.3.3 Werkzeug-1.0.1 bcrypt-3.2.0 blinker-1.4 cffi-1.14.5 click-7.1.2 dnspython-2.1.0 dominate-2.6.0 email-validator-1.1.2 et-xmlfile-1.0.1 idna-3.1 itsdangerous-1.1.0 jdcal-1.4.1 numpy-1.20.1 openpyxl-3.0.6 pandas-1.2.2 pycparser-2.20 pyodbc-4.0.30 python-dateutil-2.8.1 python-dotenv-0.15.0 pytz-2021.1 steekproef-0.0.1 text-unidecode-1.3 visitor-0.1.3 xlrd-2.0.1 xlsxwriter-1.3.7

What am I missing here? I hope I dont have to use azure/appservice-build@v2, because that way I am not sure how to install our pivate repo's, notice the line in our YAML:

git config --global url."https://${{ secrets.ACCESS_TOKEN }}@github".insteadOf https://github

We use an ACCES_TOKEN to access our private repos in our CD.

erfannariman avatar Feb 15 '21 16:02 erfannariman

@elliotwmsft @JasonFreeberg to help respond to this.

N-Usha avatar Feb 26 '21 10:02 N-Usha

I think you do in fact need to use the appservice-build@v2 action unfortunately. Python is a bit of a special beast because it has file-path dependencies on the server which is building your code. The appservice-build@v2 actions should handle those issues for you.

@JasonFreeberg, do you know if there is a way to run custom code in that action by chance?

ehamai avatar Feb 26 '21 16:02 ehamai

@ehamai @JasonFreeberg any chance we can follow this up? I still am stuck how to install a private repo during webapps-deploy

erfannariman avatar Mar 15 '21 13:03 erfannariman

Hi @erfannariman -- I believe the root problem here is that the Python takes dependencies on the build VM, so if you ship it to App Service then the directory structure is different and problems like this occur. This is covered in more detail here: https://azure.github.io/AppService/2020/12/11/cicd-for-python-apps.html

I would suggest setting SCM_DO_BUILD_DURING_DEPLOYMENT to 1 and setting PRE_BUILD_COMMAND to git config --global url."https://${{ secrets.ACCESS_TOKEN }}@github".insteadOf https://github

JasonFreeberg avatar Mar 15 '21 19:03 JasonFreeberg

@N-Usha @JasonFreeberg this is an old one, but this problem is still relevant since we run into it here and here. Is it possible to look for a solution? We tried many things, but the problem is still: The private repo is installed correctly in the Install dependencies step, but then in Webapps Deploy we get an error.

erfannariman avatar Jul 20 '23 19:07 erfannariman

@erfannariman Greetings,

  • Flask.py and Vue.js are Apps that are \build and \test fs. Uploaded by Machine for a second Job…except for, node_modules/ and venv/ dirs.
  • Exclude these dirs and Allow [Oryx]
  • Install /dependencies/ on Run.dll.img, like in Django. i.e.
    - name: 'Checkout GitHub Action'
      uses: actions/checkout@master
  with:
    name: [py-app](python-version: ${{ env.PYTHON_VERSION }})
    path: F
aker-6.1.1 Flask-1.1.2 Flask-Bcrypt-0.7.1 Flask-Bootstrap-3.3.7.1 Flask-Cors-3.0.10 Flask-Dropzone-1.5.4 Flask-Login-0.5.0 Flask-Mail-0.9.1 Flask-SQLAlchemy-2.4.4 Flask-WTF-0.14.3 Jinja2-2.11.3 MarkupSafe-1.1.1 SQLAlchemy-1.3.23 Six-1.15.0 WTForms-2.3.3 Werkzeug-1.0.1 bcrypt-3.2.0 blinker-1.4 cffi-1.14.5 click-7.1.2 dnspython-2.1.0 dominate-2.6.0 email-validator-1.1.2 et-xmlfile-1.0.1 idna-3.1 itsdangerous-1.1.0 jdcal-1.4.1 numpy-1.20.1 openpyxl-3.0.6 pandas-1.2.2 pycparser-2.20 pyodbc-4.0.30 python-dateutil-2.8.1 python-dotenv-0.15.0 pytz-2021.1 steekproef-0.0.1 text-unidecode-1.3 visitor-0.1.3 xlrd-2.0.1 xlsxwriter-1.3.7 | /home/site/wwwroot/app.py
      . 
      !node_modules/
      !venv/
  • 2nd Job vLoads \artifact. Logs into Azure CLI(cTM) then, Sets SCM_DO_BUILD_DURING_DEPLOYMENT flag FLASK_ENV >“production”. ...
  • Henceforth, unlike Django. /Workflow/ ▶ “startup-file” cmd $gunicorn --bind=0.0.0.0 --timeout 600 app:app Gunicorn ⮕ WSGI HTTP Server. Common for app.py | py.app.
- uses: actions/download-artifact@v2
  with:
    name: python-app
    path: .

- name: Log in to Azure CLI
  uses: azure/login@v1
  with:
    creds: ${{ secrets.AZURE_SERVICE_PRINCIPAL }}

- name: Configure deployment and runtime settings on the webapp
  run: |
    az configure --defaults ${{ env.RESOURCE_GROUP }}
    az webapp config appsettings --name ${{ env.WEBAPP_NAME }} --settings \
        SCM_DO_BUILD_DURING_DEPLOYMENT=true \
        FLASK_ENV=production

    az webapp config set --name ${{ env.WEBAPP_NAME }} \
        --startup-file "gunicorn --bind=0.0.0.0 --timeout 600 app:app"
git config --global url."https://${{ secrets.ACCESS_TOKEN }}@github".insteadOf https://github

SCM_DO_BUILD_DURING_DEPLOYMENT = 1 

PRE_BUILD_COMMAND -git --config --global 
url="https://${{ secrets.ACCESS_TOKEN }}@github"

$ \workflow

- name: Deploy to App Service
  uses: azure/webapps-deploy@v2
  with:
    app-name: ${{ env.WEBAPP_NAME}}

TheProdigyLeague avatar Oct 13 '23 01:10 TheProdigyLeague