如果变量不等于null,则azure devops yaml运行步骤 - 如何在条件下引用yaml变量

发布于 2025-02-11 15:08:04 字数 437 浏览 2 评论 0原文

如果数字变量具有值,我想在模板中运行此步骤。问题似乎是即使设置(如下面)变量,number似乎仍然是空的。我认为我需要另一种方法来引用此变量?我认为,如果我通过GUI设置变量,但似乎并没有从YAML捡起它,这将有效。有人知道吗?

yaml

stages:
- stage: Run
  variables:
    - name: number
      value: '123'
  jobs:
  - job:
    steps:
    - template: .\run.yml

模板

- ${{if ne(variables.number, '')}}:
    - powershell: Write-Host "Its working!"

I want to run this step in a template if the number variable has a value. Issue seems to be that even when set, like below, variables.number still seems to be empty. I think I need another way to reference this variable? I think this would work if I set the variable via the GUI but doesn't seem to pick it up from the YAML. Does anybody know?

YAML

stages:
- stage: Run
  variables:
    - name: number
      value: '123'
  jobs:
  - job:
    steps:
    - template: .\run.yml

TEMPLATE

- ${{if ne(variables.number, '')}}:
    - powershell: Write-Host "Its working!"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最佳男配角 2025-02-18 15:08:04

您需要这样使用:

stages:
- stage: Run
  variables:
    - name: number
      value: '123'
  jobs:
  - job:
    steps:
    - ${{if ne(variables.number, '')}}:
       - template: .\run.yml

从模板无法访问变量。

You need to use it like this:

stages:
- stage: Run
  variables:
    - name: number
      value: '123'
  jobs:
  - job:
    steps:
    - ${{if ne(variables.number, '')}}:
       - template: .\run.yml

Variables is not accessible from the template.

岁月静好 2025-02-18 15:08:04

azure devops yaml运行步骤,如果变量不等于null-如何参考条件中的yaml变量

则需要注意单引号的等效性

由于将比较值设置为空'',因此您的变量需要引用'variables.number',否则其条件始终为false :

- ${{if ne('variables.number', '')}}:

我的测试YAML脚本:

stages:
- stage: Run
  variables:
    - name: number
      value: '123'
  jobs:
  - job:
    steps:
        - template: child.yml

child.yml:

steps:
- ${{if ne('variables.number', '')}}:
    - script: echo works fine.

它在我这边工作正常。

Azure DevOps YAML run step if variable not equal null - how to reference yaml variable in condition

You need to pay attention to the equivalence of single quotes.

Since you set the comparison value to be empty '', then your variable needs to be quoted 'variables.number' , otherwise its condition is always False:

- ${{if ne('variables.number', '')}}:

My test YAML scripts:

stages:
- stage: Run
  variables:
    - name: number
      value: '123'
  jobs:
  - job:
    steps:
        - template: child.yml

child.yml:

steps:
- ${{if ne('variables.number', '')}}:
    - script: echo works fine.

It works fine on my side.

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文