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 Targets2run-name: Upload Impacted Targets for ${{ github.ref_name }}3on: pull_request45jobs:6 compute_impacted_targets:7 name: Compute Impacted Targets8 runs-on: ubuntu-latest9 steps:10 - name: Checkout11 uses: actions/checkout@v312 with:13 lfs: true1415 - name: Compute Impacted Targets16 uses: trunk-io/merge-action@v117 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 Targets2on: pull_request34jobs:5 compute_impacted_targets:6 name: Compute Impacted Targets7 runs-on: ubuntu-latest8 steps:9 - name: Checkout10 uses: actions/checkout@v31112 - name: Compute Impacted Targets13 uses: trunk-io/nx-action@v114 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!