GitHub Actions: Does the IF have an ELSE?
Asked Answered
D

6

83

In GitHub Actions:

I have an if, but I still need to run some other 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 ":("
Discant answered 29/3, 2020 at 15:57 Comment(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?Ionic
C
54

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

Chiller answered 8/4, 2020 at 7:53 Comment(7)
so you cannot have multiple if in a single steps.name?Ionic
@DanChaltiel I don't think soDiscant
You can't use !, that's not valid YAML syntax. Why would you write that without testing it?Million
@Million You can use ! if you write your condition in ${{ }} brackets. Here is an exampleChiller
@DanChaltiel see my comment which can be useful in your case: https://mcmap.net/q/241401/-github-actions-does-the-if-have-an-elseRajiv
@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.Book
YOU CAN NOW HAVE ELSE #60917431Chalaza
R
31

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.

Rajiv answered 8/2, 2021 at 21:44 Comment(0)
T
19

I found that we could also use an expression for that. Here is 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 are in Conditional operator or function for expression syntax #409.

Twum answered 24/3, 2023 at 14:54 Comment(0)
E
18

An alternative solution to GitHub actions-based commands, is to use shell script commands for an if-else statement.

On a Ubuntu machine, an example workflow to check if the commit has a tag or not is:

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
Ellswerth answered 6/6, 2022 at 23:31 Comment(0)
M
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 }}
Mungo answered 27/4, 2021 at 22:53 Comment(1)
A third party dependency that requires updates, might have security vulnerabilities, etc., just for if...Capsulate
M
-1

You can also do something like this if you want to check for some data type value without using the 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 repositories at the same time, but I only want to run my command once.

Read more on expressions on Expressions (in workflows and actions).

Maher answered 18/10, 2022 at 19:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.