将 ARM 模板与 Azure Pipelines 集成Integrate ARM templates with Azure Pipelines

可将 Azure 资源管理器模板(ARM 模板)与 Azure Pipelines 集成,以实现持续集成和持续部署 (CI/CD)。You can integrate Azure Resource Manager templates (ARM templates) with Azure Pipelines for continuous integration and continuous deployment (CI/CD). 教程 ARM 模板与 Azure Pipelines 的持续集成演示如何使用 ARM 模板部署任务从 GitHub 存储库部署模板。The tutorial Continuous integration of ARM templates with Azure Pipelines shows how to use the ARM template deployment task to deploy a template from your GitHub repo. 如果要直接从存储库部署模板,则可以使用此方法。This approach works when you want to deploy a template directly from a repository.

本文介绍使用 Azure Pipelines 部署模板的另外两种方法。In this article, you learn two more ways to deploy templates with Azure Pipelines. 本文介绍以下操作:This article shows how to:

  • 添加用于运行 Azure PowerShell 脚本的任务Add task that runs an Azure PowerShell script. 此选项的优势是可在整个开发生命周期中提供一致性,因为你可以使用运行本地测试时所用的同一脚本。This option has the advantage of providing consistency throughout the development life cycle because you can use the same script that you used when running local tests. 脚本将部署模板,但也可以执行其他操作,例如,获取要用作参数的值。Your script deploys the template but can also perform other operations such as getting values to use as parameters.

    Visual Studio 提供包含 PowerShell 脚本的 Azure 资源组项目Visual Studio provides the Azure Resource Group project that includes a PowerShell script. 该脚本会将项目中的生成工件暂存到资源管理器可以访问的存储帐户。The script stages artifacts from your project to a storage account that Resource Manager can access. 生成工件是项目中的一些项,例如链接的模板、脚本和应用程序二进制文件。Artifacts are items in your project such as linked templates, scripts, and application binaries. 如果要继续使用项目中的脚本,请使用本文中显示的 PowerShell 脚本任务。If you want to continue using the script from the project, use the PowerShell script task shown in this article.

  • 添加用于复制和部署任务的任务Add tasks to copy and deploy tasks. 此选项可以方便地取代项目脚本。This option offers a convenient alternative to the project script. 在管道中配置两个任务。You configure two tasks in the pipeline. 一个任务将生成工件暂存到可访问的位置。One task stages the artifacts to an accessible location. 其他任务从该位置部署模板。The other task deploys the template from that location.

准备项目Prepare your project

本文假设 ARM 模板和 Azure DevOps 组织已准备好创建管道。This article assumes your ARM template and Azure DevOps organization are ready for creating the pipeline. 以下步骤说明如何确保准备就绪:The following steps show how to make sure you're ready:

  • 你已有一个 Azure DevOps 组织。You have an Azure DevOps organization. 如果没有,请免费创建一个If you don't have one, create one for free. 如果你的团队已创建了一个 Azure DevOps 组织,请确保你是要使用的 Azure DevOps 项目的管理员。If your team already has an Azure DevOps organization, make sure you're an administrator of the Azure DevOps project that you want to use.

  • 已配置与 Azure 订阅之间的服务连接You've configured a service connection to your Azure subscription. 管道中的任务将以服务主体的身份执行。The tasks in the pipeline execute under the identity of the service principal. 有关创建连接的步骤,请参阅创建 DevOps 项目For steps to create the connection, see Create a DevOps project.

  • 你有一个用于定义项目基础结构的 ARM 模板You have an ARM template that defines the infrastructure for your project.

创建管道Create pipeline

  1. 如果事先尚未添加管道,需要创建一个新管道。If you haven't added a pipeline previously, you need to create a new pipeline. 在 Azure DevOps 组织中,选择“管道”和“新建管道”。 From your Azure DevOps organization, select Pipelines and New pipeline.

    添加新管道

  2. 指定代码的存储位置。Specify where your code is stored. 在下图中,选择的是“Azure Repos Git”。The following image shows selecting Azure Repos Git.

    选择代码源

  3. 从该源中,选择包含项目代码的存储库。From that source, select the repository that has the code for your project.

    选择存储库

  4. 选择要创建的管道类型。Select the type of pipeline to create. 可以选择“初学者管道”。You can select Starter pipeline.

    选择管道

现已准备好添加 Azure PowerShell 任务,或者复制文件并部署任务。You're ready to either add an Azure PowerShell task or the copy file and deploy tasks.

Azure PowerShell 任务Azure PowerShell task

