Configuring CI Using Bitbucket Pipelines and Nx
Below is an example of a Bitbucket Pipeline setup for an Nx workspace - building and testing only what is affected.
1image: node:20
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 format:check
12            - npx nx affected -t lint,test,build --base=origin/master --head=HEAD --configuration=ci
13
14  branches:
15    main:
16      - step:
17          name: "Build and test affected apps on 'main' branch changes"
18          caches: # optional
19            - node
20          script:
21            - npm ci
22            - npx nx format:check
23            - npx nx affected -t lint,test,build --base=HEAD~1 --configuration=ci
24The pull-requests and main jobs implement the CI workflow.
Distributed Task Execution
This pipeline uses Distributed Task Execution (DTE) to automatically distribute work across multiple agent processes.
1image: node:20
2
3clone:
4  depth: full
5
6definitions:
7  steps:
8    - step: 
9        name: Agent
10        script:
11          - export NX_BRANCH=$BITBUCKET_PR_ID
12
13          - npm ci
14          - npx nx-cloud start-agent
15
16pipelines:
17  pull-requests:
18    '**':
19      - parallel:
20          - step:
21              name: CI
22              script:
23                - export NX_BRANCH=$BITBUCKET_PR_ID
24
25                - npm ci
26                - npx nx-cloud start-ci-run --stop-agents-after="build" --agent-count=3
27                - npx nx-cloud record -- npx nx format:check
28                - npx nx affected --target=lint,test,build
29                - npx nx-cloud stop-all-agents
30          - step: 
31          - step: 
32          - step: 
33