@e-square/nx-affected-matrix

June 18, 2023 · View on GitHub

LATEST GitHub Workflow Status Codecov All Contributors MIT

Summary

A GitHub Action that outputs a matrix of NX targets (with some other useful outputs.
This action enables CI level parallelism by splitting the affected projects into multiple parallel jobs.

It is recommended to use this action's outputs with @e-square/nx-distributed-task, check out the monorepo's README for a full usage example of both actions.

Usage

Inputs

namedescriptiondefaultrequired
targetsComma-delimited targets to run-
maxDistributionMaximum distribution of jobs per target. Can be number that will be used for all targets, or an array/object that matches the targets input3
workingDirectoryPath to the Nx workspace, needed if not the repository root-
main-branchnx-set-shas action input. The 'main' branch of your repository (the base branch which you target with PRs)'main'
workflow-idThe ID of the github action workflow to check for successful run or the name of the file name containing the workflow-
checkoutShould the action do git checkouttrue
argsSpace-delimited args to add to nx affected command-

Outputs

namedescription
matrixThe affected matrix to be consumed
appsA comma-delimited list of the affected apps
libsA comma-delimited list of the affected libs (including non buildable libs)
hasChangesboolean that will be true when there is at least one job in the affected matrix
jobs:
  setup:
    runs-on: ubuntu-latest
    name: Affected Matrix
    outputs:
      hasChanges: ${{ steps.affected.outputs.hasChanges }}
      matrix: ${{ steps.affected.outputs.matrix }}
    steps:
      - name: Calculate affected projects
        uses: e-square-io/nx-affected-matrix@v2
        id: affected
        with:
          targets: 'test,build'
          maxDistribution: 3

For each supplied target, the action will slice the list of affected projects into multiple groups.
So the output matrix from the example above will be:

{
  "include": [
    {
      "target": "test",
      "projects": "project1,project2"
    },
    {
      "target": "test",
      "projects": "project3,project4"
    },
    ...
  ]
}

Control distribution per target

In some cases it might be useful to change the distribution only for a particular target.
maxDistribution input can receive an object with keys matching to the supplied targets or an array that will match by index. e.g:

- name: Calculate affected projects
  uses: e-square-io/nx-affected-matrix@v2
  id: affected
  with:
    targets: 'test,build'
    maxDistribution: '[2,1]' # or '{"test": 2, "build": 1}'

will produce 2 jobs for test target and one for build target.
Note: The action allows to set a partial distribution config, any target that isn't specified in the config will receive the default distribution value (3)

FAQ

Q: Can I use this action without any prior actions? (checkout, npm ci, etc)
A: Yes! The action is already bundled with everything needed to run NX's affected command.
Also, the action will do the checkout for you with clean: false & fetch-depth: 0 (which is required in order to calculate the changes).

Known Issues

  1. Cannot read properties of undefined (reading 'endsWith')
    Workaround: make sure all of your projects has root defined in their project.json config file