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 ?
3 Likes
@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:
@uditgaurav ,
You can write like as the following:
- name: Running all tests
if: |
startsWith(github.event.comment.body, '/run-e2e-all') ||
startsWith(github.event.comment.body, '/run-e2e-first-test') ||
startsWith(github.event.comment.body, '/run-e2e-second-test') ||
startsWith(github.event.comment.body, '/run-e2e-third-test') ||
startsWith(github.event.comment.body, '/run-e2e-fourth-test') ||
startsWith(github.event.comment.body, '/run-e2e-fifth-test') ||
startsW…
Note: if you use “${{ }} ” to surround the condition, this will not work.
6 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
smac89
June 23, 2021, 8:13am
#6
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
3 Likes
[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'
) }}
1 Like
This did not work for me. In my case the first conditional was evaluated and did not throw an error but the second never was. In your case, having || would not throw an error either unless you tested worflow_run
manually.
- name: Set AWS Env
if: |
${{ github.ref == 'refs/heads/master'
&& (!contains(github.event.head_commit.message, 'skip-release')
)}}
However, @brightran 's example did work:
- name: Set AWS Env
if: |
github.ref != 'refs/heads/master'
&& !contains(github.event.head_commit.message, 'skip-release')