If else in Github workflow

We have if condition GitHub workflow.
Can we have if else block or switch case also?

Not as such, no. There are three approaches I’ve used, depending on which makes the most sense in the given context:

      - name: conditional command
        run: |
          if $CONDITION; then
              # run the one command
          else
              # run the other command
          fi
  • A mix of the two others: Have one step set a condition variable (environment variable or output) and check that in the if condition of later steps. That way you can encapsulate potentially complex conditions in a step, and have simple if conditions on the later steps.

In certain cases a pseudo-ternary might work for you, e.g.

${{ env.FOO == 'foo' && 'yes' || 'no }}

This is roughly equivalent to FOO == 'foo' ? 'yes' : 'no in JavaScript.

Not really helpful for if conditions, but if you want to evaluate to a value then it can be handy at times.