Github Actions 中的 If 或条件

Sam*_*dar 19 github github-actions cicd

我一直在尝试在 Github actions 中构建 CICD 管道,但我们无法在其中处理 if 和 or 条件。下面是我们的代码片段的示例,

    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
Run Code Online (Sandbox Code Playgroud)

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

有什么办法可以拥有ifor条件吗?

更新:

该条件将不起作用,更新后的条件将起作用。

if: ${{ (needs.rules.outputs.branch_name != 'production') && (needs.rules.outputs.branch_name != 'staging') }}
Run Code Online (Sandbox Code Playgroud)

Krz*_*tof 19

您的条件应如下所示:

   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
Run Code Online (Sandbox Code Playgroud)

但是,如果您发现它不起作用,则可能与此问题有关 -如果跳过“needs”属性中的作业,则作业级“if”条件无法正确评估

  • Krzysztof:根据[本节](https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idif),`如果表达式包含任何运算符,则表达式必须是包含在 ${{ }} 中以明确将其标记为评估`。所以在这种情况下你无论如何都需要使用 `${{ }}` 。 (5认同)
  • 嘿@GuiFalourd 和@Krzysztof 问题已解决。必须使用以下条件才能使其无法在临时和生产环境中运行。因此,当它处于开发或 QA 环境中时,满足以下条件并跳过该作业。```yaml if: ${{ (needs.rules.outputs.branch_name != '生产') && (needs.rules.outputs.branch_name != 'staging') }} ``` 当它处于暂存或生产状态时代码片段下面的环境将告诉操作运行而不是跳过 ``yaml if: ${{ (needs.rules.outputs.branch_name != 'product') ||(needs.rules.outputs.branch_name != 'staging' ) }} ``` (3认同)