如果在Github Actions中使用条件语句

18

我一直在尝试在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

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

有没有办法使用ifor条件?

更新:

该条件无效,下面是更新后的条件:

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

你能分享一下你的“rules”阶段/任务吗? - Krzysztof Madej
嘿,问题已经解决了。必须使用以下条件才能使其不在暂存和生产环境中运行。因此,当它处于开发或QA环境中时,它满足以下条件并跳过作业。if: ${{ (needs.rules.outputs.branch_name != 'production') && (needs.rules.outputs.branch_name != 'staging') }}当它处于暂存或生产环境中时,下面的代码片段将告诉操作运行而不是跳过if: ${{ (needs.rules.outputs.branch_name != 'production') ||(needs.rules.outputs.branch_name != 'staging') }} - Sam-Sundar
不是a或不是b将始终评估为true,因为a!= b。如果您想要同时排除它们,请更改逻辑为AND,就像您的评论和建议答案中那样。 - Sandra
1个回答

18

嘿,我也尝试过这个,但它似乎也不起作用。如果 if 没有得到验证,那么它仍然会运行该作业。 - Sam-Sundar
5
根据这个章节,Krzysztof说:"如果表达式中包含任何运算符,则必须使用${{ }}将表达式显式地标记为需要进行求值。" 因此,在这种情况下,您仍然需要使用${{ }} - GuiFalourd
Sam:你试过在表达式开头使用 always() && 吗?根据这篇帖子的说法,它可能会解决问题。 - GuiFalourd
3
嘿 @GuiFalourd 和 @Krzysztof问题已解决。必须使用以下条件才能使它不在暂存和生产环境下运行。因此,当处于开发或QA环境时,它满足以下条件并跳过作业。 if: ${{ (needs.rules.outputs.branch_name != 'production') && (needs.rules.outputs.branch_name != 'staging') }}而当处于暂存或生产环境时,以下代码片段将告诉操作只运行不跳过。if: ${{ (needs.rules.outputs.branch_name != 'production') ||(needs.rules.outputs.branch_name != 'staging') }} - Sam-Sundar
@Sam-Sundar,我更新了我的回复并添加了有效的条件。如果您认为这个问题已经解决,请接受它作为答案,以免打扰其他用户,或者请发布您自己的回复。 - Krzysztof Madej
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接