Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to handle empty matrix #66

Closed
sam-hoffman opened this issue Feb 11, 2021 · 3 comments
Closed

How to handle empty matrix #66

sam-hoffman opened this issue Feb 11, 2021 · 3 comments
Labels
question Further information is requested

Comments

@sam-hoffman
Copy link

I'd like to run actions with the matrix option only when the matrix isn't empty, but I'm currently getting an error saying "Matrix vector does not contain any values".

Ideally, if the matrix vector didn't contain any values, the test job would not run.

My action looks like this:

jobs:
  changes:
    runs-on: ubuntu-latest
    outputs:
      # Expose matched filters as job 'services' output variable
      services: ${{ steps.filter.outputs.changes }}
    steps:
    # For pull requests it's not necessary to checkout the code
    - uses: dorny/paths-filter@v2
      id: filter
      with:
        filters: |
          service1: 
            - service1/**/*.py
            - service1/env.yml
          service2:
            - service2/**/*.py
            - service2/env.yml

  # JOB to test each of modified services
  test:
    needs: changes
    strategy:
      matrix:
        # Parse JSON array containing names of all filters matching any of changed files
        service: ${{ fromJson(needs.changes.outputs.services) }}
    runs-on: ubuntu-latest
    ...

Thanks in advance for your help!

@dorny
Copy link
Owner

dorny commented Feb 12, 2021

You can use if condition like this:

test:
  needs: changes
  if: ${{ needs.changes.outputs.services != '[]' && needs.changes.outputs.services != '' }}
  strategy:
    matrix:
      service: ${{ fromJson(needs.changes.outputs.services) }}

Job if condition is evaluated before job is actually started. I think it's evaluated only once and is not affected by matrix strategy although I'm not 100% sure about that.

Both job and steps outputs are just strings. That's why you have to use fromJson() to parse it into array.
Empty array will be serialized as [] so you can simply check it in the if condition.
Second part of the condition just checks if it's not an empty string - that would mean some failure during changes job execution.

@dorny dorny added the question Further information is requested label Feb 12, 2021
@sam-hoffman
Copy link
Author

This worked great! Thank you so much, and thanks for this great action :)

neocturne added a commit to freifunk-gluon/gluon that referenced this issue Dec 18, 2021
The CI should be successful when there is nothing to check. Add if
condition as proposed in [1].

[1] dorny/paths-filter#66 (comment)
mkg20001 pushed a commit to ffgraz/gluon that referenced this issue Jan 12, 2022
The CI should be successful when there is nothing to check. Add if
condition as proposed in [1].

[1] dorny/paths-filter#66 (comment)
@sshymko
Copy link

sshymko commented Nov 17, 2022

To account for empty array formatting differences ([], [ ], etc.), the condition can be updated like so:

test:
  needs: changes
  if: ${{ needs.changes.outputs.services != '' && toJson(fromJson(needs.changes.outputs.services)) != '[]' }}

smaye81 added a commit to connectrpc/examples-es that referenced this issue Aug 15, 2023
This fixes two issues on CI with `paths-filter`:

1. Perform a checkout prior to the step. For more info, see this issue:
dorny/paths-filter#88
2. Only perform CI step if there are prior changes. For more info, see
dorny/paths-filter#66
cyqsimon added a commit to cyqsimon/el-rust-pkgs-spec that referenced this issue Oct 12, 2023
cyqsimon added a commit to cyqsimon/el-rust-pkgs-spec that referenced this issue Oct 12, 2023
cyqsimon added a commit to cyqsimon/el-rust-pkgs-spec that referenced this issue Oct 12, 2023
Squashed commit of the following:

commit ecfee6a
Author: cyqsimon <28627918+cyqsimon@users.noreply.github.com>
Date:   Thu Oct 12 13:30:10 2023 +0800

    Only run reporting job when build ID output is not empty

    - See dorny/paths-filter#66

commit c286138
Author: cyqsimon <28627918+cyqsimon@users.noreply.github.com>
Date:   Thu Oct 12 13:26:18 2023 +0800

    Better job & step naming

commit fa80712
Author: cyqsimon <28627918+cyqsimon@users.noreply.github.com>
Date:   Thu Oct 12 13:25:17 2023 +0800

    rm mock build ID

commit 445f56f
Author: cyqsimon <28627918+cyqsimon@users.noreply.github.com>
Date:   Thu Oct 12 13:24:18 2023 +0800

    `jq` is installed by default

commit 3e526a0
Author: cyqsimon <28627918+cyqsimon@users.noreply.github.com>
Date:   Thu Oct 12 13:18:31 2023 +0800

    Use a running mock build ID

commit fd1c375
Author: cyqsimon <28627918+cyqsimon@users.noreply.github.com>
Date:   Thu Oct 12 13:14:29 2023 +0800

    Query API until build completion

commit ddd091a
Author: cyqsimon <28627918+cyqsimon@users.noreply.github.com>
Date:   Thu Oct 12 12:53:26 2023 +0800

    Create job matrix using JSON array output

commit 139dc51
Author: cyqsimon <28627918+cyqsimon@users.noreply.github.com>
Date:   Thu Oct 12 12:47:43 2023 +0800

    Generate JSON array correctly

commit 1fd4c8e
Author: cyqsimon <28627918+cyqsimon@users.noreply.github.com>
Date:   Thu Oct 12 12:42:21 2023 +0800

    Correctly output whole array

commit 627b1e5
Author: cyqsimon <28627918+cyqsimon@users.noreply.github.com>
Date:   Thu Oct 12 12:39:56 2023 +0800

    Add mock build IDs

commit 4e0f5d6
Author: cyqsimon <28627918+cyqsimon@users.noreply.github.com>
Date:   Thu Oct 12 12:36:20 2023 +0800

    add build ID output
hertzsprung added a commit to hertzsprung/binsley that referenced this issue May 27, 2024
egpbos added a commit to NLeSC/guide that referenced this issue Oct 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants