【问题标题】:Using secret variable in Azure CLI task in release pipeline在发布管道中的 Azure CLI 任务中使用机密变量
【发布时间】:2021-06-04 07:59:20
【问题描述】:

在我的发布管道中,我有两个变量 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. 是否有其他方法可以从支持秘密变量的发布管道部署二头肌模板?

【问题讨论】:

    标签: azure azure-devops azure-bicep


    【解决方案1】:

    你可以试试这个:

    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 变量而不是环境变量。

    【讨论】:

    • 这产生了与以前相同的结果和错误消息。这个documentation 包含以下内容: > 在 YAML 管道中,您可以在根、阶段和作业级别设置变量。您还可以在 UI 中指定 YAML 管道之外的变量。当您在 UI 中设置变量时,该变量可以被加密并设置为机密。秘密变量不会在 YAML 管道中自动解密,需要使用 env: 或根级别的变量传递给您的 YAML 文件。
    【解决方案2】:

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

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

    将机密传递给部署的其他选项是使用 json 文件中的密钥库引用:https://docs.microsoft.com/en-us/azure/azure-resource-manager/templates/template-tutorial-use-key-vault(如果机密在某个密钥库中)。如果您需要在入口文件(在您的情况下为 deployment.bicep)中使用秘密,则使用参考,但如果模块使用秘密,则可以在二头肌中使用 getSecret 函数(从 0.4 开始):https://github.com/Azure/bicep/blob/main/docs/spec/modules.md#using-existing-key-vaults-secret-as-input-for-secure-string-module-parameter 并作为参数传递 name 和 RG 以及 key vault 的订阅和 secret 的名称。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-09
      • 2021-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-02
      • 2022-01-21
      相关资源
      最近更新 更多