秘密变量可以在其中包含另一个变量的语法而不扩展该变量吗?

问题描述 投票:0回答:1

我在变量组中有一个秘密变量,可以包含格式

$(...)
,但是我希望按字面意思处理它,有没有办法做到这一点?到目前为止我所尝试的一切都扩展了内部变量。

trigger: none

variables:
  - group: SomeVariableGroup # containing variable someVar with value hello$(anotherVar)
  - name: anotherVar
    value: XXX

stages:
  - stage: stage1
    jobs:
      - job: job1
        steps:
          - bash: echo $(someVar) # This should print hello$(anotherVar) NOT helloXXX
azure-pipelines
1个回答
0
投票

使用宏语法

$( )
引用的变量在相同的变量范围中设置是递归扩展 - 这就是为什么运行
echo '$(someVar)'
打印
'helloXXX'
,而不是
'hello$(anotherVar)'

但另一方面,不存在的变量(或在不同范围设置的变量)不会被扩展,即如果找不到

myvar
变量,它将被渲染为
$(myvar)

如果

someVar
anotherVar
用于不同目的,我建议您在作业级别设置/引用这些变量,以使其仅可用于真正需要的作业。

示例:

trigger: none

stages:
  - stage: stage1
    jobs:
      - job: job1
        displayName: 'Job that uses local variable'
        dependsOn: []
        variables:
          - name: anotherVar
            value: XXX
        steps:
          - checkout: none
          - bash: echo '$(someVar)'    #  Output: $(someVar)
          - bash: echo '$(anotherVar)' #  Output: XXX

      - job: job2
        displayName: 'Job that uses variable group'
        dependsOn: []
        variables:
          # Contains variable 'someVar' with value 'hello$(anotherVar)'
          - group: SomeVariableGroup
        steps:
          - checkout: none
          - bash: echo '$(someVar)'    #  Output: hello$(anotherVar)
          - bash: echo '$(anotherVar)' #  Output: $(anotherVar)
© www.soinside.com 2019 - 2024. All rights reserved.