如何从Teamcity传递变量到Octopus Deploy并正确读取?
解决方法
问题出在PowerShell的字符串插值语法上:当你在双引号字符串里直接写$OctopusParameters["OctopusRegistry"]时,PowerShell会优先把$OctopusParameters解析为整个字典对象,而非读取对应键的取值。你可以通过以下方式修复:
使用子表达式语法:把取值操作放在
$()中,让PowerShell先执行内部的取值逻辑,再将结果插入字符串:Write-Output "OctopusRegistry: $($OctopusParameters["OctopusRegistry"])"改用单引号包裹键名:在双引号字符串内,用单引号作为键名的包裹符,避免和外层双引号冲突:
Write-Output "OctopusRegistry: $OctopusParameters['OctopusRegistry']"
另外也可以先将变量值存入临时变量再输出,写法更直观:
$registryValue = $OctopusParameters["OctopusRegistry"] Write-Output "OctopusRegistry: $registryValue"
确认下TeamCity端的--variable "OctopusRegistry=Snapshot"参数传递是正确的,Octopus已经接收到该变量,只是PowerShell的语法解析问题导致输出异常。
内容的提问来源于stack exchange,提问作者Pablo Estévez
相关产品推荐
相关产品推荐

