你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

通过 Azure Pipelines 进行持续交付

使用 Azure Pipelines 来自动部署到 Azure Functions。 利用 Azure Pipelines,可以使用 Azure DevOps 通过持续集成 (CI) 和持续交付 (CD) 来进行生成、测试和部署。

YAML 管道是使用存储库中的 YAML 文件定义的。 步骤是管道的最小组成要素,并且可以是脚本或任务(预打包的脚本)。 了解构成管道的关键概念和组件

你将使用 AzureFunctionApp 任务部署到 Azure Functions。 现在有两个版本的 AzureFunctionApp 任务(AzureFunctionApp@1AzureFunctionApp@2)。 AzureFunctionApp@2 包括增强的验证支持,可降低管道因错误而失败的可能性。

请在本文顶部选择要使用的任务版本。 YAML 管道不适用于 Azure DevOps 2019 及更早版本。

先决条件

生成应用

  1. 登录到 Azure DevOps 组织,并导航到你的项目。
  2. 在项目中,导航到“Pipelines”页面 。 然后选择“新建管道”。
  3. 对于“代码位于何处?”,请选择以下选项之一
    • GitHub:你可能会重定向到 GitHub 进行登录。 如果是这样,请输入 GitHub 凭据。 如果此连接是你的第一个 GitHub 连接,则向导还会引导你完成将 DevOps 连接到 GitHub 帐户的过程。
    • Azure Repos Git:你可以立即在当前 DevOps 项目中选择存储库
  4. 存储库列表显示时,请选择示例应用存储库。
  5. Azure Pipelines 会分析存储库,并在“配置管道”中提供可能模板的列表。 选择适合你的语言的函数应用模板。 如果未看到正确的模板,请选择“显示更多”
  6. 选择“保存并运行”,再选择“直接提交到主分支”,然后再次选择“保存并运行” 。
  7. 开始新运行。 请等待运行完成。

YAML 生成管道示例

以下特定于语言的管道可用于生成应用。

可以使用以下示例创建用于生成 .NET 应用的 YAML 文件。

如果在生成应用时看到错误,请验证所使用的 .NET 版本是否与 Azure Functions 版本匹配。 有关详细信息,请参阅 Azure Functions 运行时版本概述

pool:
  vmImage: 'windows-latest'
steps:
- script: |
    dotnet restore
    dotnet build --configuration Release
- task: DotNetCoreCLI@2
  inputs:
    command: publish
    arguments: '--configuration Release --output publish_output'
    projects: '*.csproj'
    publishWebProjects: false
    modifyOutputPath: false
    zipAfterPublish: false
- task: ArchiveFiles@2
  displayName: "Archive files"
  inputs:
    rootFolderOrFile: "$(System.DefaultWorkingDirectory)/publish_output"
    includeRootFolder: false
    archiveFile: "$(System.DefaultWorkingDirectory)/build$(Build.BuildId).zip"
- task: PublishBuildArtifacts@1
  inputs:
    PathtoPublish: '$(System.DefaultWorkingDirectory)/build$(Build.BuildId).zip'
    artifactName: 'drop'

部署你的应用

你将部署 Azure 函数应用部署任务。 此任务需要 Azure 服务连接作为输入。 Azure 服务连接存储要从 Azure Pipelines 连接到 Azure 的凭据。

若要部署到 Azure Functions,请在 azure-pipelines.yml 文件的末尾添加以下代码片段。 默认 appType 为 Windows。 可以通过将 appType 设置为 functionAppLinux 来指定 Linux。

trigger:
- main

variables:
  # Azure service connection established during pipeline creation
  azureSubscription: <Name of your Azure subscription>
  appName: <Name of the function app>
  # Agent VM image name
  vmImageName: 'ubuntu-latest'

- task: AzureFunctionApp@1 # Add this at the end of your file
  inputs:
    azureSubscription: <Azure service connection>
    appType: functionAppLinux # default is functionApp
    appName: $(appName)
    package: $(System.ArtifactsDirectory)/**/*.zip
    #Uncomment the next lines to deploy to a deployment slot
    #Note that deployment slots is not supported for Linux Dynamic SKU
    #deployToSlotOrASE: true
    #resourceGroupName: '<Resource Group Name>'
    #slotName: '<Slot name>'

该代码段假定 YAML 文件中的生成步骤在代理上的 $(System.ArtifactsDirectory) 文件夹中生成 zip 存档。

部署容器

每次成功生成后,都可以自动将代码部署为容器化函数应用。 若要了解有关容器的详细信息,请参阅使用容器和 Azure Functions

部署到容器的最简单的方法是使用容器部署上的 Azure 函数应用任务

