将资源管理器模板与 Azure Pipelines 集成Integrate Resource Manager templates with Azure Pipelines

Visual Studio 提供了 Azure 资源组项目用于创建模板并将其部署到 Azure 订阅。Visual Studio provides the Azure Resource Group project for creating templates and deploying them to your Azure subscription. 可将此项目与 Azure Pipelines 集成,以实现持续集成和持续部署 (CI/CD)。You can integrate this project with Azure Pipelines for continuous integration and continuous deployment (CI/CD).

可通过两种方式在 Azure Pipelines 中部署模板:There are two ways to deploy templates with Azure Pipelines:

  • 添加用于运行 Azure PowerShell 脚本的任务Add task that runs an Azure PowerShell script. 此选项的优势是可在整个开发生命周期中提供一致性,因为使用的脚本与 Visual Studio 项目中包含的脚本相同 (Deploy-AzureResourceGroup.ps1)。This option has the advantage of providing consistency throughout the development life cycle because you use the same script that is included in the Visual Studio project (Deploy-AzureResourceGroup.ps1). 该脚本会将项目中的生成工件暂存到资源管理器可以访问的存储帐户。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. 然后,该脚本将部署模板。Then, the script deploys the template.

  • 添加用于复制和部署任务的任务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 and the other task deploys the template.

本文介绍了这两种方法。This article shows both approaches.

准备项目Prepare your project

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

创建管道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. 以下 YAML 文件将创建 Azure PowerShell 任务The following YAML file creates an Azure PowerShell task:

pool:
  name: Hosted Windows 2019 with VS2019
  demands: azureps

steps:
- task: AzurePowerShell@3
  inputs:
    azureSubscription: 'demo-deploy-sp'
    ScriptPath: 'AzureResourceGroupDemo/Deploy-AzureResourceGroup.ps1'
    ScriptArguments: -ResourceGroupName 'demogroup' -ResourceGroupLocation 'chinaeast'
    azurePowerShellVersion: LatestVersion

将该任务设置为 AzurePowerShell@3 时,管道将使用 AzureRM 模块中的命令对连接进行身份验证。When you set the task to AzurePowerShell@3, the pipeline uses commands from the AzureRM module to authenticate the connection. 默认情况下,Visual Studio 项目中的 PowerShell 脚本使用 AzureRM 模块。By default, the PowerShell script in the Visual Studio project uses the AzureRM module. 如果已将脚本更新为使用 Az 模块,请将任务设置为 AzurePowerShell@4If you've updated your script to use the Az module, set the task to AzurePowerShell@4.

steps:
- task: AzurePowerShell@4

对于 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'

如果不需要暂存生成工件,则只需传递用于部署的资源组的名称和位置。If you don't need to stage artifacts, just pass the name and location of a resource group to use for deployment. 如果该资源组尚不存在,Visual Studio 脚本将创建该资源组。The Visual Studio script creates the resource group if it doesn't already exist.

ScriptArguments: -ResourceGroupName '<resource-group-name>' -ResourceGroupLocation '<location>'

如果需要将生成工件暂存到现有存储帐户,请使用:If you need to stage artifacts to an existing storage account, use:

ScriptArguments: -ResourceGroupName '<resource-group-name>' -ResourceGroupLocation '<location>' -UploadArtifacts -ArtifactStagingDirectory '$(Build.StagingDirectory)' -StorageAccountName '<your-storage-account>'

了解如何创建任务后,接下来让我们完成编辑管道的步骤。Now, that you understand how to create the task, let's go through the steps to edit the pipeline.

  1. 打开管道,并将内容替换为 YAML:Open your pipeline, and replace the contents with your YAML:

    pool:
     name: Hosted Windows 2019 with VS2019
     demands: azureps
    
    steps:
    - task: AzurePowerShell@3
     inputs:
       azureSubscription: 'demo-deploy-sp'
       ScriptPath: 'AzureResourceGroupDemo/Deploy-AzureResourceGroup.ps1'
       ScriptArguments: -ResourceGroupName 'demogroup' -ResourceGroupLocation 'chinaeast' -UploadArtifacts -ArtifactStagingDirectory '$(Build.StagingDirectory)' -StorageAccountName 'stage3a4176e058d34bb88cc'
       azurePowerShellVersion: LatestVersion
    
  2. 选择“保存” 。Select Save.

    保存管道

  3. 提供要提交的消息,并直接提交到 masterProvide a message for the commit, and commit directly to master.

  4. 选择“保存”时,生成管道将自动运行。 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 to stage the artifacts and deploy the template.

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

