快速入门:将 Bicep 与 Azure Pipelines 集成

本快速入门介绍如何将 Bicep 文件与 Azure Pipelines 集成,以实现持续集成和持续部署 (CI/CD)。

它简要介绍了部署 Bicep 文件所需执行的管道任务。

先决条件

如果没有 Azure 订阅,请在开始前创建试用版订阅

需要一个 Azure DevOps 组织。 如果没有,请创建一个试用版订阅。 如果你的团队已创建了一个 Azure DevOps 组织,请确保你是要使用的 Azure DevOps 项目的管理员。

需要配置到 Azure 订阅的服务连接。 管道中的任务将以服务主体的身份执行。 有关创建连接的步骤,请参阅创建 DevOps 项目

需要一个定义项目基础结构的 Bicep 文件。 此文件位于存储库中。

创建管道

  1. 从 Azure DevOps 组织中,选择“管道”和“创建管道”。

    Screenshot of creating new pipeline.

  2. 指定代码的存储位置。 此快速入门使用 Azure Repos Git。

    Screenshot of selecting code source.

  3. 选择包含项目代码的存储库。

    Screenshot of selecting repository.

  4. 选择“初学者管道”作为要创建的管道的类型。

    Screenshot of selecting pipeline.

部署 Bicep 文件

可使用 Azure 资源组部署任务或 Azure CLI 任务来部署 Bicep 文件。

使用 Azure 资源管理器模板部署任务

注意

如果具有 bicepparam 文件,AzureResourceManagerTemplateDeployment@3 任务将不起作用。

  1. 将初学者管道替换为以下 YAML。 它将创建资源组,并使用 Azure 资源管理器模板部署任务部署 Bicep 文件。

    trigger:
    - main
    
    name: Deploy Bicep files
    
    variables:
      vmImageName: 'ubuntu-latest'
    
      azureServiceConnection: '<your-connection-name>'
      resourceGroupName: 'exampleRG'
      location: '<your-resource-group-location>'
      templateFile: './main.bicep'
    pool:
      vmImage: $(vmImageName)
    
    steps:
    - task: AzureResourceManagerTemplateDeployment@3
      inputs:
        deploymentScope: 'Resource Group'
        azureResourceManagerConnection: '$(azureServiceConnection)'
        action: 'Create Or Update Resource Group'
        resourceGroupName: '$(resourceGroupName)'
        location: '$(location)'
        templateLocation: 'Linked artifact'
        csmFile: '$(templateFile)'
        overrideParameters: '-storageAccountType Standard_LRS'
        deploymentMode: 'Incremental'
        deploymentName: 'DeployPipelineTemplate'
    
  2. 更新 azureServiceConnectionlocation 的值。

  3. 验证存储库中有 main.bicep,以及 Bicep 文件的内容。

  4. 选择“保存” 。 生成管道将自动运行。 返回生成管道的摘要并观察状态。

使用 Azure CLI 任务

  1. 将初学者管道替换为以下 YAML。 它将创建资源组,并使用 Azure CLI 任务部署 Bicep 文件:

    trigger:
    - main
    
    name: Deploy Bicep files
    
    variables:
      vmImageName: 'ubuntu-latest'
    
      azureServiceConnection: '<your-connection-name>'
      resourceGroupName: 'exampleRG'
      location: '<your-resource-group-location>'
      templateFile: 'main.bicep'
    pool:
      vmImage: $(vmImageName)
    
    steps:
    - task: AzureCLI@2
      inputs:
        azureSubscription: $(azureServiceConnection)
        scriptType: bash
        scriptLocation: inlineScript
        useGlobalConfig: false
        inlineScript: |
          az --version
          az group create --name $(resourceGroupName) --location $(location)
          az deployment group create --resource-group $(resourceGroupName) --template-file $(templateFile)
    

    若要替代参数,请将 inlineScript 的最后一行更新为:

    az deployment group create --resource-group $(resourceGroupName) --template-file $(templateFile) --parameters storageAccountType='Standard_GRS' location='chinanorth'
    

    有关任务输入的说明,请参阅 Azure CLI 任务。 在气隙云上使用此任务时,必须将任务的 useGlobalConfig 属性设置为 true。 默认值为 false

  2. 更新 azureServiceConnectionlocation 的值。

  3. 验证存储库中有 main.bicep,以及 Bicep 文件的内容。

  4. 选择“保存” 。 生成管道将自动运行。 返回生成管道的摘要并观察状态。

清理资源

如果不再需要本文中创建的 Azure 资源,请使用 Azure CLI 或 Azure PowerShell 删除快速入门资源组。

az group delete --name exampleRG

后续步骤