在yaml pipeline中,如果需要在阶段中设置变量并在后续的任务中使用,需要使用jobs级别的variables节点而不是stage级别的variables节点。
代码示例:
jobs:
- job: myJob
variables:
myVar: "Hello"
steps:
- script: |
echo $(myVar)
echo $(stageVar) # 这里会报错
displayName: "Run a script"
- bash: echo $(myVar)
displayName: "Run a Bash script"
- stage: myStage
displayName: "My stage"
variables:
stageVar: "Hello from stage"
jobs:
- job: myJob
steps:
- script: |
echo $(myVar)
echo $(stageVar)
displayName: "Run a script"