在Azure管道YAML脚本中使用运行时表达式 [英] Using runtime expressions in azure pipelines yaml script

查看:13
本文介绍了在Azure管道YAML脚本中使用运行时表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过从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:windowssystem32cmd.exe" /D /E:ON /V:OFF /S /C "CALL "d:a\_temp3cb45b74-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:windowssystem32cmd.exe" /D /E:ON /V:OFF /S /C "CALL "d:a\_temp5e42dc54-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管道YAML脚本中使用运行时表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