英文:
Correct syntax to reference an azure pipeline variable inside an expression
问题
variables:
- name: MyVar
${{ if and(eq(variables['foo'], 'bar'), eq(parameters.environment, 'xyz')) }}:
value: abc
${{ if and(eq(variables['foo'], 'baz'), eq(parameters.environment, 'pqr')) }}:
value: def
英文:
I have a block like the following, where foo
is a pipeline variable created in AzureDevOps and I want to access the value in my yaml file
variables:
- name: MyVar
${{ if and(eq(variables['foo'], 'bar') , eq(parameters.environment, 'xyz')) }}:
value: abc
${{ if and(eq(variables['foo'], 'baz') , eq(parameters.environment, 'pqr')) }}:
value: def
the above doesn't seem to be the correct syntax though, as my variable MyVar
remains empty. What is the correct syntax? Apart from the one above, I have tried the following without success
variables:
- name: MyVar
${{ if and(eq(variables.foo, 'bar') , eq(parameters.environment, 'xyz')) }}:
value: abc
${{ if and(eq(variables.foo, 'baz') , eq(parameters.environment, 'pqr')) }}:
value: def
and
variables:
- name: MyVar
${{ if and(eq($(foo), 'bar') , eq(parameters.environment, 'xyz')) }}:
value: abc
${{ if and(eq($(foo), 'baz') , eq(parameters.environment, 'pqr')) }}:
value: def
答案1
得分: 1
正如Daniel所写,您混合了编译时表达式和运行时表达式。
在编译时表达式${{ }}
中使用变量是可能的,但它必须是静态变量。
variables:
staticVar: 'my value' # 静态变量
compileVar: ${{ variables.staticVar }} # 编译时表达式
isMain: $[eq(variables['Build.SourceBranch'], 'refs/heads/main')] # 运行时表达式
steps:
- script: |
echo ${{variables.staticVar}} # 输出 my value
echo $(compileVar) # 输出 my value
echo $(isMain) # 输出 True
请参阅模板表达式文档。
使用模板表达式来指定在管道初始化期间动态解析值的方式。将您的模板表达式包装在此语法内:
${{ }}
。
模板表达式可以展开模板参数,还可以展开变量。您可以使用参数来影响模板的展开方式。参数对象的工作方式类似于表达式中的变量对象。只有预定义的变量可以在模板表达式中使用。
这可能会与运行时参数有些误导,它们建议属于运行时评估:
参数仅在模板解析时可用。参数在管道运行之前扩展,因此由
${{ }}
括起来的值将被参数值替换。如果需要在管道运行期间广泛使用您的值,请使用变量。
英文:
As Daniel wrote you are mixing compile-time expressions with run-time expressions.
This is possible to use variables in compile-time expression which is ${{ }}
, but it has to be static variable.
variables:
staticVar: 'my value' # static variable
compileVar: ${{ variables.staticVar }} # compile time expression
isMain: $[eq(variables['Build.SourceBranch'], 'refs/heads/main')] # runtime expression
steps:
- script: |
echo ${{variables.staticVar}} # outputs my value
echo $(compileVar) # outputs my value
echo $(isMain) # outputs True
Please take a look on template expression doc
> Use template expressions to specify how values are dynamically resolved during pipeline initialization. Wrap your template expression inside this syntax: ${{ }}.
>
> Template expressions can expand template parameters, and also variables. You can use parameters to influence how a template is expanded. The parameters object works like the variables object in an expression. Only predefined variables can be used in template expressions.
It could be somehow misleading with runtime parameters which suggest belonging to run-time evaluation:
> Parameters are only available at template parsing time. Parameters are expanded just before the pipeline runs so that values surrounded by ${{ }} are replaced with parameter values. Use variables if you need your values to be more widely available during your pipeline run.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论