Azure Devops管道中的动态变量/评估/字符串评估?

时间:2020-03-15 19:02:53

标签: azure-devops azure-pipelines

如何在Azure DevOps管道中进行“评估”?我在一个bash步骤中尝试了eval,它说eval不存在。

例如:

trigger:
- master

pool:
  vmImage: 'windows-latest'

variables:
    var: 'middle'
    start_middle_end: '123'

name: 'test'

steps:
- bash: |
    echo $(start_$(var)_end)
    echo $(start_$[var]_end)
    echo $[start_$(var)_end]
    ${{format('{0} {1}', 'echo', '$(start_$(var)_end)')}}

我想做的是回声123。您可以在上面看到各种尝试。实际的意思是:

2020-03-15T19:00:20.9585338Z /d/a/_temp/704b13e7-2bb0-4099-98a9-292925b35f19.sh: line 1: start_middle_end: command not found
2020-03-15T19:00:20.9721864Z /d/a/_temp/704b13e7-2bb0-4099-98a9-292925b35f19.sh: line 2: start_0_end: command not found
2020-03-15T19:00:20.9741785Z 0
2020-03-15T19:00:20.9864319Z /d/a/_temp/704b13e7-2bb0-4099-98a9-292925b35f19.sh: line 4: start_middle_end: command not found

1 个答案:

答案 0 :(得分:1)

您可以通过PowerShell通过以下方式进行操作:

variables:
    var: 'middle'
    start_middle_end: '123'


steps:
- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      $middleVar= "$(var)"
      $startEndVar= "start_$($middleVar)_end"
      $startEndVarValue = (Get-item env:$startEndVar).Value
      Write-Host $startEndVarValue
      # The output will be: 123