GitHub Actions部署Azure Function因WEBSITE_RUN_FROM_PACKAGE=1崩溃求助
问题描述
向dev分支推送代码时,部署Azure Function的GitHub Action始终失败。目前仅能通过以下手动步骤解决:
- 进入Azure函数应用>配置>应用设置
- 删除
WEBSITE_RUN_FROM_PACKAGE=1配置项 - 重新运行GitHub Actions所有任务(此时部署可成功)
- 重新添加
WEBSITE_RUN_FROM_PACKAGE=1配置项,否则NodeJS项目使用msal时会陷入登录循环
注:项目拥有dev和prod两个环境。
GitHub Action失败日志
Run Azure/functions-action@v1.3.2 with: app-name: myapi package: ./functionsApi publish-profile: *** slot-name: dev scm-do-build-during-deployment: true enable-oryx-build: false respect-pom-xml: false respect-funcignore: false env: AZURE_FUNCTIONAPP_NAME: myapi AZURE_FUNCTIONAPP_SLOT: dev AZURE_FUNCTIONAPP_PACKAGE_PATH: ./functionsApi AZURE_FUNCTIONAPP_PUBLISH_PROFILE: *** NODE_VERSION: 16.x Successfully parsed SCM credential from old publish-profile format. Using SCM credential for authentication, GitHub Action will not perform resource validation. Sucessfully acquired app settings from function app (with SCM credential)! Will archive ./functionsApi into D:\a\_temp\temp_web_package_5053591759469989.zip as function app content Will use Kudu https://<scmsite>/api/zipdeploy to deploy since publish-profile is detected. Setting SCM_DO_BUILD_DURING_DEPLOYMENT in Kudu container to true Update using Client.updateAppSettingViaKudu Response with status code 204 App setting SCM_DO_BUILD_DURING_DEPLOYMENT propagated to Kudu container Setting ENABLE_ORYX_BUILD in Kudu container to false Update using Client.updateAppSettingViaKudu Response with status code 204 App setting ENABLE_ORYX_BUILD propagated to Kudu container Package deployment using ZIP Deploy initiated. Error: Failed to deploy web package to App Service. Successfully updated deployment History at https://REMOVEDpi-dev.scm.azurewebsites.net/api/deployments/d3799f4XXX933eaa14f3663f3ebdfa67f9732d7b175b1690836786357 Error: Execution Exception (state: PublishContent) (step: Invocation) Error: When request Azure resource at PublishContent, zipDeploy : Failed to use D:\a\_temp\temp_web_package_5053591759469989.zip as ZipDeploy content Error: Failed to deploy web package to App Service. Internal Server Error (CODE: 500) Error: Error: Failed to deploy web package to App Service. Internal Server Error (CODE: 500) at Kudu.<anonymous> (D:\a\_actions\Azure\functions-action\v1.3.2\node_modules\azure-actions-appservice-rest\Kudu\azure-app-kudu-service.js:175:23) at Generator.next (<anonymous>) at fulfilled (D:\a\_actions\Azure\functions-action\v1.3.2\node_modules\azure-actions-appservice-rest\Kudu\azure-app-kudu-service.js:5:58) at runMicrotasks (<anonymous>) at processTicksAndRejections (node:internal/process/task_queues:96:5) Error: Deployment Failed!
工作流文件内容
路径:myapp/.github/workflows/dev.node.js-functionapp-on-azure.yml
name: (DEV) Deploy Node.js project to Azure Function App on: push: branches: - dev env: AZURE_FUNCTIONAPP_NAME: myapp AZURE_FUNCTIONAPP_SLOT: dev AZURE_FUNCTIONAPP_PACKAGE_PATH: "./functionsApi" AZURE_FUNCTIONAPP_PUBLISH_PROFILE: "${{ secrets.AZURE_FUNCTIONAPP_PUBLISH_PROFILE_DEV }}" NODE_VERSION: "16.x" # set this to the node version to use (supports 8.x, 10.x, 12.x) jobs: build-and-deploy: runs-on: windows-latest environment: dev steps: - name: "Checkout GitHub Action" uses: actions/checkout@master - name: "Log env variables" run: | echo "AZURE_FUNCTIONAPP_NAME: ${{ env.AZURE_FUNCTIONAPP_NAME }}}" echo "AZURE_FUNCTIONAPP_SLOT: ${{ env.AZURE_FUNCTIONAPP_SLOT }}}" echo "GITHUB_WORKSPACE: ${{ github.workspace }}}" - name: Setup Node ${{ env.NODE_VERSION }} Environment uses: actions/setup-node@v1 with: node-version: ${{ env.NODE_VERSION }} - name: "Resolve Project Dependencies Using Npm" shell: pwsh run: | pushd './${{ env.AZURE_FUNCTIONAPP_PACKAGE_PATH }}' npm install --force npm run build --if-present popd - name: "Run Azure Functions Action" uses: Azure/functions-action@v1.3.2 id: fa with: app-name: ${{ env.AZURE_FUNCTIONAPP_NAME }} package: ${{ env.AZURE_FUNCTIONAPP_PACKAGE_PATH }} publish-profile: ${{ env.AZURE_FUNCTIONAPP_PUBLISH_PROFILE }} slot-name: ${{ env.AZURE_FUNCTIONAPP_SLOT }} scm-do-build-during-deployment: true enable-oryx-build: false # For more samples to get started with GitHub Action workflows to deploy to Azure, refer to https://github.com/Azure/actions-workflow-samples
配置截图

