【问题标题】:If or condition in Github ActionsGithub Actions 中的 if 或条件
【发布时间】:2021-09-19 20:11:36
【问题描述】:

我一直在尝试在 Github 操作中构建 CICD 管道,但我们无法同时处理 if 和 or 条件。下面是我们代码sn-p的例子,

    name: Build Non prod
    needs: [rules]    
    if: ${{ (needs.rules.outputs.branch_name != 'production') || (needs.rules.outputs.branch_name != 'staging') }}
    steps:
      - name: Checkout
        uses: actions/checkout@v2

因此,此任务不应在 productionstaging 分支中运行,但当操作在 staging 分支中运行时,此作业也会与其他不适用于 staging 环境的作业一起触发。

有什么方法可以让我们拥有ifor 条件?

更新:

条件无效,以下更新的条件将有效。

if: ${{ (needs.rules.outputs.branch_name != 'production') && (needs.rules.outputs.branch_name != 'staging') }}

【问题讨论】:

  • 你能分享你的rules舞台/工作吗?
  • 嘿,问题解决了。必须使用以下条件才能使其无法在登台和生产环境中运行。因此,当它在 Development 或 QA 环境中时,它满足以下条件并且工作被跳过。 yaml if: ${{ (needs.rules.outputs.branch_name != 'production') && (needs.rules.outputs.branch_name != 'staging') }} 当它在暂存或生产环境中时,代码 sn-p 将告诉操作运行而不是跳过 yaml if: ${{ (needs.rules.outputs.branch_name != 'production') ||(needs.rules.outputs.branch_name != 'staging') }}

标签: github-actions cicd


【解决方案1】:

您的情况应如下所示:

   name: Build Non prod
    needs: [rules]    
    if: ${{ (needs.rules.outputs.branch_name != 'production') && (needs.rules.outputs.branch_name != 'staging') }}
    steps:
      - name: Checkout
        uses: actions/checkout@v2

但是,如果您发现它不起作用,则可能与此问题有关 - Job-level "if" condition not evaluated correctly if job in "needs" property is skipped

【讨论】:

  • 嘿,这个我也试过了,好像也没用。如果 if 没有得到验证,它仍在运行。
  • Krzysztof:根据this sectionIf the expression contains any operators, the expression must be contained within ${{ }} to explicitly mark it for evaluation。所以在这种情况下,无论如何你都需要使用${{ }}
  • Sam:你试过在表达式的开头使用always() &&吗?根据this post,它可能会解决问题。
  • 嘿@GuiFalourd & @Krzysztof 问题解决了。必须使用以下条件才能使其无法在登台和生产环境中运行。因此,当它在 Development 或 QA 环境中时,它满足以下条件并且工作被跳过。 yaml if: ${{ (needs.rules.outputs.branch_name != 'production') && (needs.rules.outputs.branch_name != 'staging') }} 当它在暂存或生产环境中时,代码 sn-p 将告诉操作运行而不是跳过 yaml if: ${{ (needs.rules.outputs.branch_name != 'production') ||(needs.rules.outputs.branch_name != 'staging') }}
  • @Sam-Sundar 我用有效条件更新了我的回复。请随意接受它作为不要打扰其他用户的答案,因为这种情况已经解决,或者请发布您自己的回复。
猜你喜欢
  • 2021-10-05
  • 2020-03-10
  • 2021-09-10
  • 2021-06-18
  • 2020-01-14
  • 2021-09-02
  • 1970-01-01
  • 2021-01-11
  • 1970-01-01
相关资源
最近更新 更多