Github Actions部署Azure函数应用时依赖安装失败求助
尝试通过UI将GitHub仓库关联到Azure函数应用,构建依赖环节报错,错误日志如下:
Run pip install -r requirements.txt
pip install -r requirements.txt
shell: /usr/bin/bash -e {0}
env:
AZURE_FUNCTIONAPP_PACKAGE_PATH: .
PYTHON_VERSION: 3.10
pythonLocation: /opt/hostedtoolcache/Python/3.10.11/x64
ERROR: Could not open requirements file: [Errno 2] No such file or directory: 'requirements.txt'Notice: A new release of pip is available: 23.0.1 -> 23.1.2
Notice: To update, run: pip install --upgrade pip
Error: Process completed with exit code 1.
本人对CI/CD这类部署方式完全陌生,使用Windows系统,Python版本为3.10.6,附上GitHub Actions工作流文件:
# Docs for the Azure Web Apps Deploy action: https://github.com/azure/functions-action # More GitHub Actions for Azure: https://github.com/Azure/actions # More info on Python, GitHub Actions, and Azure Functions: https://aka.ms/python-webapps-actions name: Build and deploy Python project to Azure Function App - AutoAnalytics on: push: branches: - main workflow_dispatch: env: AZURE_FUNCTIONAPP_PACKAGE_PATH: '.' # set this to the path to your web app project, defaults to the repository root PYTHON_VERSION: '3.10' # set this to the python version to use (supports 3.6, 3.7, 3.8) jobs: build: runs-on: ubuntu-latest steps: - name: Checkout repository uses: actions/checkout@v2 - name: Setup Python version uses: actions/setup-python@v1 with: python-version: ${{ env.PYTHON_VERSION }} - name: Create and start virtual environment run: | python -m venv venv source venv/bin/activate - name: Install dependencies run: pip install -r requirements.txt # Optional: Add step to run tests here - name: Upload artifact for deployment job uses: actions/upload-artifact@v2 with: name: python-app path: | . !venv/ deploy: runs-on: ubuntu-latest needs: build environment: name: 'Production' url: ${{ steps.deploy-to-function.outputs.webapp-url }} steps: - name: Download artifact from build job uses: actions/download-artifact@v2 with: name: python-app path: . - name: 'Deploy to Azure Functions' uses: Azure/functions-action@v1 id: deploy-to-function with: app-name: 'APP_NAME' slot-name: 'Production' package: ${{ env.AZURE_FUNCTIONAPP_PACKAGE_PATH }} publish-profile: ${{ secrets.AZUREAPPSERVICE_PUBLISHPROFILE_PROFILE_PATH }} scm-do-build-during-deployment: true enable-oryx-build: true
1. 生成并提交requirements.txt文件
GitHub Actions构建时会在仓库根目录查找requirements.txt,缺失这个文件是报错的核心原因。操作步骤:
- 本地打开项目根目录的命令提示符或PowerShell
- 运行命令生成依赖清单:
pip freeze > requirements.txt - 将生成的
requirements.txt文件提交并推送到GitHub的main分支
2. 核对文件路径配置
如果你的函数代码不在仓库根目录,需要调整工作流中的路径配置:
- 修改环境变量
AZURE_FUNCTIONAPP_PACKAGE_PATH为代码所在子目录,比如代码在src文件夹下:env: AZURE_FUNCTIONAPP_PACKAGE_PATH: './src' - 同步修改依赖安装步骤的路径:
- name: Install dependencies run: pip install -r ${{ env.AZURE_FUNCTIONAPP_PACKAGE_PATH }}/requirements.txt
3. 避免重复构建冲突
工作流中同时启用了scm-do-build-during-deployment和enable-oryx-build,会导致Azure端再次触发构建,可能引发路径问题。建议关闭这两个参数,使用GitHub Actions构建好的产物直接部署:
- name: 'Deploy to Azure Functions' uses: Azure/functions-action@v1 id: deploy-to-function with: app-name: 'APP_NAME' slot-name: 'Production' package: ${{ env.AZURE_FUNCTIONAPP_PACKAGE_PATH }} publish-profile: ${{ secrets.AZUREAPPSERVICE_PUBLISHPROFILE_PROFILE_PATH }} scm-do-build-during-deployment: false enable-oryx-build: false
4. 优化虚拟环境操作(可选)
Ubuntu runner上的虚拟环境激活命令需要和依赖安装放在同一个步骤中,避免环境失效:
- name: Create virtual environment and install dependencies run: | python -m venv venv source venv/bin/activate pip install -r requirements.txt
内容的提问来源于stack exchange,提问作者Mitchell.Laferla

