Skip to content

Commit 96f4011

Browse files
authored
Merge branch 'master' into release-health
2 parents 7223a72 + d98f5d6 commit 96f4011

File tree

2 files changed

+32
-14
lines changed

2 files changed

+32
-14
lines changed

packages/node/src/sdk.ts

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,9 @@ 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;
96+
const detectedRelease = getSentryRelease();
97+
if (detectedRelease !== undefined) {
98+
options.release = detectedRelease;
10499
}
105100

106101
// If release is not provided, then we should disable autoSessionTracking
@@ -161,7 +156,7 @@ export async function close(timeout?: number): Promise<boolean> {
161156
}
162157

163158
/**
164-
* Function that checks if autoSessionTracking option is enabled
159+
* Function that checks if autoSessionTracking option is enabled
165160
*/
166161
export function isAutosessionTrackingEnabled(): boolean {
167162
// Also add the checks that makes sure in case when you stop session tracking or resume
@@ -171,4 +166,32 @@ export function isAutosessionTrackingEnabled(): boolean {
171166
return clientOptions.autoSessionTracking;
172167
}
173168
return false;
169+
170+
/**
171+
* Function that returns a Sentry release string dynamically from env variables
172+
*/
173+
function getSentryRelease(): string | undefined {
174+
// Always read first as Sentry takes this as precedence
175+
if (process.env.SENTRY_RELEASE) {
176+
return process.env.SENTRY_RELEASE;
177+
}
178+
179+
// This supports the variable that sentry-webpack-plugin injects
180+
const global = getGlobalObject();
181+
if (global.SENTRY_RELEASE && global.SENTRY_RELEASE.id) {
182+
return global.SENTRY_RELEASE.id;
183+
}
184+
185+
return (
186+
// GitHub Actions - https://help.github.com/en/actions/configuring-and-managing-workflows/using-environment-variables#default-environment-variables
187+
process.env.GITHUB_SHA ||
188+
// Netlify - https://docs.netlify.com/configure-builds/environment-variables/#build-metadata
189+
process.env.COMMIT_REF ||
190+
// Vercel - https://vercel.com/docs/v2/build-step#system-environment-variables
191+
process.env.VERCEL_GIT_COMMIT_SHA ||
192+
// Zeit (now known as Vercel)
193+
process.env.ZEIT_GITHUB_COMMIT_SHA ||
194+
process.env.ZEIT_GITLAB_COMMIT_SHA ||
195+
process.env.ZEIT_BITBUCKET_COMMIT_SHA
196+
);
174197
}

packages/node/test/index.test.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -278,11 +278,6 @@ describe('SentryNode initialization', () => {
278278
// Unsure if this is needed under jest.
279279
global.SENTRY_RELEASE = undefined;
280280
});
281-
test('initialization proceeds as normal if global.SENTRY_RELEASE is not set', () => {
282-
// This is mostly a happy-path test to ensure that the initialization doesn't throw an error.
283-
init({ dsn });
284-
expect(global.__SENTRY__.hub._stack[0].client.getOptions().release).toBeUndefined();
285-
});
286281

287282
describe('SDK metadata', () => {
288283
it('should set SDK data when Sentry.init() is called', () => {

0 commit comments

Comments
 (0)