Sign inSign up

Automate Chromatic with a custom provider

Chromatic automation can be included as part of any CI provider with relative ease. We’re here for you. Contact us through our in-app chat for further assistance.

Setup

To integrate Chromatic with your existing CI provider, you’ll need to add the following to your workflow:

your-workflow.yml
image: node:iron

- run:
    # ⚠️ See your package manager's documentation for the correct command to install dependencies in a CI environment.
    command: npm ci
- run:
    command: npm test
- run:
     # 👇 Publish Storybook and run visual tests in Chromatic
    command: npx chromatic

We recommend saving the project token as a (secret) 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. See your provider’s documentation for reference.

Run Chromatic on specific branches

Depending on the CI provider you’re using, running Chromatic from a specific branch will not be a issue. Refer to your CI documentation for further details.

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:

your-workflow.yml
- run:
 # 👇 Runs Chromatic with the flag to compress the build output.
 command: 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 buildScriptName parameter 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 alternatively point the action at the build output using the storybookBuildDir parameter.

your-workflow.yml
# 👇 Runs Chromatic sequentially for each monorepo subproject.
- run:
    command: cd packages/project_1 && npx chromatic
    environment:
      CHROMATIC_PROJECT_TOKEN: $CHROMATIC_PROJECT_TOKEN_1
- run:
    command: cd packages/project_2 && npx chromatic
    environment:
      CHROMATIC_PROJECT_TOKEN: $CHROMATIC_PROJECT_TOKEN_2

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

your-workflow.yml
# 👇 Runs Chromatic in parallel for each monorepo subproject.
- parallel:
    - run:
        command: cd packages/project_1 && npx chromatic
        environment:
          CHROMATIC_PROJECT_TOKEN: $CHROMATIC_PROJECT_TOKEN_1
    - run:
        command: cd packages/project_2 && npx chromatic
        environment:
          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:

your-workflow.yml
- run:
    # 👇 Enables Chromatic's TurboSnap feature.
    command: npx chromatic --only-changed

TurboSnap is highly customizable and can be configured to fit your requirements. For more information, read our documentation.

Overriding Chromatic’s branch detection

If your worflow 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:

your-workflow.yml
- run:
    # 👇 Runs the Chromatic CLI with the --branch-name flag to override the baseline branch
    command: 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.

UI Test and UI Review

UI Tests and UI Review rely on branch and baseline detection to keep track of 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 build 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 job will continue in such cases. For example:

your-workflow.yml
# Your custom CI implementation

- run:
    # 👇 Runs Chromatic with the flag to prevent stage failure
    command: npx chromatic --exit-zero-on-changes

When using --exit-zero-on-changes your job will still stop and fail if your Storybook contains stories that error. If you’d prefer Chromatic never to block your job, you can use npm run chromatic || true.

Re-run failed builds after verifying UI test results

Builds that contain visual changes need to be verified. They will fail if you are not using --exit-zero-on-changes. Once you accept all the changes, re-run the workflow and the job 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 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 chromatic command and include the --auto-accept-changes flag. For example:

your-workflow.yml
# Your custom CI implementation

- run:
    # 👇 Checks if the current branch is not main and runs Chromatic
    if: branch != main
      command: npx chromatic
    # 👇 Checks if the current branch is main and accepts all changes in Chromatic
    else:
      command: npx chromatic --auto-accept-changes

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

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:

npx chromatic --skip 'dependabot/**'

To apply this to multiple branches, use an “extended glob”. See the globs guide for details.

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