Empty matrix fails workflow

We have an NX managed monorepo with all our code. When we merge a PR, we trigger a workflow to detect and build the apps that have changed. This is done through a dynamic matrix that is built as the first step of the job.

However, sometimes when we change something that isn’t directly related to an app in the repo, the matrix ends up being empty. When the matrix is empty Actions considers the workflow to have failed.

Is there any way to override this behavior?


In the job that sets the matrix dynamically, you can try to set an output to declare whether the matrix is empty.

  • If the dynamic matrix is empty, skip executing the matrix in the subsequent process.
  • If the dynamic matrix is not empty, execute the matrix in the subsequent process.

A simple demo:

    . . .
      matrix: ${{ steps.set-matrix.outputs.matrix }}
      empty_matrix: ${{ steps.check-matrix.outputs.empty_matrix }}
      - name:  Set matrix
        id: set-matrix
        run: echo "::set-output name=matrix::{\"include\":[. . .]}"

      - name: Check matrix
        id: check-matrix
        --- if the matrix is empty, set 'empty_matrix' to be 'true' ---
        echo "::set-output name=empty_matrix::true"

        --- if the matrix is not empty,  set 'empty_matrix' to be 'false' ---
        echo "::set-output name=empty_matrix::false"

    needs: job1
    if: ${{ needs.job1.outputs.empty_matrix == 'false' }}
    . . .
      matrix: ${{fromJson(needs.job1.outputs.matrix)}}
      . . .