Configuring CI Using Bitbucket Pipelines and Nx

Below is an example of a Bitbucket Pipeline setup for an Nx workspace only building and testing what is affected.

1image: node:16
2pipelines:
3  pull-requests:
4    '**':
5      - step:
6          name: "Build and test affected apps on Pull Requests"
7          caches: # optional
8          - node
9          script:
10            - npm ci
11            - npx nx workspace-lint
12            - npx nx format:check
13            - npx nx affected --target=lint --base=origin/master --parallel --max-parallel=3
14            - npx nx affected --target=test --base=origin/master --parallel --max-parallel=3 --ci --code-coverage
15            - npx nx affected --target=build --base=origin/master --head=HEAD --parallel  --max-parallel=3
16
17  branches:
18    main:
19      - step:
20        name: "Build and test affected apps on 'main' branch changes"
21          caches: # optional
22          - node
23          script:
24            - npm ci
25            - npx nx workspace-lint
26            - npx nx format:check
27            - npx nx affected --target=lint --base=origin/master --parallel --max-parallel=3
28            - npx nx affected --target=test --base=HEAD~1 --parallel --max-parallel=3 --ci --code-coverage
29            - npx nx affected --target=build --base=HEAD~1 --parallel  --max-parallel=3
30

The pull-requests and main jobs implement the CI workflow.