【问题标题】:Azure Pipelines yml - Variable doesn't get substituted in overrideParameters part of AzureResourceManagerTemplateDeployment@3Azure Pipelines yml - AzureResourceManagerTemplateDeployment@3 的 overrideParameters 部分中未替换变量
【发布时间】:2020-08-10 15:15:49
【问题描述】:

动态创建的变量不会作为 AzureResourceManagerTemplateDeployment@3 的一部分插入 overrideParameters.

示例模板:

trigger:
- master

pool:
  vmImage: 'ubuntu-latest'

variables:
  resourceGroupName: '...'
  storageAccountName: 'actualAccountName'
  subscriptionId: '...'
  location: '...'

steps:      
- task: PowerShell@2
  displayName: "Set AzurePipeline var 'sasToken' to 1234"
  inputs:
    targetType: 'inline'
    script: |            
      $token = 1234;
      Write-Host "##vso[task.setvariable variable=sasToken;]$token"

- task: PowerShell@2
  displayName: "Testing that sasToken is set"
  inputs:
    targetType: 'inline'
    script: |            
      Write-Host "Token is: $(sasToken)"
      
- task: AzureResourceManagerTemplateDeployment@3
  inputs:
    deploymentScope: 'Resource Group'
    azureResourceManagerConnection: 'AzureRMServiceConnection'
    subscriptionId: '$(subscriptionId)'
    action: 'Create Or Update Resource Group'
    resourceGroupName: '$(resourceGroupname)'
    location: $(location)
    templateLocation: 'Linked artifact'
    csmFile: 'azuretemplates/azuredeploy.json'
    overrideParameters: >-
      -appServiceTemplateUri https://$(storageAccountName).blob.core.windows.net/templates/linked-appservice-template.json?$(sasToken)
    deploymentMode: 'Incremental' 

如您所见,有一个override parameters 部分,我尝试使用各种方式附加$sasToken

到目前为止我已经尝试过:

  • $(sasToken)
  • ${{ variables.sasToken }}
  • $[ variables.sasToken ]​​i>

这些都不起作用,sasToken 始终为空。

输出网址如下所示:

https://actualAccountName.blob.core.windows.net/templates/linked-appservice-template.json?$(sasToken)

注意:$(storageAccountName) 被替换,而 $(sasToken) 没有。

我做错了什么?

【问题讨论】:

  • 您是否将sasToken 定义为机密?
  • 是的,它是一个有效期为 10 分钟的不记名令牌
  • 只是为了测试,可以改成non-secret吗?我要求这样做是因为这种语法 $(sasToken) 应该可以工作,但机密是安全的,以避免被泄露。
  • 嗨克里斯蒂安,我已经测试了你分享的代码,我可以得到价值。也许你可以在变量中定义sasToken,并添加power shell脚本来更新值,然后在overrideParameters中使用sasToken。
  • 嗨@VitoLiu-MSFT 毕竟我找到了一个解决方案,很抱歉造成混淆,示例过于简单。我在下面添加了一个共鸣。

标签: azure-devops azure-pipelines azure-pipelines-yaml azure-devops-yaml


【解决方案1】:

我很抱歉,我的示例比实际使用作业和任务的 yml 过于简单。

解决办法:

使用以下方法创建的变量:

"##vso[task.setvariable variable=sasToken]$token"

默认范围为job

为了在工作之间共享变量,需要执行几个步骤:

      - job: buildJob               #1. name your source job
        steps:
        - task: PowerShell@2
          name: obtainSasTokenTask  #2  name your source task
          inputs:
            targetType: 'inline'    #4  add isOutput=true;
            script: '##vso[task.setvariable variable=sasToken;isOutput=true]$token'
      - job: build                  #5  name your target job
        dependsOn: ['buildJob']     #6  create a dependency on prev job
        variables:                  #7  create scoped variables an reference your sasToken
            sasToken: $[dependencies.buildJob.outputs['obtainSasTokenTask.sasToken'] ]
        steps:
          - task: PowerShell@2
          inputs:
            targetType: 'inline'
            script: |               #8  use your local $(sasToken)
              Write-Host "Token is: $(sasToken)"

这是一个可怕的多步骤过程,但我还没有找到更简洁且不易出错的语法。

【讨论】:

  • 感谢分享,可以标记为答案,这样其他人可以直接找到有用的解决方案。
猜你喜欢
  • 1970-01-01
  • 2021-12-01
  • 1970-01-01
  • 2020-06-09
  • 2020-03-13
  • 1970-01-01
  • 2021-02-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多