Skip to content

Commit 70c10a9

Browse files
committed
fix: prioritise main branch in canary detection
1 parent b9f18da commit 70c10a9

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

config/env.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,16 @@ export const prNumber = process.env.REVIEW_ID || process.env.VERCEL_GIT_PULL_REQ
3939
export const gitBranch = process.env.BRANCH || process.env.VERCEL_GIT_COMMIT_REF
4040

4141
/**
42-
* Environment variable `VERCEL_ENV` provided by Vercel.
43-
* `production`, `preview`, `development`, or a custom environment name (e.g. `canary`).
44-
* @see {@link https://vercel.com/docs/environment-variables/system-environment-variables#VERCEL_ENV}
42+
* Whether this is the canary environment (main.npmx.dev).
4543
*
46-
* Whether this is the canary custom Vercel environment (main.npmx.dev).
44+
* Detected via the custom Vercel environment (`VERCEL_ENV === 'canary'`),
45+
* or as a fallback, a production deploy from the `main` branch.
46+
*
47+
* @see {@link https://vercel.com/docs/environment-variables/system-environment-variables#VERCEL_ENV}
4748
*/
48-
export const isCanary = process.env.VERCEL_ENV === 'canary'
49+
export const isCanary =
50+
process.env.VERCEL_ENV === 'canary' ||
51+
(process.env.VERCEL_ENV === 'production' && gitBranch === 'main')
4952

5053
/**
5154
* Environment variable `CONTEXT` provided by Netlify.

test/unit/config/env.spec.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,22 @@ describe('isCanary', () => {
3434
expect(isCanary).toBe(true)
3535
})
3636

37+
it('returns true when VERCEL_ENV is "production" and branch is "main"', async () => {
38+
vi.stubEnv('VERCEL_ENV', 'production')
39+
vi.stubEnv('VERCEL_GIT_COMMIT_REF', 'main')
40+
const { isCanary } = await import('../../../config/env')
41+
42+
expect(isCanary).toBe(true)
43+
})
44+
3745
it.each([
38-
['production', 'production'],
39-
['preview', 'preview'],
40-
['development', 'development'],
41-
['unset', undefined],
42-
])('returns false when VERCEL_ENV is %s', async (_label, value) => {
46+
['production (non-main branch)', 'production', 'v1.0.0'],
47+
['preview', 'preview', undefined],
48+
['development', 'development', undefined],
49+
['unset', undefined, undefined],
50+
])('returns false when VERCEL_ENV is %s', async (_label, value, branch) => {
4351
if (value !== undefined) vi.stubEnv('VERCEL_ENV', value)
52+
if (branch !== undefined) vi.stubEnv('VERCEL_GIT_COMMIT_REF', branch)
4453
const { isCanary } = await import('../../../config/env')
4554

4655
expect(isCanary).toBe(false)
@@ -98,7 +107,16 @@ describe('getEnv', () => {
98107
expect(result.env).toBe('preview')
99108
})
100109

101-
it('returns "release" for Vercel production deploys', async () => {
110+
it('returns "canary" for Vercel production deploys from main branch', async () => {
111+
vi.stubEnv('VERCEL_ENV', 'production')
112+
vi.stubEnv('VERCEL_GIT_COMMIT_REF', 'main')
113+
const { getEnv } = await import('../../../config/env')
114+
const result = await getEnv(false)
115+
116+
expect(result.env).toBe('canary')
117+
})
118+
119+
it('returns "release" for Vercel production deploys from non-main branch', async () => {
102120
vi.stubEnv('VERCEL_ENV', 'production')
103121
vi.stubEnv('VERCEL_GIT_COMMIT_REF', 'v1.0.0')
104122
const { getEnv } = await import('../../../config/env')

0 commit comments

Comments
 (0)