【问题标题】:Using runtime expressions in azure pipelines yaml script在 azure 管道 yaml 脚本中使用运行时表达式
【发布时间】:2021-08-03 19:30:20
【问题描述】:

我正在尝试通过从 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

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

我做错了什么?

【问题讨论】:

  • 该脚本除了获取脚本外,​​不是azure devops表达式。
  • 我明白这一点。但是,真的不可能使用表达式构建该脚本,然后将其传递给执行吗?
  • 我不认为这是可能的:/ 但我不确定......

标签: azure-devops azure-pipelines


【解决方案1】:

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

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

${{}} 中的表达式将在解析时进行评估。在-script 实际执行之前,表达式为 ${{}} 被解析为有效命令。

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

【讨论】:

  • 您的示例评估脚本内容:“echo $(version)”
  • @Algis 所以,解决方案有效,对吧?如果是,您可以将其标记为答案,这可能对阅读此主题的其他社区成员有所帮助。
  • 不,这是不对的,因为 $(version) 没有评估为版本号
  • @Algis $(version) 的值是什么?我可以知道变量版本在您的管道中是如何定义的吗?
  • @LeviLu-MSFT 完整测试脚本在我的问题中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-26
  • 2021-01-20
  • 2022-12-01
  • 2022-11-03
  • 1970-01-01
  • 2023-03-05
相关资源
最近更新 更多