Skip to content

Commit 723dcad

Browse files
committed
chore: use Vercel environment variables
1 parent dc0f778 commit 723dcad

1 file changed

Lines changed: 24 additions & 5 deletions

File tree

config/env.ts

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,39 @@ export { version } from '../package.json'
77
* Environment variable `PULL_REQUEST` provided by Netlify.
88
* @see {@link https://docs.netlify.com/build/configure-builds/environment-variables/#git-metadata}
99
*
10+
* Environment variable `VERCEL_GIT_PULL_REQUEST_ID` provided by Vercel.
11+
* @see {@link https://vercel.com/docs/environment-variables/system-environment-variables#VERCEL_GIT_PULL_REQUEST_ID}
12+
*
1013
* Whether triggered by a GitHub PR
1114
*/
12-
export const isPR = process.env.PULL_REQUEST === 'true'
15+
export const isPR = process.env.PULL_REQUEST === 'true' || !!process.env.VERCEL_GIT_PULL_REQUEST_ID
1316

1417
/**
1518
* Environment variable `BRANCH` provided by Netlify.
1619
* @see {@link https://docs.netlify.com/build/configure-builds/environment-variables/#git-metadata}
1720
*
21+
* Environment variable `VERCEL_GIT_COMMIT_REF` provided by Vercel.
22+
* @see {@link https://vercel.com/docs/environment-variables/system-environment-variables#VERCEL_GIT_COMMIT_REF}
23+
*
1824
* Git branch
1925
*/
20-
export const gitBranch = process.env.BRANCH
26+
export const gitBranch = process.env.BRANCH || process.env.VERCEL_GIT_COMMIT_REF
2127

2228
/**
2329
* Environment variable `CONTEXT` provided by Netlify.
2430
* @see {@link https://docs.netlify.com/build/configure-builds/environment-variables/#build-metadata}
2531
*
32+
* Environment variable `VERCEL_ENV` provided by Vercel.
33+
* @see {@link https://vercel.com/docs/environment-variables/system-environment-variables#VERCEL_ENV}
34+
*
2635
* Whether triggered by PR, `deploy-preview` or `dev`.
2736
*/
2837
export const isPreview =
29-
isPR || process.env.CONTEXT === 'deploy-preview' || process.env.CONTEXT === 'dev'
38+
isPR ||
39+
process.env.CONTEXT === 'deploy-preview' ||
40+
process.env.CONTEXT === 'dev' ||
41+
process.env.VERCEL_ENV === 'preview' ||
42+
process.env.VERCEL_ENV === 'development'
3043

3144
const git = Git()
3245
export async function getGitInfo() {
@@ -39,14 +52,20 @@ export async function getGitInfo() {
3952

4053
let commit
4154
try {
42-
commit = await git.revparse(['HEAD'])
55+
// Netlify: COMMIT_REF, Vercel: VERCEL_GIT_COMMIT_SHA
56+
commit =
57+
process.env.COMMIT_REF || process.env.VERCEL_GIT_COMMIT_SHA || (await git.revparse(['HEAD']))
4358
} catch {
4459
commit = 'unknown'
4560
}
4661

4762
let shortCommit
4863
try {
49-
shortCommit = await git.revparse(['--short=7', 'HEAD'])
64+
if (commit && commit !== 'unknown') {
65+
shortCommit = commit.slice(0, 7)
66+
} else {
67+
shortCommit = await git.revparse(['--short=7', 'HEAD'])
68+
}
5069
} catch {
5170
shortCommit = 'unknown'
5271
}

0 commit comments

Comments
 (0)