解决方案
自动化手动修复流程
既然删除再恢复WEBSITE_RUN_FROM_PACKAGE能解决问题,可直接在工作流中加入自动处理步骤,替代手动操作:
- 添加Azure登录与配置项操作步骤:使用Azure CLI在部署前后分别移除和恢复目标配置项,需提前在GitHub Secrets中存储Azure服务主体凭据和订阅ID。
修改后的工作流示例:
name: (DEV) Deploy Node.js project to Azure Function App on: push: branches: - dev env: AZURE_FUNCTIONAPP_NAME: myapp AZURE_FUNCTIONAPP_SLOT: dev AZURE_FUNCTIONAPP_PACKAGE_PATH: "./functionsApi" AZURE_FUNCTIONAPP_PUBLISH_PROFILE: "${{ secrets.AZURE_FUNCTIONAPP_PUBLISH_PROFILE_DEV }}" NODE_VERSION: "16.x" AZURE_SUBSCRIPTION_ID: "${{ secrets.AZURE_SUBSCRIPTION_ID }}" RESOURCE_GROUP_NAME: "你的资源组名称" # 替换为实际资源组名 jobs: build-and-deploy: runs-on: windows-latest environment: dev steps: - name: "Checkout GitHub Action" uses: actions/checkout@master - name: "Log env variables" run: | echo "AZURE_FUNCTIONAPP_NAME: ${{ env.AZURE_FUNCTIONAPP_NAME }}}" echo "AZURE_FUNCTIONAPP_SLOT: ${{ env.AZURE_FUNCTIONAPP_SLOT }}}" echo "GITHUB_WORKSPACE: ${{ github.workspace }}}" - name: Setup Node ${{ env.NODE_VERSION }} Environment uses: actions/setup-node@v1 with: node-version: ${{ env.NODE_VERSION }} - name: "Resolve Project Dependencies Using Npm" shell: pwsh run: | pushd './${{ env.AZURE_FUNCTIONAPP_PACKAGE_PATH }}' npm install --force npm run build --if-present popd # 登录Azure并移除WEBSITE_RUN_FROM_PACKAGE配置 - name: Azure Login uses: azure/login@v1 with: creds: ${{ secrets.AZURE_CREDENTIALS }} subscription-id: ${{ env.AZURE_SUBSCRIPTION_ID }} - name: Remove WEBSITE_RUN_FROM_PACKAGE setting run: | az functionapp config appsettings delete --name ${{ env.AZURE_FUNCTIONAPP_NAME }} --slot ${{ env.AZURE_FUNCTIONAPP_SLOT }} --settings WEBSITE_RUN_FROM_PACKAGE --resource-group ${{ env.RESOURCE_GROUP_NAME }} - name: "Run Azure Functions Action" uses: Azure/functions-action@v1.3.2 id: fa with: app-name: ${{ env.AZURE_FUNCTIONAPP_NAME }} package: ${{ env.AZURE_FUNCTIONAPP_PACKAGE_PATH }} publish-profile: ${{ env.AZURE_FUNCTIONAPP_PUBLISH_PROFILE }} slot-name: ${{ env.AZURE_FUNCTIONAPP_SLOT }} scm-do-build-during-deployment: true enable-oryx-build: false # 恢复WEBSITE_RUN_FROM_PACKAGE配置 - name: Restore WEBSITE_RUN_FROM_PACKAGE setting run: | az functionapp config appsettings set --name ${{ env.AZURE_FUNCTIONAPP_NAME }} --slot ${{ env.AZURE_FUNCTIONAPP_SLOT }} --settings WEBSITE_RUN_FROM_PACKAGE=1 --resource-group ${{ env.RESOURCE_GROUP_NAME }}
其他排查优化方向
- 升级Action版本:当前使用的
Azure/functions-action@v1.3.2版本较旧,尝试升级到最新稳定版(如v4),新版本可能修复了ZIP Deploy与WEBSITE_RUN_FROM_PACKAGE共存的兼容性问题。 - 查看Kudu详细日志:登录
https://<你的函数应用名>-dev.scm.azurewebsites.net,查看部署时的Kudu日志,定位500错误的具体触发原因(如文件权限冲突、存储空间不足等)。 - 验证包完整性:在工作流中添加ZIP包校验步骤,确保打包后的文件无损坏,避免因包异常导致部署失败。
内容的提问来源于stack exchange,提问作者wazowski
相关产品推荐
相关产品推荐

