---
title: Automate Chromatic with Jenkins
description: Learn how to configure Chromatic with Jenkins
sidebar: { order: 7, label: Jenkins }
---

import IntegrationSnippets from "../../components/IntegrationSnippets.astro";

# Automate Chromatic with Jenkins

Chromatic’s automation can be included as part of your Jenkins pipeline with relative ease.

## Setup

To integrate Chromatic with your existing [multistage pipeline](https://www.jenkins.io/doc/tutorials/build-a-multibranch-pipeline-project/), you'll need to add the following:

{/* prettier-ignore-start */}

<IntegrationSnippets>
  <Fragment slot="storybook">
    ```groovy title="JenkinsFile"
    pipeline {
      /* Other pipeline configuration. */

      stages {
        /* Other pipeline stages */

        /* 👇 Adds Chromatic as a stage */
        stage('Run Chromatic') {
          environment {
            CHROMATIC_PROJECT_TOKEN = credentials('chromatic-project-token')
          }
          steps {
            /* 👇 Runs the Chromatic CLI */
            sh "npx chromatic"
          }
        }
      }
    }
    ```
  </Fragment>
  <Fragment slot="playwright">
     ```groovy title="JenkinsFile"
     pipeline {
       /* Other pipeline configuration. */

       stages {
         /* Other pipeline stages */

         stage('Playwright') {
           agent {
             docker {
               image 'mcr.microsoft.com/playwright:v1.56.0-noble'
               reuseNode true
             }
           }
           steps {
             sh 'npm ci'
             sh 'npx playwright test'
           }
           post {
             always {
              /*
               Chromatic automatically defaults to the test-results directory.
               Replace with the path to your custom directory and adjust the CHROMATIC_ARCHIVE_LOCATION environment variable accordingly.
              */
               archiveArtifacts 'test-results/**'
             }
           }
         }
         stage('Run Chromatic'){
           environment {
             CHROMATIC_PROJECT_TOKEN = credentials('chromatic-project-token')
             CHROMATIC_ARCHIVE_LOCATION = 'test-results'
           }
           steps {
             /* 👇 Runs the Chromatic CLI */
             sh "npx chromatic --playwright"
           }
         }
       }
     }
    ```
  </Fragment>
  <Fragment slot="cypress">
     ```groovy title="JenkinsFile"
     pipeline {
       /* Other pipeline configuration. */

       stages {
         /* Other pipeline stages */

         stage('Cypress') {
           agent {
             docker {
               image 'cypress/browsers:node-22.20.0-chrome-141.0.7390.54-1-ff-143.0.4-edge-141.0.3537.57-1'
               reuseNode true
             }
           }
           environment {
             ELECTRON_EXTRA_LAUNCH_ARGS = '--remote-debugging-port=9222'
           }
           steps {
             sh 'npm ci'
             sh 'npm run dev &'
             sh 'npx cypress run'
           }
           post {
             always {
              /*
               Chromatic automatically defaults to the cypress/downloads directory.
               Replace with the path to your custom directory and adjust the CHROMATIC_ARCHIVE_LOCATION environment variable accordingly.
              */
               archiveArtifacts 'cypress/downloads/**'
             }
           }
         }
         stage('Run Chromatic'){
           environment {
             CHROMATIC_PROJECT_TOKEN = credentials('chromatic-project-token')
             CHROMATIC_ARCHIVE_LOCATION = 'cypress/downloads'
           }
           steps {
             /* 👇 Runs the Chromatic CLI */
             sh "npx chromatic --cypress"
           }
         }
       }
     }
    ```
  </Fragment>
</IntegrationSnippets>

{/* prettier-ignore-end */}

<div class="aside">

We recommend saving the project token as a secret text environment variable named `CHROMATIC_PROJECT_TOKEN` for security reasons. When the Chromatic CLI is executed, it will read the environment variable automatically without any additional flags. Refer to the official official Jenkins [environment variables documentation](https://www.jenkins.io/doc/book/pipeline/jenkinsfile/#handling-credentials) to learn more about it.

</div>

### Run Chromatic on specific branches

If you need to customize your workflow to run Chromatic on specific branches, adjust your `Jenkinsfile` like so:

```groovy title="JenkinsFile"
pipeline {
  /* Other pipeline configuration. */

  stages {
    /* Other pipeline stages */

    /* 👇 Adds Chromatic as a stage */
    stage('Run Chromatic') {
      when {
        branch 'example' /* 👈 Filters the execution to run only on the main branch */
      }
      environment {
        CHROMATIC_PROJECT_TOKEN = credentials('chromatic-project-token')
      }
      steps {
        /* 👇 Runs the Chromatic CLI */
        sh "npx chromatic"
      }
    }
  }
}
```

<div class="aside">

Read the official Jenkins [conditional pipeline documentation](https://www.jenkins.io/doc/book/pipeline/syntax/).

</div>

Now your pipeline will only run Chromatic in the `example` branch.

### Run Chromatic on large projects

Chromatic is prepared to handle large file uploads (with a limit of 5000 files, including stories and assets). If your project exceeds this limit, we recommend adjusting your workflow and run the `chromatic` command with the `--zip` flag to compress your build before uploading it. For example:

```groovy title="JenkinsFile"
pipeline {
  /* Other pipeline configuration. */

  stages {
    /* Other pipeline stages */

    /* 👇 Adds Chromatic as a stage */
    stage('Run Chromatic') {
      environment {
        CHROMATIC_PROJECT_TOKEN = credentials('chromatic-project-token')
      }
      steps {
        /* 👇 Runs Chromatic with the flag to compress the build output. */
        sh "npx chromatic --zip"
      }
    }
  }
}
```

### Run Chromatic on monorepos

Chromatic can be run on monorepos that have multiple subprojects. Each subproject will need it's own project token stored as an environment variable.

#### Prerequisites

1. Ensure that you're in the correct working directory for the subproject.
2. Have `build-storybook` npm script in the subproject's `package.json` file OR explicitly name the script using the using the `--build-script-name` CLI flag and make sure the script is listed in the subproject's `package.json` file.

If you've already built your Storybook in a separate CI step, you can adjust your workflow to include the `--storybook-build-dir` CLI flag to point to the build output directory.

```groovy title="JenkinsFile"
pipeline {
  /* Other pipeline configuration. */

  stages {
    /* Other pipeline stages */

    /* 👇 Adds Chromatic as a stage */
    stage('Run Chromatic') {
      environment {
        CHROMATIC_PROJECT_TOKEN_1 = credentials('chromatic-project-token-1')
        CHROMATIC_PROJECT_TOKEN_2 = credentials('chromatic-project-token-2')
      }
      /* 👇 Runs Chromatic sequentially for each monorepo subproject. */
      steps {
        dir('packages/project_1/'){
          sh "npx chromatic --project-token=${CHROMATIC_PROJECT_TOKEN_1}"
        }
        dir('packages/project_2/'){
          sh "npx chromatic --project-token=${CHROMATIC_PROJECT_TOKEN_2}"}"
      }
    }
  }
}
```

If you want to run Chromatic in parallel for each subproject, you can use this snippet below.

```groovy title="JenkinsFile"
pipeline {
  /* Other pipeline configuration. */
  stages {
     /* Other pipeline stages */

    /* 👇 Adds Chromatic as a stage */
    stage('Run Chromatic') {

      /* 👇 Runs Chromatic in parallel for each monorepo subproject. */
      parallel {
        stage('Publish Project 1 to Chromatic') {
          environment {
            CHROMATIC_PROJECT_TOKEN_1 = credentials('chromatic-project-token-1')
          }
          steps {
            dir('packages/project_1/'){
              sh "npx chromatic --project-token=${CHROMATIC_PROJECT_TOKEN_1}"
            }
          }
        }
        stage('Publish Project 2 to Chromatic') {
          environment {
            CHROMATIC_PROJECT_TOKEN_2 = credentials('chromatic-project-token-2')
          }
          steps {
            dir('packages/project_2/'){
              sh "npx chromatic --project-token=${CHROMATIC_PROJECT_TOKEN_2}"
            }
          }
        }
      }
    }
  }
}
```

### Enable TurboSnap

TurboSnap is an advanced Chromatic feature implemented to improve the build time for large projects, disabled by default once you add Chromatic to your CI environment. To enable it, you'll need to adjust your existing workflow and run the `chromatic` command with the `--only-changed` flag as follows:

```groovy title="JenkinsFile"
pipeline {
  /* Other pipeline configuration. */

  stages {
    /* Other pipeline stages */

    /* 👇 Adds Chromatic as a stage */
    stage('Run Chromatic') {
      environment {
        CHROMATIC_PROJECT_TOKEN = credentials('chromatic-project-token')
      }
      steps {
        /* 👇 Enables Chromatic's TurboSnap feature. */
        sh "npx chromatic --only-changed"
      }
    }
  }
}
```

<div class="aside">

TurboSnap is highly customizable and can be configured to fit your requirements. For more information, read our [documentation](/docs/turbosnap).

</div>

### Overriding Chromatic's branch detection

If your Jenkins pipeline includes a set of rules for branches (e.g., renames the branch, creates ephemeral, or temporary branches) it can lead to unforeseen build errors.

In this case, you can adjust your workflow and include the `--branch-name` flag. This flag overrides Chromatic's default branch detection in favor of the specified branch:

```groovy title="JenkinsFile"
pipeline {
  /* Other pipeline configuration. */

  stages {
    /* Other pipeline stages */

    /* 👇 Adds Chromatic as a stage */
    stage('Run Chromatic') {
      environment {
        CHROMATIC_PROJECT_TOKEN = credentials('chromatic-project-token')
        YOUR_BRANCH='your-branch'
      }
      steps {
        /* 👇 Runs the Chromatic CLI --branch-name flag to override the baseline branch */
        sh "npx chromatic --branch-name=${YOUR_BRANCH}"
      }
    }
  }
}
```

Chromatic will now detect the correct branch and run your workflow. You can also apply this when fixing cross-fork UI comparisons.

### Recommended configuration for build events

Jenkins like CI systems offer the option of running builds for various types of events. For instance for commits pushed to a branch in a pull request. Or for "merge" commits between that branch and the base branch (main).

These specific types of commits (merge) don't persist in the history of your repository. That can cause Chromatic's baselines to be lost in certain situations.

If you're using the Jenkins [GitHub Pull Request plugin](https://github.com/jenkinsci/ghprb-plugin/blob/master/README.md), our recommendation for your pipeline is the following:

- Choose the `ghprbPullId` specifier for the `refspec`
- Ensure the Branch Specifier is set to `${ghprbActualCommit}`

If you're using other Jenkins plugins in your pipeline, refer to this [documentation](https://www.jenkins.io/doc/book/pipeline/multibranch/) for more information.

### UI Test and UI Review

[UI Tests](/docs#test-how-uis-look--function) and [UI Review](/docs/review) rely on [branch and baseline](/docs/branching-and-baselines) detection to keep track of [snapshots](/docs/snapshots). We recommend the following configuration.

#### Command exit code for "required" checks

If you are using pull request statuses as required checks before merging, you may not want your pipeline to fail if test snapshots render without errors (but with changes). To achieve this, pass the flag `--exit-zero-on-changes` to the `chromatic` command, and your pipeline will continue in such cases. For example:

```groovy title="JenkinsFile"
pipeline {
  /* Other pipeline configuration. */

  stages {
    /* Other pipeline stages */

    /* 👇 Adds Chromatic as a stage in the pipeline */
    stage('Run Chromatic') {
      environment {
        CHROMATIC_PROJECT_TOKEN = credentials('chromatic-project-token')
      }
      steps {
        /* 👇 Runs Chromatic with the flag to prevent stage failure */
        sh "npx chromatic --exit-zero-on-changes"
      }
    }
  }
}
```

<div class="aside">

Read our [configuration reference documentation](/docs/configure#options).

</div>

When using `--exit-zero-on-changes` your pipeline will still stop and fail if your Storybook contains stories that error. If you'd prefer Chromatic _never_ to block your pipeline, you can use `npx chromatic || true`.

#### Re-run failed builds after verifying UI test results

Builds that contain visual changes need to be [verified](/docs/quickstart#4-review-changes). They will fail if you are not using `--exit-zero-on-changes`. Once you accept all the changes, re-run the build and the `Run Chromatic` pipeline stage will pass.

If you deny any change, you will need to make the necessary code changes to fix the test (and thus start a new build) to get Chromatic to pass again.

#### Maintain a clean "main" branch

A clean `main` branch is a development **best practice** and **highly recommended** for Chromatic. This means testing your `main` branch to ensure builds are passing. It's important to note that baselines will not persist through branching and merging unless you test your `main` branch.

If the builds are a result of direct commits to `main`, you will need to accept changes to keep the main branch clean. If they're merged from `feature-branches`, you will need to make sure those branches are passing _before_ you merge into `main`.

#### Squash/rebase merge and the "main" branch

We use GitHub, GitLab, and Bitbucket APIs respectively to detect squashing and rebasing so your baselines match your expectations no matter your Git workflow (see [Branching and Baselines](/docs/branching-and-baselines#how-do-baselines-get-preserved-during-squash-and-rebase-merging) for more details).

If you’re using this functionality but notice the incoming changes were not accepted as baselines in Chromatic, then you'll need to adjust the pipeline and include a new Chromatic stage using the `--auto-accept-changes` flag. For example:

```groovy title="JenkinsFile"
pipeline {
  /* Other pipeline configuration. */

  stages {
    /* Other pipeline stages */

    /* 👇 Checks if the current branch is not main and runs Chromatic */
    stage('Run Chromatic') {
      when {
        not {
          branch 'main'
        }
      }
      environment {
        CHROMATIC_PROJECT_TOKEN = credentials('chromatic-project-token')
      }
      steps {
        sh "npx chromatic"
      }
    }
    /* 👇 Checks if the current branch is main and runs Chromatic with the flag to accept all changes */
    stage('Run Chromatic and auto accept changes') {
      when {
        branch 'main'
      }
      environment {
        CHROMATIC_PROJECT_TOKEN = credentials('chromatic-project-token')
      }
      steps {
        sh "npx chromatic --auto-accept-changes"
      }
    }
  }
}
```

<div class="aside">

Read our [configuration reference documentation](/docs/configure#options).

</div>

Including the `--auto-accept-changes` flag ensures all incoming changes will be accepted as baselines. Additionally, you'll maintain a clean `main` branch.

If you want to test the changes introduced by the rebased branch, you can adjust your workflow and include a new step with the `ignore-last-build-on-branch` flag. For example:

```groovy title="JenkinsFile"
pipeline {
  /* Other pipeline configuration. */

  stages {
    /* Other pipeline stages */

    /* 👇 Adds Chromatic as a stage in the pipeline */
    stage('Run Chromatic') {
      environment {
        CHROMATIC_PROJECT_TOKEN = credentials('chromatic-project-token')
      }
      steps {
        /* 👇 Option to skip the last build on target branch */
        sh "npx chromatic --ignore-last-build-on-branch=my-branch"
      }
    }
  }
}
```

<div class="aside">

Read our [configuration reference documentation](/docs/configure#options).

</div>

Including the `--ignore-last-build-on-branch` flag ensures the latest build for the specific branch is not used as a baseline.

#### Run Chromatic on external forks of open source projects

You can enable PR checks for external forks by sharing your project token where you configured the Chromatic command (often in `package.json` or in the pipeline step).

Sharing project tokens allows contributors and others to run Chromatic builds on your project, consuming your snapshot quota. They cannot access your account, settings, or accept baselines. This can be an acceptable tradeoff for open source projects that value community contributions.

#### Skipping builds for certain branches

Sometimes you might want to skip running a build for a certain branch, but still have Chromatic mark the latest commit on that branch as "passed". Otherwise pull requests could be blocked due to required checks that remain pending. To avoid this issue, you can run `chromatic` with the `--skip` flag. This flag accepts a branch name or glob pattern.

One use case for this feature is skipping builds for branches created by a bot. For instance, Dependabot automatically updates a projects dependencies. Although some dependencies can result in UI changes, you might not find it worthwhile to run Chromatic for every single dependency update. Instead, you could rely on Chromatic running against the `main` or `develop` branch.

To skip builds for `dependabot` branches, use the following:

```shell
npx chromatic --skip 'dependabot/**'
```

<div class="aside">

Read our [configuration reference documentation](/docs/configure#options).

</div>

To apply this to multiple branches, use an "extended glob". See the [globs guide] for details.

```shell
npx chromatic --skip '@(renovate/**|dependabot/**)'
```

[globs guide]: /docs/globs