Azure Pipeline 拒绝从 zip 部署,而是从不存在的 blob 调用

标签 azure yaml azure-pipelines azure-pipelines-yaml

对 YAML 来说还很陌生,但我已经向一位和我同样困惑的同事展示了这一点。我已经构建了一个 CI/CD 管道,并且 CI 端似乎工作得很好。管道声称已成功部署函数应用程序,但当我去检查时,函数应用程序中没有任何内容,并且管道的输出代码似乎是从不存在的 blob 存储文件夹调用的。我们无法弄清楚如何改变它的行为。

有人以前见过这种东西吗?

这是 YAML:

# Python Function App to Linux on Azure
# Build a Python function app and deploy it to Azure as a Linux function app.
# Add steps that analyze code, save build artifacts, deploy, and more:
# https://learn.microsoft.com/azure/devops/pipelines/languages/python

trigger:
- master

variables:
  # Azure Resource Manager connection created during pipeline creation
  azureSubscription: 'ab1c86d0-0d0c-4029-913b-e5483df967b2'

  # Function app name
  functionAppName: 'categoriserfunctionapp'

  # Agent VM image name
  vmImageName: 'ubuntu-latest'

  # Working Directory
  workingDirectory: ''

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.6 # Functions V2 supports Python 3.6 as of today
        architecture: 'x64'

    - 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: "$(System.DefaultWorkingDirectory)"
        includeRootFolder: false
        archiveType: zip
        archiveFile: "$(System.DefaultWorkingDirectory)/$(Build.BuildId).zip"
        replaceExistingArchive: true

    - task: PublishBuildArtifacts@1
      inputs:
        PathtoPublish: '$(System.DefaultWorkingDirectory)/$(Build.BuildId).zip'
        artifactName: 'drop'
        
- stage: Deploy
  displayName: Deploy stage
  dependsOn: Build
  condition: succeeded()

  jobs:
  - deployment: Deploy
    displayName: Deploy
    environment: 'test'
    pool:
      vmImage: 'windows-latest'

    strategy:
      runOnce:
        deploy:

          steps:
          - task: DownloadPipelineArtifact@2
            displayName: 'Download Pipeline Artifact'
            inputs:
              buildType: 'current'
              artifactName: 'drop'
              targetPath: '$(Pipeline.Workspace)/drop/'
          - task: AzureFunctionApp@1
            inputs:
              azureSubscription: 'Visual Studio Enterprise Subscription – MPN (f1f3e234-557b-4acd-b353-2ff89c547e49)'
              appType: 'functionAppLinux'
              appName: 'categoriserfunctionapp'
              package: '$(Pipeline.Workspace)/drop/$(Build.BuildId).zip'
              runtimeStack: 'PYTHON|3.9'

这是 CD 端的输出:

2022-09-27T15:59:26.6330661Z ##[section]Starting: AzureFunctionApp
2022-09-27T15:59:26.6442433Z ==============================================================================
2022-09-27T15:59:26.6442718Z Task         : Azure Functions
2022-09-27T15:59:26.6443009Z Description  : Update a function app with .NET, Python, JavaScript, PowerShell, Java based web applications
2022-09-27T15:59:26.6443282Z Version      : 1.208.2
2022-09-27T15:59:26.6443454Z Author       : Microsoft Corporation
2022-09-27T15:59:26.6443686Z Help         : https://aka.ms/azurefunctiontroubleshooting
2022-09-27T15:59:26.6443957Z ==============================================================================
2022-09-27T15:59:27.4092341Z Got service connection details for Azure App Service:'categoriserfunctionapp'
2022-09-27T15:59:30.9793711Z Trying to update App Service Application settings. Data: {"WEBSITE_RUN_FROM_PACKAGE":"https://categoriserfunc9c44.blob.core.windows.net/azure-pipelines-deploy/package_1664294369833.zip?***"}
2022-09-27T15:59:32.4412565Z Updated App Service Application settings.
2022-09-27T15:59:32.4414182Z Updated WEBSITE_RUN_FROM_PACKAGE Application setting to https://categoriserfunc9c44.blob.core.windows.net/azure-pipelines-deploy/package_1664294369833.zip?***
2022-09-27T15:59:37.4527980Z Syncing triggers for function app
2022-09-27T15:59:39.3782043Z Sync triggers for function app completed successfully
2022-09-27T15:59:41.0691225Z Successfully added release annotation to the Application Insight : categoriserfunctionapp
2022-09-27T15:59:41.3968439Z App Service Application URL: https://categoriserfunctionapp.azurewebsites.net
2022-09-27T15:59:41.4063927Z ##[section]Finishing: AzureFunctionApp

我们尝试更改 WEBSITE_RUN_FROM_PACKAGE 的硬代码,但自从我刷新函数应用程序后,它似乎又变回来了。

