Storybook Git Metadata
Status: Stable
This guide explains how a Storybook publishes its git metadata, enabling the design tests library to detect the current deployment version being tested.
Overview
The design tests library can automatically detect the current deployment version by reading /git-metadata.json from your Storybook build. The @qs/storybook-addon-design-tests addon writes this file during storybook dev and storybook build.
What it provides: two fields, gitHash and createdAt. The library uses the gitHash as the reference when the file has none. The file is public on every Storybook host, so it holds nothing else.
Why it's needed:
- Tracks which deployment is being tested
- Enables baseline version tracking
- Provides attribution for visual changes
- Helps debug "what changed" questions
Recommended: the Storybook addon
Add @qs/storybook-addon-design-tests to the addons of the Storybook main config. It supports Storybook 7.6, 8.6 and 10 and requires Node.js 24 or later.
// .storybook/main.js
module.exports = {
addons: ["@qs/storybook-addon-design-tests"],
};
storybook dev and storybook build then serve /git-metadata.json:
{
"gitHash": "abc123def456789...",
"createdAt": "2025-10-28T14:00:00.000Z"
}
No generator script, no staticDirs entry and no .gitignore line are needed.
Without a git checkout
The addon resolves gitHash in this order: DESIGN_TESTS_GIT_HASH; the commit variable of a common CI system (GIT_COMMIT in Jenkins with the git plugin, GITHUB_SHA, CI_COMMIT_SHA in GitLab CI, BITBUCKET_COMMIT, BUILD_SOURCEVERSION in Azure Pipelines); git rev-parse HEAD; and the build fails with a message when none resolves. DESIGN_TESTS_BUILD_TIME (ISO 8601) overrides createdAt; a value that is not a date fails the build. The build also fails when the addon cannot write the file. Set the option enabled: false to turn the addon off. The addon prints one line that names the source.
Most pipelines need no setting, because the commit variable of the CI system is already set. Set DESIGN_TESTS_GIT_HASH when the build runs from a copy of the source without .git, or when the variable is empty. Examples:
// Jenkins, declarative pipeline
environment { DESIGN_TESTS_GIT_HASH = "${env.GIT_COMMIT}" }
stages {
stage("Build Storybook") { steps { sh "pnpm build-storybook" } }
}
# GitHub Actions
- name: Build Storybook
run: pnpm build-storybook
env:
DESIGN_TESTS_GIT_HASH: ${{ github.sha }}
# GitLab CI
build-storybook:
script: pnpm build-storybook
variables:
DESIGN_TESTS_GIT_HASH: $CI_COMMIT_SHA
# Bitbucket Pipelines
- step:
script:
- export DESIGN_TESTS_GIT_HASH=$BITBUCKET_COMMIT
- pnpm build-storybook
The Azure Pipelines variable BUILD_SOURCEVERSION needs no mapping. For a build that runs outside CI, DESIGN_TESTS_GIT_HASH=$(git rev-parse HEAD) pnpm build-storybook sets it by hand.
Fallback: the generator script
Use the script only for a project that is not a Storybook, or that cannot use the addon. It writes the same two fields. The steps below apply to the script only; the addon needs none of them.
1. Create the Generator Script
Create .storybook/scripts/generate-git-metadata.js:
const fs = require('fs');
const { execSync } = require('child_process');
const path = require('path');
function generateGitMetadata() {
try {
const gitHash = process.env.DESIGN_TESTS_GIT_HASH || execSync('git rev-parse HEAD').toString().trim();
const metadata = { gitHash, createdAt: new Date().toISOString() };
const outputDir = path.join(__dirname, '../generated');
fs.mkdirSync(outputDir, { recursive: true });
fs.writeFileSync(path.join(outputDir, 'git-metadata.json'), JSON.stringify(metadata, null, 2), 'utf-8');
console.log(`Generated git-metadata.json (${gitHash})`);
} catch (error) {
console.warn('Could not generate git metadata:', error.message);
// Non-fatal - tests can still run without this metadata
}
}
if (require.main === module) {
generateGitMetadata();
}
module.exports = { generateGitMetadata };
2. Configure Storybook to Include the File
Update .storybook/main.js or .storybook/main.ts:
module.exports = {
// ... other config
staticDirs: [
'../public',
// Serve generated files at root (includes git-metadata.json)
{ from: '../.storybook/generated', to: '/' },
],
// ... rest of config
};
How it works:
- Files in
.storybook/generated/are served at the root of the Storybook /git-metadata.jsonbecomes accessible athttps://your-storybook.com/git-metadata.json- Design tests library fetches this file during test preparation
3. Update Build Script
Update package.json to run the generator before building:
{
"scripts": {
"build-storybook": "node .storybook/scripts/generate-git-metadata.js && storybook build",
"build-storybook:ci": "node .storybook/scripts/generate-git-metadata.js && storybook build --quiet"
}
}
4. Add to .gitignore
Add the generated directory to .gitignore:
# Storybook generated files
.storybook/generated/
Why ignore it:
- Generated during build, not source code
- Contains git hash specific to current commit
- Should be regenerated on each build
Testing the Setup
After implementing, verify the metadata is accessible:
-
Build Storybook:
npm run build-storybook -
Check the generated file:
cat storybook-static/git-metadata.jsonShould output:
{
"gitHash": "abc123def456789...",
"createdAt": "2025-10-28T14:00:00.000Z"
} -
Serve and test:
npx http-server storybook-static -p 8080
curl http://localhost:8080/git-metadata.json -
Run design tests:
# The library will automatically detect the metadata
pnpm run design-tests run --project storybookLook for this in the output:
🔍 Detecting current deployment metadata...
✓ Current metadata detected from /git-metadata.json
✓ Current: abc123def456789...
Fallback Detection
If /git-metadata.json is not available, the design tests library will fall back to:
-
Environment variables:
CURRENT_REFERENCE="abc123|develop|2025-10-28T14:00:00Z" \
CURRENT_REFERENCE_LINK="https://example.com/commit/abc123" \
pnpm test -
Legacy meta tag detection:
<meta name="git-commit" content="abc123def456"> -
Unknown fallback:
reference: "unknown|unknown|{current-timestamp}"
Benefits
✅ Automatic version tracking - No manual configuration needed ✅ Traceability - Link back to exact commit being tested ✅ Debugging - Know exactly what changed when tests fail ✅ Baseline attribution - Track which commit created current baseline ✅ CI-friendly - Works in Jenkins, GitHub Actions, etc.
Troubleshooting
Q: The file isn't accessible at /git-metadata.json
A: With the addon, check that it is listed in addons and that enabled is not false. With the script, check that staticDirs in .storybook/main.js includes the generated directory with to: '/'
Q: Git commands fail during build
A: With the addon, the build fails with a message. Set DESIGN_TESTS_GIT_HASH, build where the CI system sets its commit variable (see Without a git checkout), or set the option enabled: false. With the script, run the build in a git repository (not a .zip download)
Q: Design tests still show "unknown" reference A: Verify the file exists and is valid JSON. Check browser console for fetch errors.
Q: Can I use a different filename?
A: Yes, but you'll need to customize the detectCurrentMeta function in the library
Related Documentation
- Storybook addon - Installation, options and commit resolution
- Baseline Management - How baseline metadata works
- Adding Projects - Adding new projects to test configuration