You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Azure Pipeline部署Python Durable Function至Linux版Azure Function App

问题:Azure Pipelines部署Python Durable Function后出现500错误

我有一个基于Python 3.10编写的Durable Function,代码存储在Azure Repos中,需要构建并部署至Linux版Azure Function App。已尝试使用以下Pipeline配置部署,部署成功但调用时出现500错误,而通过VS Code手动部署则运行正常。

相关信息

  • Function App:操作系统为Linux,运行时栈为Python 3.10
  • 代码存储于Azure Repos
  • 代码结构:仓库中包含[FOLDER1_ReposReference]目录,内含函数代码文件、requirements.txt等核心文件

已配置的错误Pipeline代码

- task: UsePythonVersion@0
  displayName: 'Use Python 3.10'
  inputs:
    versionSpec: 3.10

steps:
- script: |
   python -m venv antenv
   source antenv/bin/activate
   # Upgrade pip
   python -m pip install --upgrade pip
   
   # Verify that pip is up to date
   pip --version
   
   pip install -r $(System.DefaultWorkingDirectory)/[FOLDER1_ReposReference]/requirements.txt
  displayName: 'Command Line Script'
  
steps:
- task: ArchiveFiles@2
  displayName: 'Archive $(System.DefaultWorkingDirectory)/[FOLDER1_ReposReference]'
  inputs:
    rootFolderOrFile: '$(System.DefaultWorkingDirectory)/[FOLDER1_ReposReference]'
    includeRootFolder: false
    archiveFile: '$(Build.ArtifactStagingDirectory)/durable_function.zip'
    
steps:
- task: PublishBuildArtifacts@1
  displayName: 'Publish Artifact: drop'
  inputs:
    PathtoPublish: '$(Build.ArtifactStagingDirectory)/durable_function.zip'
    
steps:
- task: AzureFunctionApp@2
  displayName: Deploy
  inputs:
    connectedServiceNameARM: 'SUBSCRIPTION_ID'
    appType: functionAppLinux
    appName: 'functionapp-name'
    package: '$(Build.ArtifactStagingDirectory)/*.zip'
    runtimeStack: 'PYTHON|3.10'

问题原因及修正方案

核心问题

  1. 重复定义steps节点:YAML中steps是任务数组,重复定义会导致仅最后一组steps执行,前面的依赖安装、打包任务未实际运行,部署包缺少依赖引发500错误。
  2. 依赖安装路径错误:原脚本将依赖安装到虚拟环境antenv,但打包时未将虚拟环境内的依赖纳入部署包,而VS Code手动部署会自动处理依赖打包逻辑。

修正后的Pipeline代码

trigger:
- main # 根据你的实际分支名调整

pool:
  vmImage: 'ubuntu-latest' # 匹配Linux版Function App的部署环境

steps:
- task: UsePythonVersion@0
  displayName: '使用Python 3.10'
  inputs:
    versionSpec: '3.10'
    addToPath: true

- script: |
    # 升级pip版本
    python -m pip install --upgrade pip
    # 将依赖安装到Azure Function识别的标准路径
    pip install --target .python_packages/lib/site-packages -r requirements.txt
  displayName: '安装Python依赖'
  workingDirectory: '$(System.DefaultWorkingDirectory)/[FOLDER1_ReposReference]'

- task: ArchiveFiles@2
  displayName: '打包函数代码'
  inputs:
    rootFolderOrFile: '$(System.DefaultWorkingDirectory)/[FOLDER1_ReposReference]'
    includeRootFolder: false
    archiveFile: '$(Build.ArtifactStagingDirectory)/durable_function.zip'

- task: PublishBuildArtifacts@1
  displayName: '发布构建产物'
  inputs:
    PathtoPublish: '$(Build.ArtifactStagingDirectory)/durable_function.zip'
    artifactName: 'drop'

- task: AzureFunctionApp@2
  displayName: '部署到Azure Function App'
  inputs:
    connectedServiceNameARM: 'SUBSCRIPTION_ID' # 替换为你的ARM服务连接名称
    appType: 'functionAppLinux'
    appName: 'functionapp-name' # 替换为你的Function App名称
    package: '$(Build.ArtifactStagingDirectory)/durable_function.zip'
    runtimeStack: 'PYTHON|3.10'
    deploymentMethod: 'zipDeploy' # 明确指定zip部署方式

关键说明

  • 所有任务统一放在单个steps数组下,确保执行顺序正确。
  • 通过--target参数将依赖安装到.python_packages/lib/site-packages,这是Azure Function Linux运行时识别依赖的标准路径,打包时会自动纳入部署包。
  • 设置workingDirectory确保依赖安装在函数代码目录内。
  • 明确deploymentMethod避免部署方式歧义。

内容的提问来源于stack exchange,提问作者Parmar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 04:32:48