If or condition in Github Actions
Asked Answered
C

1

25

I've been trying to build a CICD pipeline in Github actions and we're not able to process if and or conditions in the same. below is the example of our code snippet,

    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

So this task should not run in production and staging branch, but when actions runs in staging branch, this job is also getting triggered along with other jobs which are not meant for staging environment.

Is there any way we can have if and or condition ?

Update:

The condition will not work, below updated condition will work.

if: ${{ (needs.rules.outputs.branch_name != 'production') && (needs.rules.outputs.branch_name != 'staging') }}
Cockeyed answered 9/7, 2021 at 12:59 Comment(3)
Could you share your rules stage/job?Gery
Hey, the issue is solved. Had to use below condition for it to not run on staging and production environment. So when it's in Development or QA env, it's satisfying below condition and the job is skipped . yaml if: ${{ (needs.rules.outputs.branch_name != 'production') && (needs.rules.outputs.branch_name != 'staging') }} And when it's in Staging or Production environment below code snippet will tell actions to run and not skip yaml if: ${{ (needs.rules.outputs.branch_name != 'production') ||(needs.rules.outputs.branch_name != 'staging') }} Cockeyed
not a OR not b will always evaluate to true, as a != b. if you want them both excluded, change your logic to AND like in your comment & the suggested answerDelphiadelphic
G
24

You condition should look like below:

   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

However if you find it not working it could be related to this issue - Job-level "if" condition not evaluated correctly if job in "needs" property is skipped

Gery answered 9/7, 2021 at 13:28 Comment(7)
Hey, I tried this too, and it also seems to be not working. If the if is not getting validated, it's still running the job.Cockeyed
Krzysztof: According to this section, If the expression contains any operators, the expression must be contained within ${{ }} to explicitly mark it for evaluation. So you would need to use ${{ }} anyway in this case.Gamut
Sam: Did you try using always() && at the beginning of the expression? According to this post it might resolve the problem.Gamut
Hey @Gamut & @Krzysztof The issue is solved. Had to use below condition for it to not run on staging and production environment. So when it's in Development or QA env, it's satisfying below condition and the job is skipped . yaml if: ${{ (needs.rules.outputs.branch_name != 'production') && (needs.rules.outputs.branch_name != 'staging') }} And when it's in Staging or Production environment below code snippet will tell actions to run and not skip yaml if: ${{ (needs.rules.outputs.branch_name != 'production') ||(needs.rules.outputs.branch_name != 'staging') }} Cockeyed
@Cockeyed I updated my reply with valid condition. Feel free to accept it as answer to do not disturb other user as this case is solved or please publish your own reply.Gery
in my case, I needed to put my env vars on the same level as the steps in my yaml, as otherwise the if inside a step didn't have access to those variables.Delphiadelphic
@Cockeyed if: ${{ (needs.rules.outputs.branch_name != 'production') ||(needs.rules.outputs.branch_name != 'staging') }} - if production, this will evaluate to true, because it's not staging. if staging, it'll also evaluate to true, as it's not production. using the combination!= ... || ...!= will make your logic always evaluate to true (regardless of the branch name)Delphiadelphic

© 2022 - 2024 — McMap. All rights reserved.