I'm using NX in my CI with GitHub Actions and I checkout the repository using actions/checkout@v4.
When I try to use nx affected:..., for example nx affected:lint or nx affected:test it fails with the following error:
fatal: ambiguous argument 'main': unknown revision or path not in the working tree. Use '--' to separate paths from revisions, like this: 'git [...] -- [...]'
The problem is that the main branch is not available. I tried to fix this problem using fetch-depth: 0 as suggested by the checkout action's documentation, but it did not solve my problem.
Execute the following command before your
:affectedcommand to ensure your CI environment has an unambiguous main available in your branch.git rev-parse --verify main || git remote set-branches origin main && git fetch --depth 1 origin main && git branch main origin/mainWhat this does is
mainbranch is unambiguous (see git rev-parse)mainto matchorigin/mainThis would, however, fail if you run the command while on
mainalready, so you should only execute it in a branch.Something like this in your
ymlshould do the trick: