Search code examples
githubgithub-actionscicd

If or condition in Github Actions


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') }}

Solution

  • 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