0

动态创建的变量不会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)
  • ${{ 变量.sasToken }}
  • $[ 变量.sasToken ]

这些都不起作用,而且sasToken总是空的。

输出网址如下所示:

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

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

我究竟做错了什么?

4

1 回答 1

0

我很抱歉,我的示例比使用作业和任务的实际 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)"

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

于 2020-08-11T12:05:56.297 回答