本部分介绍如何使用一个在项目中运行 PowerShell 脚本的任务来配置持续部署。This section shows how to configure continuous deployment by using a single task that runs the PowerShell script in your project. 如果需要部署模板的 PowerShell 脚本,请参阅 Deploy-AzTemplate.ps1Deploy-AzureResourceGroup.ps1If you need a PowerShell script that deploys a template, see Deploy-AzTemplate.ps1 or Deploy-AzureResourceGroup.ps1.

以下 YAML 文件将创建 Azure PowerShell 任务The following YAML file creates an Azure PowerShell task:

trigger:
- master

pool:
  vmImage: 'ubuntu-latest'

steps:
- task: AzurePowerShell@5
  inputs:
    azureSubscription: 'script-connection'
    ScriptType: 'FilePath'
    ScriptPath: './Deploy-AzTemplate.ps1'
    ScriptArguments: -Location 'chinaeast' -ResourceGroupName 'demogroup' -TemplateFile templates\mainTemplate.json
    azurePowerShellVersion: 'LatestVersion'

将任务设置为 AzurePowerShell@5 时,管道使用 Az 模块When you set the task to AzurePowerShell@5, the pipeline uses the Az module. 如果要在脚本中使用 AzureRM 模块,请将任务设置为 AzurePowerShell@3If you're using the AzureRM module in your script, set the task to AzurePowerShell@3.

steps:
- task: AzurePowerShell@3

对于 azureSubscription,请提供创建的服务连接的名称。For azureSubscription, provide the name of the service connection you created.

inputs:
    azureSubscription: '<your-connection-name>'

对于 scriptPath,请提供管道文件中脚本的相对路径。For scriptPath, provide the relative path from the pipeline file to your script. 可以在存储库中查看该路径。You can look in your repository to see the path.

ScriptPath: '<your-relative-path>/<script-file-name>.ps1'

ScriptArguments 中,提供脚本所需的任何参数。In ScriptArguments, provide any parameters needed by your script. 以下示例演示脚本的一些参数,但你需要为脚本自定义参数。The following example shows some parameters for a script, but you'll need to customize the parameters for your script.

ScriptArguments: -Location 'chinaeast' -ResourceGroupName 'demogroup' -TemplateFile templates\mainTemplate.json

选择“保存”时,生成管道将自动运行。When you select Save, the build pipeline is automatically run. 返回生成管道的摘要并观察状态。Go back to the summary for your build pipeline, and watch the status.

查看结果

可以选择当前正在运行的管道来查看有关任务的详细信息。You can select the currently running pipeline to see details about the tasks. 管道运行完成后,你将看到每个步骤的结果。When it finishes, you see the results for each step.

复制并部署任务Copy and deploy tasks

本部分介绍如何使用两个任务来配置持续部署。This section shows how to configure continuous deployment by using a two tasks. 第一个任务将生成工件暂存到存储帐户,第二个任务部署模板。The first task stages the artifacts to a storage account and the second task deploy the template.

若要将文件复制到存储帐户,必须为服务连接的服务主体分配存储 blob 数据参与者或存储 blob 数据所有者角色。To copy files to a storage account, the service principal for the service connection must be assigned the Storage Blob Data Contributor or Storage Blob Data Owner role. 有关详细信息,请参阅 AzCopy 入门For more information, see Get started with AzCopy.

以下 YAML 显示了 Azure 文件复制任务The following YAML shows the Azure file copy task.

trigger:
- master

pool:
  vmImage: 'windows-latest'

steps:
- task: AzureFileCopy@4
  inputs:
    SourcePath: 'templates'
    azureSubscription: 'copy-connection'
    Destination: 'AzureBlob'
    storage: 'demostorage'
    ContainerName: 'projecttemplates'
  name: AzureFileCopy

需要根据环境修改此任务的多个部分。There are several parts of this task to revise for your environment. SourcePath 指示生成工件相对于管道文件的位置。The SourcePath indicates the location of the artifacts relative to the pipeline file.

SourcePath: '<path-to-artifacts>'

对于 azureSubscription,请提供创建的服务连接的名称。For azureSubscription, provide the name of the service connection you created.

azureSubscription: '<your-connection-name>'

对于存储和容器名称,请提供用于存储生成工件的存储帐户和容器的名称。For storage and container name, provide the names of the storage account and container you want to use for storing the artifacts. 该存储帐户必须存在。The storage account must exist.

storage: '<your-storage-account-name>'
ContainerName: '<container-name>'