若要部署,在 YAML 文件的末尾添加以下代码片段:

trigger:
- main

variables:
  # Container registry service connection established during pipeline creation
  dockerRegistryServiceConnection: <Docker registry service connection>
  imageRepository: <Name of your image repository>
  containerRegistry: <Name of the Azure container registry>
  dockerfilePath: '$(Build.SourcesDirectory)/Dockerfile'
  tag: '$(Build.BuildId)'

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

- task: AzureFunctionAppContainer@1 # Add this at the end of your file
  inputs:
    azureSubscription: '<Azure service connection>'
    appName: '<Name of the function app>'
    imageName: $(containerRegistry)/$(imageRepository):$(tag)

该代码片段将 Docker 映像推送到 Azure 容器注册表。 容器部署上的 Azure 函数应用任务从指定的存储库中拉取与 BuildId 对应的相应 Docker 映像,然后部署该映像。

有关完整的端到端管道示例(包括生成容器和发布到容器注册表),请参阅此 Azure Pipelines 容器部署示例

部署到槽

可以将函数应用配置为具有多个槽。 通过槽,你可以安全地部署应用并对其进行测试,然后再将其提供给客户。

以下 YAML 代码片段演示如何部署到过渡槽,然后交换到生产槽:

- task: AzureFunctionApp@1
  inputs:
    azureSubscription: <Azure service connection>
    appType: functionAppLinux
    appName: <Name of the Function app>
    package: $(System.ArtifactsDirectory)/**/*.zip
    deployToSlotOrASE: true
    resourceGroupName: <Name of the resource group>
    slotName: staging

- task: AzureAppServiceManage@0
  inputs:
    azureSubscription: <Azure service connection>
    WebAppName: <name of the Function app>
    ResourceGroupName: <name of resource group>
    SourceSlot: staging
    SwapWithProduction: true

通过 Azure CLI 创建管道

若要在 Azure 中创建生成管道,请使用 az functionapp devops-pipeline create 命令。 创建生成管道是为了生成和发布在存储库中所做的任何代码更改。 此命令生成一个新的 YAML 文件(用于定义生成和发布管道),然后将该文件提交到存储库。 此命令的先决条件取决于代码的位置。

  • 如果代码位于 GitHub 中:

    • 你必须对订阅具有写入权限。

    • 你必须是 Azure DevOps 中的项目管理员。

    • 你必须有权创建具有足够权限的 GitHub 个人访问令牌 (PAT)。 有关详细信息,请参阅 GitHub PAT 权限要求

    • 你必须有权将内容提交到 GitHub 存储库的主分支,这样才能提交自动生成的 YAML 文件。

  • 如果代码位于 Azure Repos 中:

    • 你必须对订阅具有写入权限。

    • 你必须是 Azure DevOps 中的项目管理员。

生成应用

  1. 登录到 Azure DevOps 组织,并导航到你的项目。
  2. 在项目中,导航到“Pipelines”页面 。 然后选择“操作”以创建新的管道。
  3. 首先选择“GitHub”作为源代码位置,完成向导的各个步骤 。
  4. 可能会重定向到 GitHub 进行登录。 如果是这样,请输入 GitHub 凭据。
  5. 存储库列表显示时,请选择示例应用存储库。
  6. Azure Pipelines 将分析存储库,并推荐一个模板。 选择“保存并运行”,再选择“直接提交到主分支”,然后再次选择“保存并运行” 。
  7. 开始新运行。 请等待运行完成。

YAML 生成管道示例

以下特定于语言的管道可用于生成应用。

可以使用以下示例创建用于生成 .NET 应用的 YAML 文件:

pool:
  vmImage: 'windows-latest'
steps:
- script: |
    dotnet restore
    dotnet build --configuration Release
- task: DotNetCoreCLI@2
  inputs:
    command: publish
    arguments: '--configuration Release --output publish_output'
    projects: '*.csproj'
    publishWebProjects: false
    modifyOutputPath: false
    zipAfterPublish: false
- task: ArchiveFiles@2
  displayName: "Archive files"
  inputs:
    rootFolderOrFile: "$(System.DefaultWorkingDirectory)/publish_output"
    includeRootFolder: false
    archiveFile: "$(System.DefaultWorkingDirectory)/build$(Build.BuildId).zip"
- task: PublishBuildArtifacts@1
  inputs:
    PathtoPublish: '$(System.DefaultWorkingDirectory)/build$(Build.BuildId).zip'
    artifactName: 'drop'

部署你的应用

