在发布管道中的 Azure CLI 任务中使用秘密变量

问题描述

在我的发布管道中,我有两个变量 adminLogin 和 adminPassword,它们被标记为机密。 我在同一发布管道中有一个 Azure CLI@2 类型的任务。它是一个在 Ubuntu 代理上运行的内联 Powershell Core 脚本(我之前尝试过 Windows 代理,但遇到了同样的问题)。 目的是部署一个二头肌模板,发送秘密变量 adminLogin 和 adminPassword 作为参数。

问题是我无法访问任务中的秘密变量。 我试着像这样直接访问它

Write-Host "##[warning]Using an input-macro works: $(adminLogin)"

但这没有用。 我也尝试映射一个环境变量

#Your build pipeline references a secret variable named ‘adminLogin’. Create or edit the build pipeline for this YAML file,define the variable on the Variables tab,and then select the option to make it secret. See https://go.microsoft.com/fwlink/?linkid=865972
variables:
  resourceGroupName: '...'
  environment: 'Test'
  webSku: 'B1'
  maxVCores: '1'
  databaseName: '...'
  applicationLogsRetentionInMB: '50'
  databaseAutoTurnOffDelay: '60'
  databaseMaxSizeInGiB: '10'

steps:
- task: AzureCLI@2
  displayName: 'Deploy bicep template'
  inputs:
    azureSubscription: '....'
    scriptType: pscore
    scriptLocation: inlineScript
    inlineScript: |
     Write-Host "##[warning]Using an input-macro works: $(adminLogin)"
     Write-Host "##[warning]Using the mapped env var for this task works and is recommended: $env:LOGIN"
     az deployment group create  `
     --template-file $(System.DefaultWorkingDirectory)/..../deployment.bicep `
     --resource-group $(resourceGroupName) `
     --parameters '{  \"environment\":{ \"value\": \"$(environment)\"},\"adminLogin\":{ \"value\": \"$env:LOGIN\"},\"adminPassword\":{ \"value\": \"$env:PASSWORD\"},\"webSku\":{ \"value\": \"$(webSku)\"},\"maxVCores\":{ \"value\": $(maxVCores)},\"databaseName\":{ \"value\": \"$(databaseName)\"},\"applicationLogsRetentionInMB\":{ \"value\": $(applicationLogsRetentionInMB)},\"databaseAutoTurnOffDelay\":{ \"value\": $(databaseAutoTurnOffDelay)},\"databaseMaxSizeInGiB\":{ \"value\": $(databaseMaxSizeInGiB)}}'
  env:
    LOGIN: $(adminLogin)
    PASSWORD: $(adminPassword)

部署二头肌模板时未发送 adminLogin 和 adminPassword,我收到此错误消息

{
  "code": "DeploymentFailed","message": "At least one resource deployment operation failed. Please list deployment operations for details. Please see https://aka.ms/DeployOperations for usage details.","details": [
    {
      "code": "InvalidParameterValue","message": "Invalid value given for parameter Login. Specify a valid parameter value."
    }
  ]
}

所以我有两个问题

  1. 如何更改我的 Azure CLI 任务以使其可以访问秘密变量?
  2. 从支持秘密变量的发布管道部署二头肌模板是否有不同的方法?

解决方法

你能试试这个吗:

steps:
- task: AzureCLI@2
  displayName: 'Deploy bicep template'
  inputs:
    azureSubscription: '....'
    scriptType: pscore
    scriptLocation: inlineScript
    inlineScript: |
     Write-Host "##[warning]Using an input-macro works: $(adminLogin)"
     Write-Host "##[warning]Using the mapped env var for this task works and is recommended: $env:LOGIN"
     az deployment group create  `
     --template-file $(System.DefaultWorkingDirectory)/..../deployment.bicep `
     --resource-group $(resourceGroupName) `
     --parameters '{  \"environment\":{ \"value\": \"$(environment)\"},\"adminLogin\":{ \"value\": \"$(adminLogin)\"},\"adminPassword\":{ \"value\": \"$(adminPassword)\"},\"webSku\":{ \"value\": \"$(webSku)\"},\"maxVCores\":{ \"value\": $(maxVCores)},\"databaseName\":{ \"value\": \"$(databaseName)\"},\"applicationLogsRetentionInMB\":{ \"value\": $(applicationLogsRetentionInMB)},\"databaseAutoTurnOffDelay\":{ \"value\": $(databaseAutoTurnOffDelay)},\"databaseMaxSizeInGiB\":{ \"value\": $(databaseMaxSizeInGiB)}}'

我使用了 Azure DevOops 变量而不是环境变量。

,

Azure CLI 接受 parameterName=value 形式的参数,也许您应该尝试使用它来传递秘密而不是内联 json。你把它放在撇号里,所以里面的 shell 变量不会被解析,这可能会导致你的错误。

此外,您可以将非秘密参数放入 json 文件中,并在 --parameters 中仅传递该文件的名称。 CLI 将检测并读取文件以获取参数。秘密作为键=值条目传递。

将机密传递给部署的其他选项是在 json 文件中使用密钥保管库引用:https://docs.microsoft.com/en-us/azure/azure-resource-manager/templates/template-tutorial-use-key-vault(如果机密在某个密钥保管库中)。如果您需要在入口文件(在您的情况下为 deployment.bicep)中使用秘密,则使用参考,但如果秘密被模块使用,您可以在 bicep 中使用 getSecret 函数(从 0.4 开始):{{3}并作为参数传递名称和密钥库的 RG 和订阅以及密钥的名称。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...