如何将 Azure 管道变量传递到 AzureResourceManagerTemplateDeployment@3 任务使用的 ARM 模板?

Ale*_*ber 3 azure azure-resource-manager azure-service-fabric azure-keyvault azure-pipelines

我尝试在每天晚上安排的 Azure 管道中执行以下 2 个步骤:

  1. 将自签名证书放入密钥库
  2. 通过 ARM 模板部署 Service Fabric 群集,并使用证书指纹和秘密 ID 作为参数。

在密钥库中创建证书的第一步对我来说效果很好:

# import the self-signed certificate ccg-self-signed-cert into the Keyvault
- task: AzurePowerShell@5
  inputs:
    azureSubscription: '${{ parameters.ArmConnection }}'
    ScriptType: 'InlineScript'
    azurePowerShellVersion: '3.1.0'
    Inline: |
      $Pwd = ConvertTo-SecureString -String 'MyPassword' -Force -AsPlainText
      $Base64 = 'MIIKqQ____3000_CHARS_HERE______1ICAgfQ=='
      $Cert = Import-AzKeyVaultCertificate -VaultName $(KeyVaultName) -Name my-self-signed-cert -CertificateString $Base64 -Password $Pwd
      echo "##vso[task.setvariable variable=Thumbprint;isOutput=true]$Cert.Thumbprint"
Run Code Online (Sandbox Code Playgroud)

我想我按行设置了管道变量echo(不太确定,如何验证......)

但是,如何在下一个管道任务中将保存证书指纹值的管道变量传递给 ARM 模板?

# deploy SF cluster by ARM template and use the SF Cluster certificate thumbsprint as admin cert
- task: AzureResourceManagerTemplateDeployment@3
  inputs:
    deploymentScope: 'Resource Group'
    azureResourceManagerConnection: '${{ parameters.ArmConnection }}'
    subscriptionId: 'XXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXX'
    action: 'Create Or Update Resource Group'
    resourceGroupName: '${{ parameters.resourceGroupName }}'
    location: 'West Europe'
    templateLocation: 'Linked artifact'
    csmFile: '$(Build.SourcesDirectory)/pipelines/templates/sfcluster.json'
    csmParametersFile: '$(Build.SourcesDirectory)/pipelines/templates/sfcluster-params.json'
    deploymentMode: 'Incremental'
Run Code Online (Sandbox Code Playgroud)

我正在使用azure-quickstart-template创建 SF 集群。

如果你看一下它,它需要一个证书指纹作为参数:

"certificateThumbprint": {
  "type": "string",
  "metadata": {
    "description": "Certificate Thumbprint"
  }
},

"certificateUrlValue": {
  "type": "string",
  "metadata": {
    "description": "Refers to the location URL in your key vault where the certificate was uploaded, it is should be in the format of https://<name of the vault>.vault.azure.net:443/secrets/<exact location>"
  }
},
Run Code Online (Sandbox Code Playgroud)

如何将值从 AzurePowerShell@5 任务传递到后续 AzureResourceManagerTemplateDeployment@3 任务使用的 ARM 模板?

更新:

我尝试遵循 Nilay 的建议,并将 3 个变量放入我的 sfcluster.json ARM 模板中:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "clusterName": {
            "type": "string",
            "defaultValue": "ccg-sfcluster",
            "minLength": 5,
            "metadata": {
                "description": "Name of the SF cluster"
            }
        },
        "certificateThumbprint": {
            "type": "string",
            "defaultValue": "[$env:THUMBPRINT]",
            "metadata": {
                "description": "Certificate Thumbprint"
            }
        },
        "sourceVaultResourceId": {
            "type": "string",
            "defaultValue": "[$env:KEYVAULTID]",
            "metadata": {
                "description": "Resource Id of the key vault, is should be in the format of /subscriptions/<Sub ID>/resourceGroups/<Resource group name>/providers/Microsoft.KeyVault/vaults/<vault name>"
            }
        },
        "certificateUrlValue": {
            "type": "string",
            "defaultValue": "[$env:SECRETID]",
            "metadata": {
                "description": "Refers to the location URL in your key vault where the certificate was uploaded, it is should be in the format of https://<name of the vault>.vault.azure.net:443/secrets/<exact location>"
            }
        }
    },
    "variables": {
Run Code Online (Sandbox Code Playgroud)

但是我收到语法错误:

2020-05-27T12:31:54.1327314Z There were errors in your deployment. Error code: InvalidTemplate.
2020-05-27T12:31:54.1354742Z ##[error]Deployment template language expression evaluation failed: 'The language expression '$env:THUMBPRINT' is not valid: the string character ':' at position '4' is not expected.'. Please see https://aka.ms/arm-template-expressions for usage details.
2020-05-27T12:31:54.1361090Z ##[debug]Processed: ##vso[task.issue type=error;]Deployment template language expression evaluation failed: 'The language expression '$env:THUMBPRINT' is not valid: the string character ':' at position '4' is not expected.'. Please see https://aka.ms/arm-template-expressions for usage details.
Run Code Online (Sandbox Code Playgroud)

如果我省略中的方括号,则会出现类似的错误

"defaultValue": "$env:THUMBPRINT",
Run Code Online (Sandbox Code Playgroud)

bmo*_*sft 5

您需要在部署任务上设置覆盖参数。删除您添加到模板中的所有默认值。您的任务 yaml 将类似于:

- task: AzureResourceManagerTemplateDeployment@3
  inputs:
    deploymentScope: 'Resource Group'
    action: 'Create Or Update Resource Group'
    overrideParameters: '-certificateThumbprint $(Thumbprint) -sourceVaultResourceId $(vaultId) -certificateUrlValue $(certUrl)'
Run Code Online (Sandbox Code Playgroud)

$(paren) 语法是您在任务定义中引用变量的方式 - 因此将它们更改为您命名的变量。