创建复制文件任务后,可以添加该任务来部署暂存模板。After creating the copy file task, you're ready to add the task to deploy the staged template.

以下 YAML 显示了 Azure 资源管理器模板部署任务The following YAML shows the Azure Resource Manager template deployment task:

- task: AzureResourceManagerTemplateDeployment@3
  inputs:
    deploymentScope: 'Resource Group'
    azureResourceManagerConnection: 'copy-connection'
    subscriptionId: '00000000-0000-0000-0000-000000000000'
    action: 'Create Or Update Resource Group'
    resourceGroupName: 'demogroup'
    location: 'China North'
    templateLocation: 'URL of the file'
    csmFileLink: '$(AzureFileCopy.StorageContainerUri)templates/mainTemplate.json$(AzureFileCopy.StorageContainerSasToken)'
    csmParametersFileLink: '$(AzureFileCopy.StorageContainerUri)templates/mainTemplate.parameters.json$(AzureFileCopy.StorageContainerSasToken)'
    deploymentMode: 'Incremental'
    deploymentName: 'deploy1'

此任务有几个部分需要更详细地评审。There are several parts of this task to review in greater detail.

  • deploymentScope:从以下选项中选择部署范围:Management GroupSubscriptionResource GroupdeploymentScope: Select the scope of deployment from the options: Management Group, Subscription, and Resource Group. 若要详细了解范围,请参阅部署范围To learn more about the scopes, see Deployment scopes.

  • azureResourceManagerConnection:提供创建的服务连接的名称。azureResourceManagerConnection: Provide the name of the service connection you created.

  • subscriptionId:提供目标订阅 ID。subscriptionId: Provide the target subscription ID. 此属性仅适用于资源组部署范围和订阅部署范围。This property only applies to the Resource Group deployment scope and the subscription deployment scope.

  • resourceGroupNamelocation:提供要部署到的资源组的名称和位置。resourceGroupName and location: provide the name and location of the resource group you want to deploy to. 如果该资源组不存在,任务将创建该资源组。The task creates the resource group if it doesn't exist.

    resourceGroupName: '<resource-group-name>'
    location: '<location>'
    
  • csmFileLink:提供暂存模板的链接。csmFileLink: Provide the link for the staged template. 设置该值时,请使用从文件复制任务返回的变量。When setting the value, use variables returned from the file copy task. 以下示例链接到名为 mainTemplate.json 的模板。The following example links to a template named mainTemplate.json. 包含名为 templates 的文件夹,因为文件复制任务将文件复制到该文件夹中。The folder named templates is included because that where the file copy task copied the file to. 在管道中,提供模板的路径和模板的名称。In your pipeline, provide the path to your template and the name of your template.

    csmFileLink: '$(AzureFileCopy.StorageContainerUri)templates/mainTemplate.json$(AzureFileCopy.StorageContainerSasToken)'
    

管道如下所示:Your pipeline look like:

trigger:
- master

pool:
  vmImage: 'windows-latest'

steps:
- task: AzureFileCopy@4
  inputs:
    SourcePath: 'templates'
    azureSubscription: 'copy-connection'
    Destination: 'AzureBlob'
    storage: 'demostorage'
    ContainerName: 'projecttemplates'
  name: AzureFileCopy
- task: AzureResourceManagerTemplateDeployment@3
  inputs:
    deploymentScope: 'Resource Group'
    azureResourceManagerConnection: 'copy-connection'
    subscriptionId: '00000000-0000-0000-0000-000000000000'
    action: 'Create Or Update Resource Group'
    resourceGroupName: 'demogroup'
    location: 'China North'
    templateLocation: 'URL of the file'
    csmFileLink: '$(AzureFileCopy.StorageContainerUri)templates/mainTemplate.json$(AzureFileCopy.StorageContainerSasToken)'
    csmParametersFileLink: '$(AzureFileCopy.StorageContainerUri)templates/mainTemplate.parameters.json$(AzureFileCopy.StorageContainerSasToken)'
    deploymentMode: 'Incremental'
    deploymentName: 'deploy1'

选择“保存”时,生成管道将自动运行。When you select Save, the build pipeline is automatically run. 返回生成管道的摘要并观察状态。Go back to the summary for your build pipeline, and watch the status.

后续步骤Next steps

若要了解如何通过 GitHub Actions 使用 ARM 模板,请参阅使用 GitHub Actions 部署 Azure 资源管理器模板To learn about using ARM templates with GitHub Actions, see Deploy Azure Resource Manager templates by using GitHub Actions.