通过GitHub Actions将Azure部署到Azure Web App时未检测到框架

92dk7w1h  于 2023-10-22  发布在  Git
关注(0)|答案(1)|浏览(147)

我正在使用Github将flask应用部署到Azure Web应用中。我通过Azure使用GitHub操作配置了CI/CD,然后通过部署中心执行这些setup instructions,它会自动在我的存储库中为我创建一个工作流。
我单独部署了我的前端,只想通过GitHub操作部署后端文件夹并部署到Azure Web应用程序(基本上只是想部署我的存储库的一部分)。
我的repo结构是这样组织的:

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

下面是我的工作流文件:

# 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 }}

在通过GitHub操作进行部署时,我能够成功构建和部署,但我在Azure Web应用程序部署日志中获得No framework detected; using default app from /opt/defaultsiteGenerating gunicorn command for 'application:app',然后应用程序无法工作(后端未正确部署)。当我通过VS代码和Azure应用服务扩展手动部署时,这种情况不会发生,相反,我会得到Detected an app based on FlaskGenerating gunicorn command for 'app:app',这是正确的,然后我的应用就可以工作了。在这个手动版本中,我可以手动选择要上传的文件夹进行部署。

我怀疑这是上传工件的东西,它无法找到我的后端文件夹(文件夹)。令人困惑的是,我为GitHub操作部署打开了调试模式,它能够找到requirements.txt,并且似乎能够在上传工件时找到我的后端文件。下面是调试日志的一个片段:

##[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

我尝试了几种不同的方法将working-directory设置为backend/,如下所示:
1.在每一步都将其设置为使用指定的路径或工作目录
1.在cd backend的所有步骤的开头添加一个步骤
1.将.deployment文件添加到我的repo的根目录中,内容如下:

[config]
project = /backend

但是所有的试验都导致了同样的问题(没有检测到框架)。
将赞赏任何想法和帮助!

polhcujo

polhcujo1#

如果您是从子文件夹部署应用程序,则需要在workflow.yml文件的package部分的'Deploy to Azure Web App'中提及您的路径。
正如在pacakage中的MS文档中所提到的,路径是从pacakage中获取的。我刚刚添加了./backend的路径。
您还需要在Web应用程序的Environment variables中添加此设置。正如MS Doc中所述。
从2020年10月起,Linux Web应用程序将需要在下载发布配置文件之前将应用程序设置WEBSITE_WEBDEPLOY_USE_SCM设置为true。今后将取消这一要求。
我的目录

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

相关问题