'github-action: does the IF have an ELSE?

in github action I have an if, but I still need to run someother thing if I'm in the else case. Is there a clean way to do it or do I have to do another step with the same condition at false?

 - if: contains(['SNAPSHOT'],env.BUILD_VERSION)
   name:IF
   run: echo ":)"
 - if: false == contains(['SNAPSHOT'], env.BUILD_VERSION)
   name: Else
   run: echo ":("


Solution 1:[1]

You can do the following which would only run the script where the condition passes:

job_name:
  runs-on: windows-latest
  if: "!contains(github.event.head_commit.message, 'SKIP SCRIPTS')"    <--- skips everything in this job if head commit message does not contain 'SKIP SCRIPTS'

  steps:
    - uses: ....
  
    - name: condition 1
      if: "contains(github.event.head_commit.message, 'CONDITION 1 MSG')"
      run: script for condition 1

   - name: condition 2
      if: "contains(github.event.head_commit.message, 'CONDITION 2 MSG')"
      run: script for condition 2

and so on. Of course you would use your own condition here.

Solution 2:[2]

You may consider using the haya14busa/action-cond action. It is useful when the if-else operation is needed to set dynamic configuration of other steps (don't need to duplicate the whole step to set different values in a few parameters).

Example:

- name: Determine Checkout Depth
  uses: haya14busa/action-cond@v1
  id: fetchDepth
  with:
    cond: ${{ condition }}
    if_true: '0'  # string value
    if_false: '1' # string value
- name: Checkout
  uses: actions/checkout@v2
  with:
    fetch-depth: ${{ steps.fetchDepth.outputs.value }}

Solution 3:[3]

Alternative solution to github actions based commands, is to use shell script commands for if else statement.

On Ubuntu machine,example workflow to check if commit has a tag or not,

runs-on: ubuntu-latest
steps:
  - uses: actions/checkout@v2
  - run: |
      ls
      echo ${{ github.ref }}
      ref='refs/tags/v'
      if [[ ${{ github.ref }} == *${ref}* ]]; then
        v=$(echo ${{ github.ref }} | cut -d'/' -f3)
        echo "version tag is ${v}"
      else
        echo "There is no github tag reference, skipping"
      fi

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 lbragile
Solution 2
Solution 3 s-lab