How to write multi-line condition in if

I used '>' to describe a multi-line condition in if.

I thought this would work as expected, but the combination of '>' and '${{ }}' seems to work strangely.

Below is the workflow created for confirmation.

name: condition-tests

on:
  push:
    branches: [ master ]

jobs:
  test:
    runs-on: ubuntu-latest

    steps:
    - name: test1
      run: echo hoge
      if: ( 'a' == 'a' )

    - name: test2
      run: echo hoge
      if: ( 'a' == 'b' )

    - name: test3
      run: echo hoge
      if: >
        'a' == 'a'

    - name: test4
      run: echo hoge
      if: >
        'a' == 'b'

    - name: test5
      run: echo hoge
      if: >
        ${{ 'a' == 'a' }}

    - name: test6
      run: echo hoge
      if: >
        ${{ 'a' == 'b' }}

And the execution result is this.

I thought that the last test6 was skipped, but it is not actually skipped.

I think that there is something wrong with the Parser in GitHub Actions when I combine '>' and '${{ }}'. Is my workflow something syntactically wrong ?

1 Like

@kei-yamazaki,

you need to use the symbol ‘|’ to set the multi-line condition.
For example:

jobs:
  job1:
    if: |
      github.event_name == 'push' ||
      (github.event_name == 'pull_request' && github.head_ref == 'master')

Here is a same topic as reference:

Note: if you use “${{ }}” to surround the condition, this will not work.

3 Likes

@brightran,
Thank you for your reply.

Is it a specification that doesn’t work when using “|” and “${{ }}” together?
If so, I think it should be documented.
https://docs.github.com/en/actions/reference/context-and-expression-syntax-for-github-actions#about-contexts-and-expressions

2 Likes

@kei-yamazaki ,

Yes, I agree.
The documentation even does not mention using the ‘|’ symbol to declare a multi-line if conditional.

3 Likes

Just an FYI for anyone wondering:

Multiline condition works for assignment. So something like

jobs:
  job1:
    outputs:
      my-output: ${{ steps.get-latest-release.outcome == 'success' &&
                     steps.get-latest-dev.outcome == 'success' &&
                     (steps.get-latest-release.outputs.result != steps.get-latest-dev.outputs.result) }}

HTH

1 Like

[FYI to people that just found this discussion]

Note: if you use “ ${{ }} ” to surround the condition, this will not work.

In the meantime it seems to work to combine | and ${{ ... }} in multiline if-conditions (on GHE 3.0.10):

Example:

jobs:
  job_test:
    if: |
        ${{ github.event_name == 'push'
            || ( github.event_name == 'workflow_run'
                 && github.event.workflow_run.conclusion == 'success'
               ) }}