有人有解决这个问题的想法吗?

最佳答案

根据您提供的 YAML 稍作修改,我就可以成功部署到 azure 函数应用。

trigger:
- none

variables:
  # Azure Resource Manager connection created during pipeline creation
  azureSubscription: '<ARM Service Connection Name>'
  resourceGroupName: '<Resource Group Name of storage>'
  # Function app name
  functionAppName: '<Your Function App Name>'
  # Agent VM image name
  vmImageName: 'ubuntu-latest'

  # Working Directory
  workingDirectory: ''

  storage_str: 'DefaultEndpointsProtocol=https;AccountName=xxx;AccountKey=xxx;EndpointSuffix=core.windows.net'

stages:
- stage: Build
  displayName: Build stage

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

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

    - 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: "$(System.DefaultWorkingDirectory)"
        includeRootFolder: false
        archiveType: zip
        archiveFile: "$(System.DefaultWorkingDirectory)/$(Build.BuildId).zip"
        replaceExistingArchive: true

    - task: PublishBuildArtifacts@1
      inputs:
        PathtoPublish: '$(System.DefaultWorkingDirectory)/$(Build.BuildId).zip'
        artifactName: 'drop'
        
- stage: Deploy
  displayName: Deploy stage
  dependsOn: Build
  condition: succeeded()

  jobs:
  - deployment: Deploy
    displayName: Deploy
    environment: 'test'
    pool:
      vmImage: 'windows-latest'

    strategy:
      runOnce:
        deploy:
          steps:
          - task: DownloadPipelineArtifact@2
            displayName: 'Download Pipeline Artifact'
            inputs:
              buildType: 'current'
              artifactName: 'drop'
              targetPath: '$(Pipeline.Workspace)/drop/'
          - task: AzureAppServiceSettings@1
            inputs:
              azureSubscription: '$(azureSubscription)'
              appName: '$(functionAppName)'
              resourceGroupName: '$(resourceGroupName)'
              appSettings: |
                [
                  {
                    "name": "AzureWebJobsStorage",
                    "value": "$(storage_str)",
                    "slotSetting": false
                  }
                ]
          - task: AzureFunctionApp@1
            inputs:
              azureSubscription: '$(azureSubscription)'
              appType: 'functionAppLinux'
              appName: '$(functionAppName)'
              package: '$(Pipeline.Workspace)/drop/$(Build.BuildId).zip'
              runtimeStack: 'PYTHON|3.9'

enter image description here

enter image description here

enter image description here

根据您的描述,blob 文件不存在?如果是这样,该函数将无法成功执行。

您必须确保有一个文件可用于“从包运行”。

您的 YAML 存在多个问题,可能会导致此问题:

  1. YAML 中的变量未完全使用。 Deployment阶段的服务连接和变量定义不一样(我改成一样的)。

  2. 使用的python版本和azure function app版本不一样(我已经改成了一样的)。

顺便说一句,为了排除存储被某些策略或程序控制的问题,您可以创建一个新的存储来测试并提供上面YAML文件中的连接字符串(您的包的位置) function app是基于AzureWebJobsStorage确定的,上面的YAML可以在实际部署之前更改设置。)。

另外,如果您可以在本地(例如 VS Code)上毫无问题地部署函数应用,那么您可以使用如下所示的方法来部署函数应用。

trigger:
- none
variables:
- name: System.debug
  value: true
pool:
  VMAS #agent based on your local machine.
steps:
- task: AzurePowerShell@5
  inputs:
    azureSubscription: 'xxx'
    ScriptType: 'InlineScript'
    Inline: 'func azure functionapp publish xxx --build remote'
    azurePowerShellVersion: 'LatestVersion'

我的仓库结构如下:

enter image description here

关于Azure Pipeline 拒绝从 zip 部署,而是从不存在的 blob 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73870921/

相关文章:

azure - 尝试访问 azure Rest api 时授权失败,出现 401

Docker-compose,远程上下文,相对于本地机器而不是远程的路径

c# - 命名空间 'Management' 中不存在类型或命名空间名称 'Microsoft.SqlServer' - SqlParser 和 VSTS 自动构建

powershell - "PowerShell on Target Machines"任务因 TFS 2017\Azure Dev Ops 中的错误而失败

azure-devops - 从模板将环境变量设置为 azure Devops 管道步骤

Azure 认知服务 - 批量转录 API 响应错误消息 "The recording URI is invalid."

Azure 开发运营 : Can not load nuget package from feed in Azure Pipeline

python - 如何使用 beautiful soup 将爬取数据上传到 python 中的 AZURE BLOB STORAGE 中?

amazon-web-services - 在 Elastic Beanstalk 上运行 .config 文件?

amazon-web-services - 在 Cloudformation 模板中传递安全组 ID 和子网 ID