你将使用 Azure 函数应用部署 v2 任务进行部署。 此任务需要 Azure 服务连接作为输入。 Azure 服务连接存储要从 Azure Pipelines 连接到 Azure 的凭据。 应创建一个使用工作负载标识联合的连接。

该任务的 v2 版本包括对 .NET、Python 和 Node 的较新应用程序堆栈的支持。 该任务包括网络预部署检查。 发生预部署问题时,部署会停止。

若要部署到 Azure Functions,请在 azure-pipelines.yml 文件的末尾添加以下代码片段。 默认 appType 为 Windows。 可以通过将 appType 设置为 functionAppLinux 来指定 Linux。

trigger:
- main

variables:
  # Azure service connection established during pipeline creation
  azureSubscription: <Name of your Azure subscription>
  appName: <Name of the function app>
  # Agent VM image name
  vmImageName: 'ubuntu-latest'

- task: AzureFunctionApp@2 # Add this at the end of your file
  inputs:
    azureSubscription: <Azure service connection>
    appType: functionAppLinux # default is functionApp
    appName: $(appName)
    package: $(System.ArtifactsDirectory)/**/*.zip
    deploymentMethod: 'auto' # 'auto' | 'zipDeploy' | 'runFromPackage'. Required. Deployment method. Default: auto.
    #Uncomment the next lines to deploy to a deployment slot
    #Note that deployment slots is not supported for Linux Dynamic SKU
    #deployToSlotOrASE: true
    #resourceGroupName: '<Resource Group Name>'
    #slotName: '<Slot name>'

该代码段假定 YAML 文件中的生成步骤在代理上的 $(System.ArtifactsDirectory) 文件夹中生成 zip 存档。

部署容器

每次成功生成后,都可以自动将代码作为自定义容器部署到 Azure Functions。 若要了解有关容器的详细信息,请参阅使用容器和 Azure Functions

使用用于容器的 Azure 函数应用任务进行部署

部署到容器的最简单的方法是使用容器部署上的 Azure 函数应用任务

若要部署,在 YAML 文件的末尾添加以下代码片段:

trigger:
- main

variables:
  # Container registry service connection established during pipeline creation
  dockerRegistryServiceConnection: <Docker registry service connection>
  imageRepository: <Name of your image repository>
  containerRegistry: <Name of the Azure container registry>
  dockerfilePath: '$(Build.SourcesDirectory)/Dockerfile'
  tag: '$(Build.BuildId)'

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

- task: AzureFunctionAppContainer@1 # Add this at the end of your file
  inputs:
    azureSubscription: '<Azure service connection>'
    appName: '<Name of the function app>'
    imageName: $(containerRegistry)/$(imageRepository):$(tag)

该代码片段将 Docker 映像推送到 Azure 容器注册表。 容器部署上的 Azure 函数应用任务从指定的存储库中拉取与 BuildId 对应的相应 Docker 映像,然后部署该映像。

部署到槽

可以将函数应用配置为具有多个槽。 通过槽,你可以安全地部署应用并对其进行测试,然后再将其提供给客户。

以下 YAML 代码片段演示如何部署到过渡槽,然后交换到生产槽:

- task: AzureFunctionApp@2
  inputs:
    azureSubscription: <Azure service connection>
    appType: functionAppLinux
    appName: <Name of the Function app>
    package: $(System.ArtifactsDirectory)/**/*.zip
    deploymentMethod: 'auto'
    deployToSlotOrASE: true
    resourceGroupName: <Name of the resource group>
    slotName: staging

- task: AzureAppServiceManage@0
  inputs:
    azureSubscription: <Azure service connection>
    WebAppName: <name of the Function app>
    ResourceGroupName: <name of resource group>
    SourceSlot: staging
    SwapWithProduction: true

通过 Azure CLI 创建管道

若要在 Azure 中创建生成管道,请使用 az functionapp devops-pipeline create 命令。 创建生成管道是为了生成和发布在存储库中所做的任何代码更改。 此命令生成一个新的 YAML 文件(用于定义生成和发布管道),然后将该文件提交到存储库。 此命令的先决条件取决于代码的位置。

  • 如果代码位于 GitHub 中:

    • 你必须对订阅具有写入权限。

    • 你必须是 Azure DevOps 中的项目管理员。

    • 你必须有权创建具有足够权限的 GitHub 个人访问令牌 (PAT)。 有关详细信息,请参阅 GitHub PAT 权限要求

    • 你必须有权将内容提交到 GitHub 存储库的主分支,这样才能提交自动生成的 YAML 文件。

  • 如果代码位于 Azure Repos 中:

    • 你必须对订阅具有写入权限。

    • 你必须是 Azure DevOps 中的项目管理员。

后续步骤