73

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 ":("
1
  • 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? Aug 29, 2020 at 19:01

6 Answers 6

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

7
  • 2
    so you cannot have multiple if in a single steps.name? Aug 30, 2020 at 7:51
  • @DanChaltiel I don't think so
    – sab
    Aug 31, 2020 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, 2020 at 14:43
  • 20
    @Peter You can use ! if you write your condition in ${{ }} brackets. Here is an example
    – fabasoad
    Dec 23, 2020 at 14:16
  • @DanChaltiel see my comment which can be useful in your case: stackoverflow.com/a/66109820/4298115
    – lbragile
    Feb 8, 2021 at 21:45
27

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.

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

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.