Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 32 additions & 8 deletions packages/node/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,9 @@ export function init(options: NodeOptions = {}): void {
}

if (options.release === undefined) {
const global = getGlobalObject<Window>();
// Prefer env var over global
if (process.env.SENTRY_RELEASE) {
options.release = process.env.SENTRY_RELEASE;
}
// This supports the variable that sentry-webpack-plugin injects
else if (global.SENTRY_RELEASE && global.SENTRY_RELEASE.id) {
options.release = global.SENTRY_RELEASE.id;
const detectedRelease = getSentryRelease();
if (detectedRelease !== undefined) {
options.release = detectedRelease;
}
}

Expand Down Expand Up @@ -152,3 +147,32 @@ export async function close(timeout?: number): Promise<boolean> {
}
return Promise.reject(false);
}

/**
* A function that returns a Sentry release string dynamically from env variables
*/
function getSentryRelease(): string | undefined {
// Always read first as Sentry takes this as precedence
if (process.env.SENTRY_RELEASE) {
return process.env.SENTRY_RELEASE;
}

// This supports the variable that sentry-webpack-plugin injects
const global = getGlobalObject();
if (global.SENTRY_RELEASE && global.SENTRY_RELEASE.id) {
return global.SENTRY_RELEASE.id;
}

return (
// GitHub Actions - https://help.github.com/en/actions/configuring-and-managing-workflows/using-environment-variables#default-environment-variables
process.env.GITHUB_SHA ||
// Netlify - https://docs.netlify.com/configure-builds/environment-variables/#build-metadata
process.env.COMMIT_REF ||
// Vercel - https://vercel.com/docs/v2/build-step#system-environment-variables
process.env.VERCEL_GIT_COMMIT_SHA ||
// Zeit (now known as Vercel)
process.env.ZEIT_GITHUB_COMMIT_SHA ||
process.env.ZEIT_GITLAB_COMMIT_SHA ||
process.env.ZEIT_BITBUCKET_COMMIT_SHA
);
}
5 changes: 0 additions & 5 deletions packages/node/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,11 +278,6 @@ describe('SentryNode initialization', () => {
// Unsure if this is needed under jest.
global.SENTRY_RELEASE = undefined;
});
test('initialization proceeds as normal if global.SENTRY_RELEASE is not set', () => {
// This is mostly a happy-path test to ensure that the initialization doesn't throw an error.
init({ dsn });
expect(global.__SENTRY__.hub._stack[0].client.getOptions().release).toBeUndefined();
});

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