gpt4 book ai didi

python - 不使用 VS Code 扩展的 Azure Function 部署

转载 作者:行者123 更新时间:2023-12-02 08:11:04 24 4
gpt4 key购买 nike

我正在尝试部署 Azure Function 来执行简单的计算。它已成功部署,因为我可以看到您的 Functions 4.0 应用程序已启动并正在运行。我的逻辑( inspired by this )设置如下。

import azure.functions as func
app = func.FunctionApp(http_auth_level=func.AuthLevel.ANONYMOUS)
@app.route(route="addition")
def main(req: func.HttpRequest) -> str:
a = req.params.get("a")
b = req.params.get("b")
return "{}".format(int(a) * int(b))

当我导航到 /api/addition?a=2&b=3 时,我没有看到预期的结果。相反,我得到 404 Not Found。

我尝试使用 VS Code Azure Function extension 进行部署。成功了,我可以在屏幕上确认预期的结果。不过我想在没有 VS Code 的情况下完成它。我缺少什么?谷歌搜索它是具有挑战性的,因为每个人都问如何 VS Code 中做到这一点。

buildpipe 的编译大致如下。

- task: UsePythonVersion@0
inputs:
versionSpec: $(pythonVersion)
disableDownloadFromRegistry: true
addToPath: true
architecture: 'x64'
displayName: 'Use python $(pythonVersion)'
- script: |
python -m venv antenv
source antenv/bin/activate
pip install -r requirements.txt
workingDirectory: $(projectRoot)
displayName: 'Install requirements'

- task: ArchiveFiles@2
inputs:
rootFolderOrFile: '$(projectRoot)'
includeRootFolder: false
archiveType: 'zip'
archiveFile: '$(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip'
replaceExistingArchive: true

- publish: '$(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip'
artifact: drop

然后,它就这样部署了。

- task: UsePythonVersion@0
inputs:
versionSpec: $(pythonVersion)
disableDownloadFromRegistry: true
addToPath: true
architecture: 'x64'
displayName: 'Use python $(pythonVersion)'

- task: AzureFunctionApp@2
inputs:
azureSubscription: $(azureSubscription)
appType: 'functionAppLinux'
appName: 'func-calculationtest'
package: '$(Pipeline.Workspace)/drop/$(Build.BuildId).zip'
runtimeStack: 'PYTHON|3.10'
deploymentMethod: 'auto'

最佳答案

我使用您的代码创建了一个 Azure Functions PythonV2 Http 触发器,该触发器执行简单的添加,将代码推送到 Azure 存储库并通过 Azure DevOps 将其部署到 Function 应用程序,如下所示:-

从 DevOps yaml 模板中选择以下选项并部署您的 Function 应用:-

enter image description here

Yaml 代码:-

trigger:
- master

variables:

azureSubscription: 'correct-serviceconnection'


functionAppName: 'siliconfunc58'


vmImageName: 'ubuntu-latest'


workingDirectory: '$(System.DefaultWorkingDirectory)'

stages:
- stage: Build
displayName: Build stage

jobs:
- job: Build
displayName: Build
pool:
vmImage: $(vmImageName)

steps:
- bash: |
if [ -f extensions.csproj ]
then
dotnet build extensions.csproj --runtime ubuntu.16.04-x64 --output ./bin
fi
workingDirectory: $(workingDirectory)
displayName: 'Build extensions'

- task: UsePythonVersion@0
displayName: 'Use Python 3.6'
inputs:
versionSpec: 3.10 # Functions V2 supports Python 3.6 as of today

- bash: |
pip install --target="./.python_packages/lib/site-packages" -r ./requirements.txt
workingDirectory: $(workingDirectory)
displayName: 'Install application dependencies'

- task: ArchiveFiles@2
displayName: 'Archive files'
inputs:
rootFolderOrFile: '$(workingDirectory)'
includeRootFolder: false
archiveType: zip
archiveFile: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
replaceExistingArchive: true

- publish: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
artifact: drop

- stage: Deploy
displayName: Deploy stage
dependsOn: Build
condition: succeeded()

jobs:
- deployment: Deploy
displayName: Deploy
environment: 'development'
pool:
vmImage: $(vmImageName)

strategy:
runOnce:
deploy:

steps:
- task: AzureFunctionApp@1
displayName: 'Azure functions app deploy'
inputs:
azureSubscription: '$(azureSubscription)'
appType: functionAppLinux
appName: $(functionAppName)
package: '$(Pipeline.Workspace)/drop/$(Build.BuildId).zip'

输出:-

enter image description here

enter image description here

enter image description here

关于python - 不使用 VS Code 扩展的 Azure Function 部署,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77221779/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com