Azure管道:一个模板中的设置变量,在另一个模板中使用它
我尝试实现的目标:
- 模板1中的设置变量功能名称为
set-variable.yml
- 通过变量taperuroName作为commange to amemplate 2 nater
check-variable.yml
- check ock demanturename参数if featurename参数。在模板2。Pipeline.yml中的编译时间表达式中为空
:
trigger:
- '*' # Run on all Branches
pool:
vmImage: ubuntu-latest
stages:
- stage: test1
jobs:
- job: test1
steps:
- template: set-variable.yml
- template: check-variable.yml
parameters:
featureName: $(setVariables.featureName)
set-variable.yml:
steps:
- script: |
featureName=''
echo "##vso[task.setvariable variable=featureName;isOutput=true]$featureName"
displayName: Set variable
name: setVariables
check-variable.yml:
parameters:
- name: featureName
type: string
default: ''
steps:
- ${{ if ne(parameters.featureName, '') }}:
- script: |
echo "This should not be executed, but it is!"
displayName: "featureName != ''"
问题是,步骤farmaturename!='''
in in
变量中,check-variable.yml
被执行,即使我在farmitureName
set-variables.yml
模板要为空。
(通过变量featuRename
作为参数to模板check-variable.yml
正在工作,请参见下面的调试尝试)
=== debugging 1 ===
用于调试目的,我已经通过一些调试任务扩展了check-variable.yml
。
check-variable.yml:
parameters:
- name: featureName
type: string
default: ''
steps:
- script: |
echo "Check feature"
echo "- featureNameDotted: [${{ parameters.featureName }}]"
echo "- featureNameArray: [${{ parameters['featureName'] }}]"
echo "- featureNameEnvDotted: [$featureNameEnvDotted]"
echo "- featureNameEnvArray: [$featureNameEnvArray]"
echo "- json: [${{ convertToJson(parameters.featureName) }}]"
echo "- length: [${{ length(parameters.featureName) }}]"
echo
if [[ -z $featureNameEnvDotted ]]; then
echo "featureNameEnvDotted is (empty or null)"
else
echo "featureNameEnvDotted not (empty or null)"
fi
echo
if [[ $featureNameEnvDotted = '' ]]; then
echo "featureNameEnvDotted = ''"
else
echo "featureNameEnvDotted != ''"
fi
echo
if [[ $featureNameEnvDotted = 'hello' ]]; then
echo "featureNameEnvDotted = 'hello'"
else
echo "featureNameEnvDotted != 'hello'"
fi
env:
featureNameEnvDotted: ${{ parameters.featureName }}
featureNameEnvArray: ${{ parameters['featureName'] }}
displayName: "Debug: All"
- ${{ if eq(parameters.featureName, '') }}:
- script: |
echo
env:
featureName: ${{ parameters.featureName }}
displayName: "Debug: Empty!!"
- ${{ if ne(parameters.featureName, '') }}:
- script: |
echo
env:
featureName: ${{ parameters.featureName }}
displayName: "Debug: Not empty!!"
- ${{ if eq(parameters.featureName, 'hello') }}:
- script: |
echo
env:
featureName: ${{ parameters.featureName }}
displayName: "Debug: Equals 'hello'"
- ${{ if ne(parameters.featureName, '') }}:
- script: |
echo "This should not be executed, but it is!"
displayName: "featureName != ''"
任务的输出debug:all
is:
检查功能
- 特色:[]
- featurenamearray:[]
- featurenameenvdotted:[]
- featurenameenvarray:[]
- JSON:[]
- 长度:[27]
taperunameenvdotted是(空或null)
taperunameenvdotted =''
farmaturenameenvdotted!='Hello'
''''''''''''''''''''''''''''''''''''''''''''' 。 featurename是空的,它不是“你好”。我不明白的“长度”。
并执行以下任务:
- 设置变量
- 调试:所有
- 调试:不为空!
- farmatureName!=''
为什么任务调试:不空!代码>不是吗? featurename参数IMO是空的!
===调试2 ===
我已经离开了check-variable.yml
相同。但是我在set-variable.yml
模板中设置featuRename
将其设置为“ hello”。有了这个,我想检查变量farmatureName
的确会正确传递到模板的参数check-variable.yml
set-variable.yml:
steps:
- script: |
featureName='hello'
echo "##vso[task.setvariable variable=featureName;isOutput=true]$featureName"
displayName: Set variable
name: setVariables
任务的输出调试:ALL
是:
检查功能
- 特色:[Hello]
- featurenamearray:[Hello]
- featurenameenvdotted:[Hello]
- featurenameenvarray:[Hello]
- JSON:[Hello]
- 长度:[27]
featurenameenvdotted not(空或null)
taperunameenvdotted!=''
taperunameenvdotted ='Hello'
featurenameenvarray 再次看起来很好。该参数确实具有“ Hello”值,并且IF看起来不错(我不理解的长度)。
在这里执行执行的任务列表:
- 设置变量
- 调试:所有
- 调试:不为空!
- farmaturename!=''
为什么debug:等于'Hello'
未执行?
parameters.featurename
保留类似“ Hello”的东西,但并不是字符串'Hello'。我在这里做错了吗?
What I tried to achive:
- Set variable featureName in template 1 called
set-variable.yml
- Pass variable featureName as parameter to template 2 called
check-variable.yml
- Check if featureName parameter is empty in a compile time expression in template 2.
pipeline.yml:
trigger:
- '*' # Run on all Branches
pool:
vmImage: ubuntu-latest
stages:
- stage: test1
jobs:
- job: test1
steps:
- template: set-variable.yml
- template: check-variable.yml
parameters:
featureName: $(setVariables.featureName)
set-variable.yml:
steps:
- script: |
featureName=''
echo "##vso[task.setvariable variable=featureName;isOutput=true]$featureName"
displayName: Set variable
name: setVariables
check-variable.yml:
parameters:
- name: featureName
type: string
default: ''
steps:
- ${{ if ne(parameters.featureName, '') }}:
- script: |
echo "This should not be executed, but it is!"
displayName: "featureName != ''"
The problem is, that the step featureName != ''
in check-variable.yml
gets executed, even though I set the featureName
variable in the set-variables.yml
template to be empty.
(Passing of the variable featureName
as a parameter to template check-variable.yml
is working, see debugging attempts below)
=== Debugging 1 ===
For debugging purpose, I've expanded the check-variable.yml
with some debugging tasks.
check-variable.yml:
parameters:
- name: featureName
type: string
default: ''
steps:
- script: |
echo "Check feature"
echo "- featureNameDotted: [${{ parameters.featureName }}]"
echo "- featureNameArray: [${{ parameters['featureName'] }}]"
echo "- featureNameEnvDotted: [$featureNameEnvDotted]"
echo "- featureNameEnvArray: [$featureNameEnvArray]"
echo "- json: [${{ convertToJson(parameters.featureName) }}]"
echo "- length: [${{ length(parameters.featureName) }}]"
echo
if [[ -z $featureNameEnvDotted ]]; then
echo "featureNameEnvDotted is (empty or null)"
else
echo "featureNameEnvDotted not (empty or null)"
fi
echo
if [[ $featureNameEnvDotted = '' ]]; then
echo "featureNameEnvDotted = ''"
else
echo "featureNameEnvDotted != ''"
fi
echo
if [[ $featureNameEnvDotted = 'hello' ]]; then
echo "featureNameEnvDotted = 'hello'"
else
echo "featureNameEnvDotted != 'hello'"
fi
env:
featureNameEnvDotted: ${{ parameters.featureName }}
featureNameEnvArray: ${{ parameters['featureName'] }}
displayName: "Debug: All"
- ${{ if eq(parameters.featureName, '') }}:
- script: |
echo
env:
featureName: ${{ parameters.featureName }}
displayName: "Debug: Empty!!"
- ${{ if ne(parameters.featureName, '') }}:
- script: |
echo
env:
featureName: ${{ parameters.featureName }}
displayName: "Debug: Not empty!!"
- ${{ if eq(parameters.featureName, 'hello') }}:
- script: |
echo
env:
featureName: ${{ parameters.featureName }}
displayName: "Debug: Equals 'hello'"
- ${{ if ne(parameters.featureName, '') }}:
- script: |
echo "This should not be executed, but it is!"
displayName: "featureName != ''"
The output of the task Debug: All
is:
Check feature
- featureNameDotted: []
- featureNameArray: []
- featureNameEnvDotted: []
- featureNameEnvArray: []
- json: []
- length: [27]
featureNameEnvDotted is (empty or null)
featureNameEnvDotted = ''
featureNameEnvDotted != 'hello'
The bash if's work all well. featureName is empty and it is not 'hello'. The 'length' one I don't understand.
And the following tasks get executed:
- Set variable
- Debug: All
- Debug: Not empty!!
- featureName != ''
Why do the tasks Debug: Not empty!!
and featureName != ''
get executed, but task Debug: Empty!!
not? featureName parameter IMO is empty!
=== Debugging 2 ===
I've left check-variable.yml
the same. But I set featureName
in the set-variable.yml
template to the value "hello". With this I wanted to check if the variable featureName
does get properly passed to the parameter of the template check-variable.yml
set-variable.yml:
steps:
- script: |
featureName='hello'
echo "##vso[task.setvariable variable=featureName;isOutput=true]$featureName"
displayName: Set variable
name: setVariables
The output of the task Debug: All
is:
Check feature
- featureNameDotted: [hello]
- featureNameArray: [hello]
- featureNameEnvDotted: [hello]
- featureNameEnvArray: [hello]
- json: [hello]
- length: [27]
featureNameEnvDotted not (empty or null)
featureNameEnvDotted != ''
featureNameEnvDotted = 'hello'
This again looks well. The parameter does hold the value 'hello' and the if's look good (still, length I don't understand).
And here the list of tasks which get executed:
- Set variable
- Debug: All
- Debug: Not empty!!
- featureName != ''
Why is Debug: Equals 'hello'
not executed?
parameters.featureName
holds something like the value 'hello', but it is not exactly the string 'hello'. Am I doing something wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现了这个问题。
$ {{length(parameters.featurename)}}}
评估27的将我推向正确的方向。 27是字符串$(setVariables.featurename)
的长度,该将传递给模板check-variable.yml
。可以用以下表达式对此进行测试,我将其放入
check-variable.yml
:始终执行时,设置了featurename或它是空的。
我认为发生的事情是,当
$ {{...}}}
等编译时间表达式在模板中评估时,参数featauroName被评估为$(setVariables.featurename)。在
$ {{if ...}}
表达式以及带有$ {{parameters ...}}
的脚本任务中,情况就是这种情况。评估了$ {{...}}
表达式之后,它开始执行脚本任务。在那里,它找到了变量$(setVariables.featurename)
并进行评估。但是$(setVariables.featurename)
对编译时间表达式从未使用if语句进行评估。那里只需以变量的名称。因此,为了使它上班,我在脚本任务中设置变量并使用任务条件进行评估时进行了工作。
check-variable.yml:
I found the problem. The
${{ length(parameters.featureName) }}
which evaluates to 27 pushed me in the right direction. 27 is the length of the string$(setVariables.featureName)
which is passed to the templatecheck-variable.yml
.This can be tested with the following expression which I put into
check-variable.yml
:This always executes, when featureName was set or if it was empty.
I think what happens is that when the compile time expressions like
${{ ... }}
are evaluated inside a template, the parameter featureName is evaluated to$(setVariables.featureName)
. This is the case in the${{ if ... }}
expressions as well as in the script tasks with${{ parameters...}}
. After the${{ ... }}
expressions have been evaluated, it starts executing the script tasks. There it finds the variable$(setVariables.featureName)
and evaluates it. But evaluation of the$(setVariables.featureName)
is never done for the compile time expressions with an if statement. There it just takes the name of the variable.So to get it to work I did a work around where I set a variable in a script task, and use a task condition to evaluate it.
check-variable.yml: