Running Nx Affected Commands in GitHub Actions

devinshoemaker profile image Devin Shoemaker Updated on ・2 min read

GitHub Actions is a powerful feature that allows developers to automate their CI/CD workflows directly in GitHub. It's pretty easy to get up and going with the official Node.js workflow template, however, Nx can be a bit tricky to work with.

Nx has a powerful set of commands that allow you to rebuild and retest what is affected by a change, but due to the way that GitHub Actions checks out a branch to execute a workflow, these will not work properly with the standard Node.js workflow.

The first thing to change from a standard Node.js workflow is the parameters passed into the checkout action:

- uses: actions/checkout@v2
    ref: ${{ github.event.pull_request.head.ref }}
    fetch-depth: 0

The ref command explicitly sets the commit to be checked out as the latest commit from the pull request that was initiated.

When fetch-depth is set to 0 then then the entire history of commits is fetched.

Next, we need to fetch origin master so that we have a base to run the affected commands against:

- run: git fetch --no-tags --prune --depth=5 origin master

Finally, you can begin running the affected commands:

# npm

npm run affected:build -- --base=origin/master

# Yarn

yarn affected:build --base=origin/master

Here is a full example borrowed from my nxtend repo:

name: Nx Affected CI

    branches: [master]
    branches: [master]

    runs-on: ubuntu-latest

        node-version: [12.x]

      - uses: actions/checkout@v2
          ref: ${{ github.event.pull_request.head.ref }}
          fetch-depth: 0
      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v1
          node-version: ${{ matrix.node-version }}
      - run: git fetch --no-tags --prune --depth=5 origin master
      - run: yarn install --frozen-lockfile
      - run: yarn affected:build --base=origin/master
      - run: yarn affected:lint --base=origin/master
      - run: yarn affected:test --base=origin/master
      - run: yarn affected:e2e --base=origin/master

In order to use this, create the file nx-affected.yml in the directory .github/workflows in your repository.


GitHub Actions are powerful and easy to add to your CI/CD pipeline. As your code scales with Nx, so should your testing.

Special thanks to Plysepter on GitHub for their suggestions that helped get this working.


GitHub Actions

Starting with the Node.js workflow template

Nx - Rebuilding and Retesting What is Affected

Plysepter on GitHub

Posted on May 29 by:

devinshoemaker profile

Devin Shoemaker


Full-stack developer, Nx Plugin author, streaming newbie


markdown guide