关于使用变量引用 YAML 流水线资源并出现奇怪行为的问题。

huangapple go评论97阅读模式
英文:

Question about weird behavior referencing a YAML pipeline resource using a variable for the pipeline resource name

问题

我在使用YAML变量、参数和Azure管道资源引用时遇到了奇怪的行为。以下展示了原始实现与新实现之间的工作差异,新实现只有一行更改。

工作的实现

模板 A(调用模板 B):

- template: Templates\TemplateB.yml
  serviceBuildResourceName: resourceName

模板 B(使用serviceBuildResourceName参数获取管道运行信息):

$projectId = '$(resources.pipeline.${{ parameters.serviceBuildResourceName }}.projectID)'
$pipelineId = '$(resources.pipeline.${{ parameters.serviceBuildResourceName }}.PipelineID)'

在上述实现中,一切都完美工作。

失败的实现

模板 A(调用模板 B):

- template: Templates\TemplateB.yml
  serviceBuildResourceName: $(ServiceBuildResourceName)

模板 B(使用serviceBuildResourceName参数获取管道运行信息):

$projectId = '$(resources.pipeline.${{ parameters.serviceBuildResourceName }}.projectID)'
$pipelineId = '$(resources.pipeline.${{ parameters.serviceBuildResourceName }}.PipelineID)'

唯一的区别在于,不再将硬编码的字符串传递给serviceBuildResourceName参数,而是传递一个变量,该变量的值与之前的相同,即resourceName。该变量在先前的模板中定义如下:

- name: ServiceBuildResourceName
  value: resourceName

但现在在我的管道运行中出现了以下错误:

WARNING: 2023-02-12 15:52:29.5071 Response body: {"$id":"1","innerException":null,"message":"The value is not an integer.
$(resources.pipeline.resourceName.PipelineID)

尽管我知道变量被正确填充,因为上面的错误消息包含了resources.pipeline.resourceName.PipelineID中的"resourceName",但出现了一个错误。似乎它不再识别管道资源,而是将其识别为字符串。

任何关于此问题的帮助或见解将不胜感激,谢谢!

英文:

I am experiencing weird behavior with YAML variables, parameters, and Azure pipeline resource references. The following shows the original implementation that works compared to my new implementation with a single line change that fails.


Working Implementation

Template A (makes a call to template B):

- template: Templates\TemplateB.yml
  serviceBuildResourceName: resourceName

Template B (uses serviceBuildResourceName param to get pipeline run information):

$projectId = '$(resources.pipeline.${{ parameters.serviceBuildResourceName }}.projectID)'
$pipelineId ='$(resources.pipeline.${{ parameters.serviceBuildResourceName }}.PipelineID)'

Template B goes on to use the values in $projectId and $pipelineId (along with other values not listed here since it is irrelevant) to successfully retrieve information about the a pipeline run from the specific pipeline resource, serviceBuildResourceName. Note that all pipeline resources are correctly defined at the beginning yaml file for the pipeline. In this implementation above, everything works perfectly.


Failing Implementation

Template A (makes a call to template B):

- template: Templates\TemplateB.yml
  serviceBuildResourceName: $(ServiceBuildResourceName)

Template B (uses serviceBuildResourceName param to get pipeline run information):

$projectId = '$(resources.pipeline.${{ parameters.serviceBuildResourceName }}.projectID)'
$pipelineId ='$(resources.pipeline.${{ parameters.serviceBuildResourceName }}.PipelineID)'

Note that the only difference is the following: instead of passing the hard-coded string into the serviceBuildResourceName parameter, I pass in a variable, which has the same value as before, resourceName. The variable is defined in an earlier template as such:

- name: ServiceBuildResourceName
  value: resourceName

I feel it should still work the same, but I know get the following error in my pipeline run:

WARNING: 2023-02-12 15:52:29.5071 Response body: {"$id":"1","innerException":null,"message":"The value is not an integer.
$(resources.pipeline.resourceName.PipelineID)

I know that the variable is being correctly populated since the error message above contains "resourceName" in resources.pipeline.resourceName.PipelineID, as it should.

However, for reasons unknown to me, it now throughs an error. It seems like it doesn't recognize the pipeline resource, and instead recognizes it as a string.

Any help or insight here would be greatly appreciated, thanks!

答案1

得分: 0

这似乎是因为 YAML 中预定义变量的工作方式。由于resources.pipeline...是一个预定义变量,它在编译时被解析。因此,您不能使用我所做的那样运行时定义的变量。它不会被解析为预定义变量,而会在运行时解析为字符串。

英文:

As far as I can tell, this is because of how predefined variables work in YAML. Since resources.pipeline... is a predefined variable, it gets resolved at compile time. Thus, you can't use run-time defined variables like I am doing. Instead of resolving it as a predefined variable, it will get resolved to be a string at runtime.

huangapple
  • 本文由 发表于 2023年2月14日 01:54:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75439558.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定