Back to all articles

Parallel Mode with Nx

By The Trunk TeamMay 31, 2024
merge

Leveraging parallel mode for Trunk Merge is easy for Nx enabled repos because Nx already knows the structure of your code and can automatically generate a dependency graph. Merge can use this information in parallel mode to run your CI tests more efficiently; eliminating checks when Nx already knows they cannot conflict.

Setup Trunk Merge

Connect your GitHub repository with Trunk and set up Trunk Merge with the standard instructions.

Turn on Parallel Mode

Next, enable Parallel Mode in the Trunk Merge UI.

Set Up the Github Action

Trunk provides an Nx Merge GitHub action (workflow) that leverages Nx to take care of generating the required impacted targets and uploading them to Merge, taking care of the heavy work for using Parallel mode. In order for GitHub to communicate with Trunk Merge, it needs to be able to authenticate from the GitHub Action instance to the Trunk web application using your Trunk Organization API Token.

Store your Organization Token as a GitHub Secret

Add merge action with Trunk Org Token

Now add the trunk-io/merge-action action in a new workflow in your repo under .github/workflows (e.g.: .github/workflows/uploaded_impacted_targets.yaml ).

1name: Upload Impacted Targets
2run-name: Upload Impacted Targets for ${{ github.ref_name }}
3on: pull_request
4
5jobs:
6 compute_impacted_targets:
7 name: Compute Impacted Targets
8 runs-on: ubuntu-latest
9 steps:
10 - name: Checkout
11 uses: actions/checkout@v3
12 with:
13 lfs: true
14
15 - name: Compute Impacted Targets
16 uses: trunk-io/merge-action@v1
17 with:
18 # Use your Trunk repo or org API token to authenticate impacted targets uploads.
19 # This secret should be provided as a GitHub secret.
20 # See https://docs.github.com/en/actions/security-guides/using-secrets-in-github-actions.
21 trunk-token: ${{ secrets.TRUNK_ORG_API_TOKEN }}

Add merge action with Trunk Org Token

Now add the trunk-io/nx-action action in a new workflow in your repo under .github/workflows (eg: .github/workflows/uploaded_impacted_targets.yaml )

1name: Upload Impacted Targets
2on: pull_request
3
4jobs:
5 compute_impacted_targets:
6 name: Compute Impacted Targets
7 runs-on: ubuntu-latest
8 steps:
9 - name: Checkout
10 uses: actions/checkout@v3
11
12 - name: Compute Impacted Targets
13 uses: trunk-io/nx-action@v1
14 with:
15 # Use your Trunk repo or org API token to authenticate impacted targets uploads.
16 # This secret should be provided as a GitHub secret.
17 # See https://docs.github.com/en/actions/security-guides/using-secrets-in-github-actions.
18 trunk-token: ${{ secrets.TRUNK_API_TOKEN }}

If your Nx setup is not in the root of your repo then you can add workspace-path: your_workspace_path just below the trunk-token. Commit the new workflow back to your repo. to make it active.

Now you can submit a new pull request and test it. Success!

Try it yourself or
request a demo

Get started for free

Try it yourself or
Request a Demo

Free for first 5 users