azure-devops - 在 azure 管道 yaml 脚本中使用运行时表达式

标签 azure-devops azure-pipelines

我正在尝试通过从 azure 管道传递变量来执行脚本。这是我的简单测试管道:

trigger:
- master

pool:
  vmImage: 'windows-latest'

variables:
    major: 1.2
    minor: $[counter(variables['major'], 1)]
    version: $[format('{0}.{1}', variables.major, variables.minor)]

name: $[format('{0} v{1}', 'Yaml Testing', variables['version'])]

steps:

- script: |
    echo variables['version']
    echo $(variables.version)
    echo '$(variables.version)'
    echo "$(variables.version')"
    echo $[ variables['version'] ]
    echo ${{ variables['version'] }}
    echo $(Build.BuildNumber)
  displayName: 'Run a multi-line script'

- script:     $[format('{0} {1}', 'echo', variables['version'])]
  displayName: 'Echo Formatted String'

脚本的输出是:
Generating script.
========================== Starting Command Output ===========================
##[command]"C:\windows\system32\cmd.exe" /D /E:ON /V:OFF /S /C "CALL "d:\a\_temp\3cb45b74-f6cd-4d2f-bf65-f635779b9d86.cmd""
variables['version']
$(variables.version)
'$(variables.version)'
"$(variables.version')"
$[ variables['version'] ]
$[format('{0}.{1}', variables.major, variables.minor)]
Yaml Testing v1.2.11
##[section]Finishing: Run a multi-line script


Generating script.
Script contents:
$[format('{0} {1}', 'echo', variables['version'])]
========================== Starting Command Output ===========================
##[command]"C:\windows\system32\cmd.exe" /D /E:ON /V:OFF /S /C "CALL "d:\a\_temp\5e42dc54-e027-4b9a-9af4-0db02e879b0f.cmd""
'$[format' is not recognized as an internal or external command,
operable program or batch file.
##[error]Cmd.exe exited with code '1'.
##[section]Finishing: Echo Formatted String

奇怪的是,代码在名称中工作正常,但在尝试在脚本中使用时却没有。

我究竟做错了什么?

最佳答案

$[]在运行时评估,这就是它不起作用的原因。您可以将 ${{expression}} 传递给脚本,如下所示:

- script: ${{format('{0} {1}', 'echo', '$(version)')}} 
  displayName: 'Echo Formatted String'
${{}}中的表达式将在解析时进行评估。之前-script实际执行,表达式为${{}}被解析为有效的命令。

可以像这样直接引用自定义变量'$(variableName)'而不是 $(variables.Name)

关于azure-devops - 在 azure 管道 yaml 脚本中使用运行时表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58267591/

相关文章:

azure - 包版本太长

c# - Azure Devops 中针对不同环境的 app.config 转换

azure - 我可以使用 VSTS Release Manager 在我的应用程序服务下部署单个 Web 作业,而不部署整个应用程序服务吗?

azure-devops - Azure DevOps (VSTS) 管道(发布定义)排除某些阶段(环境)的构建标记

azure-devops - 使用部署槽部署 Azure Functions 时如何避免主机重启?

.net - 如何检查是否安装了 .Net 4.7.2 的开发者包?

git - VSTS 构建失败,Git 提取失败,退出代码为 : 128

visual-studio-2013 - 如何在 Visual Studio Online Build 定义中将参数传递给 NuGet?

azure - 如何手动触发 Azure Pipelines 中的单个作业?

c# - 使用数据库文件在 Azure Devops 上运行测试