Skip to content

Commit a33d60d

Browse files
committed
Added a function that gets a sentry release from env dynamically if release is not provided on init
1 parent e39ef59 commit a33d60d

File tree

1 file changed

+32
-9
lines changed

1 file changed

+32
-9
lines changed

packages/node/src/sdk.ts

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,7 @@ export function init(options: NodeOptions = {}): void {
9393
}
9494

9595
if (options.release === undefined) {
96-
const global = getGlobalObject<Window>();
97-
// Prefer env var over global
98-
if (process.env.SENTRY_RELEASE) {
99-
options.release = process.env.SENTRY_RELEASE;
100-
}
101-
// This supports the variable that sentry-webpack-plugin injects
102-
else if (global.SENTRY_RELEASE && global.SENTRY_RELEASE.id) {
103-
options.release = global.SENTRY_RELEASE.id;
104-
}
96+
options.release = getSentryRelease();
10597
}
10698

10799
if (options.environment === undefined && process.env.SENTRY_ENVIRONMENT) {
@@ -152,3 +144,34 @@ export async function close(timeout?: number): Promise<boolean> {
152144
}
153145
return Promise.reject(false);
154146
}
147+
148+
/**
149+
* A function that returns a Sentry release string dynamically, if a release string is not passed
150+
*/
151+
export function getSentryRelease(): string {
152+
// Fetches the variable that sentry-webpack-plugin injects
153+
const global = getGlobalObject<Window>();
154+
let globalSentryRelease = undefined;
155+
if (global.SENTRY_RELEASE && global.SENTRY_RELEASE.id) {
156+
globalSentryRelease = global.SENTRY_RELEASE.id;
157+
}
158+
159+
const sentryRelease = JSON.stringify(
160+
// Always read first as Sentry takes this as precedence
161+
process.env.SENTRY_RELEASE ||
162+
// This supports the variable that sentry-webpack-plugin injects
163+
globalSentryRelease ||
164+
// GitHub Actions - https://help.github.com/en/actions/configuring-and-managing-workflows/using-environment-variables#default-environment-variables
165+
process.env.GITHUB_SHA ||
166+
// Netlify - https://docs.netlify.com/configure-builds/environment-variables/#build-metadata
167+
process.env.COMMIT_REF ||
168+
// Vercel - https://vercel.com/docs/v2/build-step#system-environment-variables
169+
process.env.VERCEL_GIT_COMMIT_SHA ||
170+
// Zeit (now known as Vercel)
171+
process.env.ZEIT_GITHUB_COMMIT_SHA ||
172+
process.env.ZEIT_GITLAB_COMMIT_SHA ||
173+
process.env.ZEIT_BITBUCKET_COMMIT_SHA ||
174+
'',
175+
);
176+
return sentryRelease;
177+
}

0 commit comments

Comments
 (0)