# Cypress Documentation
# Source: https://raw.githubusercontent.com/cypress-io/cypress-documentation/main/docs/app/continuous-integration/bitbucket-pipelines.mdx
# Path: docs/app/continuous-integration/bitbucket-pipelines.mdx

---
title: 'Run Cypress tests in Bitbucket Pipelines: Step-by-Step Guide'
description: Run Cypress tests in Bitbucket Pipelines with Docker images, caching, parallelization, and Cypress Cloud.
sidebar_label: Bitbucket Pipelines
---

<ProductHeading product="app" />

# Run Cypress in Bitbucket Pipelines

:::info

##### <Icon name="question-circle" color="#4BBFD2" /> What you'll learn

- How to set up Bitbucket Pipelines to run Cypress tests
- How to cache dependencies and build artifacts
- How to parallelize Cypress tests with Bitbucket Pipelines
- How to use Cypress Cloud with Bitbucket Pipelines

:::

## Basic Setup

Detailed documentation is available in the
[Bitbucket Pipelines Documentation](https://support.atlassian.com/bitbucket-cloud/docs/get-started-with-bitbucket-pipelines/).

Bitbucket runs most builds in Docker containers as described in [Docker image options](https://support.atlassian.com/bitbucket-cloud/docs/docker-image-options/).

If you use the currently available default Bitbucket / Atlassian Linux images listed in the
"Default build environment" of the Bitbucket Cloud documentation
[Use Docker images as build environments](https://support.atlassian.com/bitbucket-cloud/docs/use-docker-images-as-build-environments/) you must additionally install [Cypress Linux prerequisites](/app/get-started/install-cypress#Linux-Prerequisites).

For a simpler setup, use a Cypress Docker image, as described in the following section.

## Testing with Cypress Docker Images

The Cypress team maintains the official
[Docker Images](https://github.com/cypress-io/cypress-docker-images) for running
Cypress locally and in CI, with some images including Chrome, Firefox and Edge. For
example, this allows us to run the tests in Firefox by passing the
`--browser firefox` attribute to `cypress run`.

Read about [Cypress Docker variants](/app/continuous-integration/overview#Cypress-Docker-variants) to decide which image is best for your project.

```yaml title="bitbucket-pipelines.yml"
image: cypress/browsers:22.15.0

pipelines:
  default:
    - step:
        script:
          # install dependencies
          - npm ci
          # start the server in the background
          - npm run start &
          # run Cypress tests in Firefox
          - npx cypress run --browser firefox
```

**How this `bitbucket-pipelines.yml` works:**

- On _push_ to this repository, this job will provision and start Bitbucket
  Pipelines using the Cypress Docker image. It will run the pipelines defined in the
  `pipelines` section of the configuration.
- The code is checked out from the Bitbucket repository.
- Finally, our scripts will:
  - Install npm dependencies
  - Start the project web server (`npm start`)
  - Run the Cypress tests within the Bitbucket repository using Firefox

## Caching Dependencies and Build Artifacts

Per the
[Caches documentation](https://support.atlassian.com/bitbucket-cloud/docs/cache-dependencies/),
Bitbucket offers options for caching dependencies and
build artifacts across many different workflows.

To cache `node_modules`, the npm cache across builds, the `cache` attribute and
configuration has been added below.

Artifacts from a job can be defined by providing paths to the `artifacts`
attribute.

```yaml title="bitbucket-pipelines.yml"
image: cypress/browsers:22.15.0

pipelines:
  default:
    - step:
        caches:
          - node
        script:
          # install dependencies
          - npm ci
          # start the server in the background
          - npm run start &
          # run Cypress tests in Firefox
          - npx cypress run --browser firefox
        artifacts:
          # store any generates images and videos as artifacts
          - cypress/screenshots/**
          - cypress/videos/**
```

Using the
[definitions](https://support.atlassian.com/bitbucket-cloud/docs/configure-bitbucket-pipelinesyml/#Global-configuration-options)
block we can define additional caches for npm and Cypress.

```yaml title="bitbucket-pipelines.yml"
definitions:
  caches:
    npm: $HOME/.npm
    cypress: $HOME/.cache/Cypress
```

## Parallelization

[Cypress Cloud](/cloud/get-started/introduction) offers the ability to
[parallelize and group test runs](/cloud/features/smart-orchestration/parallelization)
along with additional insights and [analytics](/cloud/features/analytics/overview) for
Cypress tests.

Before diving into an example of a parallelization setup, it is important to
understand the two different types of jobs that we will declare:

- **Install Job**: A job that installs and caches dependencies that will be used
  by subsequent jobs later in the Bitbucket Pipelines workflow.
- **Worker Job**: A job that handles execution of Cypress tests and depends on
  the _install job_.

### Install Job

The separation of installation from test running is necessary when running
parallel jobs. It allows for reuse of various build steps aided by caching.

First, we break the pipeline up into reusable chunks of configuration using a
[YAML anchor](https://support.atlassian.com/bitbucket-cloud/docs/yaml-anchors/),
`&e2e`. This will be used by the worker jobs.

:::info

The following configuration using the `--parallel` and `--record` flags to
[cypress run](/app/references/command-line#cypress-run) requires setting up
recording test results to [Cypress Cloud](/cloud/get-started/introduction).

:::

```yaml title="bitbucket-pipelines.yml"
image: cypress/base:22.15.0

## job definition for running E2E tests in parallel
e2e: &e2e
  name: E2E tests
  caches:
    - node
    - cypress
  script:
    - npm run start &
    - npm run e2e:record -- --parallel --group UI-Chrome --ci-build-id $BITBUCKET_BUILD_NUMBER
  artifacts:
    # store any generates images and videos as artifacts
    - cypress/screenshots/**
    - cypress/videos/**
```

### Worker Jobs

Next, the worker jobs under `pipelines` that will run Cypress tests with Chrome
in parallel.

We can use the `e2e`
[YAML anchor](https://support.atlassian.com/bitbucket-cloud/docs/yaml-anchors/)
in our definition of the pipeline to execute parallel jobs using the `parallel`
attribute. This will allow us to run multiples instances of Cypress at same
time.

```yaml title="bitbucket-pipelines.yml"
## job definition for running E2E tests in parallel
## ...

pipelines:
  default:
    - step:
        name: Install dependencies
        caches:
          - npm
          - cypress
          - node
        script:
          - npm ci
    - parallel:
      # run N steps in parallel
      - step:
          <<: *e2e
      - step:
          <<: *e2e
      - step:
          <<: *e2e
definitions:
  caches:
    npm: $HOME/.npm
    cypress: $HOME/.cache/Cypress
```

The complete `bitbucket-pipelines.yml` is below:

```yaml title="bitbucket-pipelines.yml"
image: cypress/base:22.15.0

## job definition for running E2E tests in parallel
e2e: &e2e
  name: E2E tests
  caches:
    - node
    - cypress
  script:
    - npm run start &
    - npm run e2e:record -- --parallel --group UI-Chrome --ci-build-id $BITBUCKET_BUILD_NUMBER
  artifacts:
    # store any generates images and videos as artifacts
    - cypress/screenshots/**
    - cypress/videos/**

pipelines:
  default:
    - step:
        name: Install dependencies
        caches:
          - npm
          - cypress
          - node
        script:
          - npm ci
    - parallel:
        # run N steps in parallel
        - step:
            <<: *e2e
        - step:
            <<: *e2e
        - step:
            <<: *e2e
definitions:
  caches:
    npm: $HOME/.npm
    cypress: $HOME/.cache/Cypress
```

## Using Cypress Cloud with Bitbucket Pipelines

In the Bitbucket Pipelines configuration we have defined in the previous
section, we are leveraging three useful features of
[Cypress Cloud](/cloud/get-started/introduction):

<CiProviderCloudSteps />