70

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 ":("
JonasVautherin
  • 7,297
  • 6
  • 49
  • 95
sab
  • 4,352
  • 7
  • 36
  • 60
  • 1
    I have a somehow similar issue and I'm really curious about how you integrated this in your jobs, could you add a link to your GitHub config file or add a bit more context? – Dan Chaltiel Aug 29 '20 at 19:01

6 Answers6

47

GitHub Actions doesn't have else statement to run a different command/action/code. But you're right, all what you need to do is to create another step with reversed if condition. BTW, you can just use ! instead of false ==, if you surround your statement with ${{ }}.

Here are some links: if statement, operators

Philip
  • 2,888
  • 2
  • 24
  • 36
fabasoad
  • 1,613
  • 18
  • 20
  • 2
    so you cannot have multiple `if` in a single `steps.name`? – Dan Chaltiel Aug 30 '20 at 07:51
  • @DanChaltiel I don't think so – sab Aug 31 '20 at 13:23
  • 1
    You can't use `!`, that's not valid YAML syntax. Why would you write that without testing it? – Peter Dec 21 '20 at 14:43
  • 20
    @Peter You can use `!` if you write your condition in `${{ }}` brackets. [Here](https://github.com/fabasoad/twilio-voice-call-action/actions/runs/440407309/workflow#L24) is an example – fabasoad Dec 23 '20 at 14:16
  • @DanChaltiel see my comment which can be useful in your case: https://stackoverflow.com/a/66109820/4298115 – lbragile Feb 08 '21 at 21:45
  • @Peter, you can use ! just not at the start of the condition. `if: success() && !contains(['SNAPSHOT'], env.BUILD_VERSION)` will work, but `if: !contains(['SNAPSHOT'], env.BUILD_VERSION)` will not. – miigotu Jul 14 '21 at 09:21
  • YOU CAN NOW HAVE ELSE https://stackoverflow.com/questions/60916931/github-action-does-the-if-have-an-else – Cale Dec 13 '21 at 14:11
26

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.

lbragile
  • 7,549
  • 3
  • 27
  • 64
17

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 }}
agabrys
  • 8,728
  • 3
  • 35
  • 73
15

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
shyam
  • 537
  • 6
  • 16
6

I found that we could also use expression for that here how I did it:

jobs:
  build:
    runs-on: ubuntu-latest
    env:
      PkgDesc: ${{ github.event.inputs.packageDescription != '' && github.event.inputs.packageDescription ||  format('{0} - {1}', 'Commit', github.SHA)  }}

Syntax: ${{ x && 'ifTrue' || 'ifFalse' }}

More details https://github.com/actions/runner/issues/409#issuecomment-752775072

Hamdi
  • 61
  • 1
  • 3
0

You can also do something like this if you want to check for some data type value without using built in functions

- name: Notify Team on Slack
   if: ${{github.repository == 'calebcadainoo/cascade-img'}}
   run: |
     # some command

Over here, I'm triggering event on each push.

I'm pushing to two repos at the same time but I only want to run my command once.

Read More on Expressions Here:

https://docs.github.com/en/actions/learn-github-actions/expressions