如何在运行时将值从Azure DevOps变量库获取到Bicep模板中

mi7gmzs6  于 2023-10-22  发布在  其他
关注(0)|答案(1)|浏览(123)

大家早上好
我目前正在创建一个创建sql server和数据库的模板。我已经设法在运行时从管道中获取了一个参数作为环境名称。然而,我的下一个问题是,我想在四个不同的环境中使用相同的模板。其中三个具有相同的订阅ID,然后prod具有不同的订阅ID。因此,我认为最好为每个环境创建一个库,然后在我的Azure DevOps管道中,将库链接到管道。
我现在要做的是,例如,从库中获取三个值,并在管道运行时将它们放置到Bicep模板中。
有人能建议如何做到这一点吗?
谢谢你,
达伦
以下是Bicep模板:

targetScope = 'resourceGroup'

// input parameters
param Environment string
param sqladminlogin string = '${SqlAdminLogin}'
param sqladminpassword string = '{SqlAdminPassword}'
param SqlServerName string = 'SQL-${Environment}01'
param location string = resourceGroup().location
param Sql_DB_Name string = 'SQL-${Environment}'

resource SqlServerName_resource_tst 'Microsoft.Sql/servers@2022-05-01-preview' = {
  name: toLower(SqlServerName)
  location: location
  tags: {
    Service: 'TESTING'
    Environment: Environment
    'Business Owner': 'TBC'
  }
  kind: 'v12.0'
  identity: {
    type: 'SystemAssigned'
  }
  properties: {
    administratorLogin: sqladminlogin
    administratorLoginPassword: sqladminpassword
    version: '12.0'
    minimalTlsVersion: '1.2'
    publicNetworkAccess: 'Enabled'
    administrators: {
      administratorType: 'ActiveDirectory'
      principalType: 'Group'
      login: 'NSG-AG-SQL-RW-TST'
      sid: '********'
      tenantId: '**********'
      azureADOnlyAuthentication: false
    }
    restrictOutboundNetworkAccess: 'Disabled'
  }
}

管道看起来像这样:

name: bicep-deployment

trigger:
- main

pool:
  vmImage: 'windows-latest'

parameters:
  - name: Environment
    displayName: Environment
    type: string
    default: 'TST'
    values:
      - DEV
      - TST
      - UAT
      - PRD

  - name: Action
    displayName: Action
    type: string
    default: 'Plan'
    values:
      - Plan
      - Apply

variables:
  - name: Environment
    value: '${{ parameters.Environment }}'

  - name: System.Debug
    value: true

  - name: Action
    value: '${{ parameters.Action }}'

  - name: serviceConnection
    ${{ if eq( parameters['Environment'], 'DEV') }}:
      value: "AG-Dev"
    ${{ if eq( parameters['Environment'], 'TST') }}:
      value: "AG-TST"
    ${{ if eq( parameters['Environment'], 'UAT' ) }}:
      value: "AG-UAT"
    ${{ if eq( parameters['Environment'], 'PRD' ) }}:
      value: "AG-PRD"

  - name: resourceGroupName
    ${{ if eq( parameters['Environment'], 'DEV') }}:
      value: "RG-AG-DEV"
    ${{ if eq( parameters['Environment'], 'TST') }}:
      value: "RG-AG-TST"
    ${{ if eq( parameters['Environment'], 'UAT') }}:
      value: "RG-AG-UAT"
    ${{ if eq( parameters['Environment'], 'PRD') }}:
      value: "RG-AG-PR"

  # Select Variable Library to use for the environment.
  - group: ${{parameters.Environment}}

stages:
  # SQL Stages
  - stage: Preview_SQL
    jobs:
    - job: Preview
      steps:
      - task: AzureCLI@2
        inputs:
          azureSubscription: $(serviceConnection)
          scriptType: 'bash'
          scriptLocation: 'inlineScript'
          inlineScript: |
            az deployment group what-if \
              --resource-group '$(resourceGroupName)' \
              --template-file Bicep/SQL/SQL-tst.bicep \
              --parameters Environment="${{ parameters.Environment }}"

  # The deployment only runs if Apply action is selected when running the pipeline and if the validate job succeeds.
  - stage: Deploy_SQL
    dependsOn: Preview_SQL
    jobs:
      - deployment: Deploy_SQL
        displayName: Deploy_SQL
        environment: $(Environment)
        condition: and(succeeded(), eq(variables['Action'], 'Apply'))
        strategy:
          runOnce:
            deploy:
              steps:
                - checkout: self
                - task: AzureCLI@2
                  displayName: Bicep deployment
                  inputs:
                    azureSubscription: $(serviceConnection)
                    scriptType: bash
                    scriptLocation: inlineScript
                    inlineScript: |
                      set -e

                      echo '##[Section]Deploy SQL'

                      az deployment group create \
                        --resource-group $(resourceGroupName) \
                        --name "SQL-deployment" \
                        --template-file Bicep/SQL/SQL-tst.bicep \
                        --parameters Environment="${{ parameters.Environment }}"

当运行管道时,它将运行,但部署到Azure将失败。当我查看Azure门户中的部署历史记录时,它有一个错误的请求,但是如果我检查输入,我可以看到SQL管理员登录名和密码没有被复制,而是字符串$(SqlAdminLogin)& $(SqlAdminPassword)被复制。如何让这些变量的值显示在这里?

xsuvu9jc

xsuvu9jc1#

你的二头肌并没有定义所有的参数:

param Environment string
param sqladminlogin string = '${SqlAdminLogin}'
param sqladminpassword string = '{SqlAdminPassword}'
param SqlServerName string = 'SQL-${Environment}01'
param location string = resourceGroup().location
param Sql_DB_Name string = 'SQL-${Environment}'

当您传递Environment = UAT时,将为

Environment='UAT'
sqladminlogin = ''
sqladminpassword = ''
SqlServerName = 'SQL-UAT01'
location = your_location
Sql_DB_Name = 'SQL-UAT'

你应该在这里传递所有参数:

az deployment group create \
    --resource-group $(resourceGroupName) \
    --name "SQL-deployment" \
    --template-file Bicep/SQL/SQL-tst.bicep \
    --parameters Environment="${{ parameters.Environment}}" sqladminlogin=Some_value sqladminpassword=Some_value

您可以更新您的参数:

param Environment string
param sqladminlogin string
param sqladminpassword string
param SqlServerName string = 'SQL-${Environment}01'
param location string = resourceGroup().location
param Sql_DB_Name string = 'SQL-${Environment}'

https://learn.microsoft.com/en-us/cli/azure/deployment/group?view= azure-deployment-latest#az-deployment-group-create()-examples
https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/parameters

相关问题