Job with dynamic matrix crashes if matrix contains zero elements. How to resolve?

Hi all. I have a GitHub action with two jobs. The first job returns an array, which I’ve been able to pass to a second job which then uses that array as its matrix. But the first job can sometimes return an array with zero length, and when it does, the second job crashes with the error:

Error when evaluating 'strategy' for job 'Job-2'. (Line: 52, Col: 11): Matrix vector 'cfg' does not contain any values

How can I resolve this? Ideally, if job-1 returns an array with zero elements, the second job would not run at all.

I’ve seen this continue-on-error flag, but I’m not sure exactly how to use it. In my case, the crash doesn’t seem to occur on any specific step–it occurs when the job is trying to evaluate the matrix.

My Code (myfile.yml)

name: GitHub Actions Demo
on: [push]
      matrix: ${{ steps.hello.outputs.matrix }}
    runs-on: ubuntu-latest
      - run: echo "🎉 The job was automatically triggered by a ${{ github.event_name }} event."
      - name: Checkout
        uses: actions/checkout@v2.3.4
          fetch-depth: 0
      - name: Test JS action
        id: hello
        uses: myaccount/myrepo@master # This executes a JavaScript file which sets an array as its output.
    needs: Job-1
    runs-on: ubuntu-latest
          - ${{fromJson(needs.Job-1.outputs.matrix)}}
    #line 52 of the file.
      - run: echo "🎉 SHA ${{ matrix.cfg.offSha }}."
1 Like

Try putting some sort of condition if statement on Job 2, where you do something like:

if: ${{ needs.Job-1.outputs.matrix != “” }}

something like that.

1 Like

OK perfect, thanks–that works! I had found a way to set a condition on a particular step in a job, but didn’t know how to put a conditional on an entire job, which is exactly what I needed.

Can you share what the if statement looks like?

It’s essentially what you gave me :slight_smile:

if: ${{ needs.Job-1.outputs.matrix != '' }}

At the same time though, I had to modify the previous job to return an empty string if it would otherwise return an empty array. Previously it would return this string: []