- task: AzureFileCopy@3
  displayName: 'Stage files'
  inputs:
    SourcePath: 'AzureResourceGroup1'
    azureSubscription: 'demo-deploy-sp'
    Destination: 'AzureBlob'
    storage: 'stage3a4176e058d34bb88cc'
    ContainerName: 'democontainer'
    outputStorageUri: 'artifactsLocation'
    outputStorageContainerSasToken: 'artifactsLocationSasToken'
    sasTokenTimeOutInMinutes: '240'

需要根据环境修改此任务的多个部分。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. 在此示例中,文件位于名为 AzureResourceGroup1(也是项目名称)的文件夹中。In this example, the files exist in a folder named AzureResourceGroup1 which was the name of the project.

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>'

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

- task: AzureResourceGroupDeployment@2
  displayName: 'Deploy template'
  inputs:
    deploymentScope: 'Resource Group'
    ConnectedServiceName: 'demo-deploy-sp'
    subscriptionName: '01234567-89AB-CDEF-0123-4567890ABCDEF'
    action: 'Create Or Update Resource Group'
    resourceGroupName: 'demogroup'
    location: 'China North'
    templateLocation: 'URL of the file'
    csmFileLink: '$(artifactsLocation)WebSite.json$(artifactsLocationSasToken)'
    csmParametersFileLink: '$(artifactsLocation)WebSite.parameters.json$(artifactsLocationSasToken)'
    overrideParameters: '-_artifactsLocation $(artifactsLocation) -_artifactsLocationSasToken "$(artifactsLocationSasToken)"'
    deploymentMode: 'Incremental'

需要根据环境修改此任务的多个部分。There are several parts of this task to revise for your environment.

  • deploymentScope:从以下选项中选择部署范围:SubscriptionManagement GroupResource GroupdeploymentScope: Select the scope of deployment from the options: Management Group, Subscription and Resource Group. 在本演练中使用资源组Use Resource Group in this walk through. 若要详细了解范围,请参阅部署范围To learn more about the scopes, see Deployment scopes.

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

    ConnectedServiceName: '<your-connection-name>'
    
  • subscriptionName:提供目标订阅 ID。subscriptionName: 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>'
    

部署任务链接到名为 WebSite.json 的模板以及名为 WebSite.parameters.json 的参数文件。The deployment task links to a template named WebSite.json and a parameters file named WebSite.parameters.json. 请使用模板和参数文件的名称。Use the names of your template and parameter files.

了解如何创建任务后,接下来让我们完成编辑管道的步骤。Now, that you understand how to create the tasks, let's go through the steps to edit the pipeline.

  1. 打开管道,并将内容替换为 YAML:Open your pipeline, and replace the contents with your YAML:

    pool:
     name: Hosted Windows 2019 with VS2019
    
    steps:
    - task: AzureFileCopy@3
     displayName: 'Stage files'
     inputs:
       SourcePath: 'AzureResourceGroup1'
       azureSubscription: 'demo-deploy-sp'
       Destination: 'AzureBlob'
       storage: 'stage3a4176e058d34bb88cc'
       ContainerName: 'democontainer'
       outputStorageUri: 'artifactsLocation'
       outputStorageContainerSasToken: 'artifactsLocationSasToken'
       sasTokenTimeOutInMinutes: '240'
    - task: AzureResourceGroupDeployment@2
      displayName: 'Deploy template'
      inputs:
        deploymentScope: 'Resource Group'
        ConnectedServiceName: 'demo-deploy-sp'
        subscriptionName: '01234567-89AB-CDEF-0123-4567890ABCDEF'
        action: 'Create Or Update Resource Group'
        resourceGroupName: 'demogroup'
        location: 'China North'
        templateLocation: 'URL of the file'
        csmFileLink: '$(artifactsLocation)WebSite.json$(artifactsLocationSasToken)'
        csmParametersFileLink: '$(artifactsLocation)WebSite.parameters.json$(artifactsLocationSasToken)'
        overrideParameters: '-_artifactsLocation $(artifactsLocation) -_artifactsLocationSasToken "$(artifactsLocationSasToken)"'
        deploymentMode: 'Incremental'
    
  2. 选择“保存” 。Select Save.

  3. 提供要提交的消息,并直接提交到 masterProvide a message for the commit, and commit directly to master.

  4. 选择“保存”时,生成管道将自动运行。 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.

后续步骤Next steps

有关将 Azure Pipelines 与资源管理器模板配合使用的分步过程,请参阅教程:使用 Azure Pipelines 持续集成 Azure 资源管理器模板For step-by-step process on using Azure Pipelines with Resource Manager templates, see Tutorial: Continuous integration of Azure Resource Manager templates with Azure Pipelines.