@sentry/core
Version:
Base implementation for all Sentry JavaScript SDKs
85 lines (84 loc) • 2.98 kB
JavaScript
import { consoleSandbox } from "./debug-logger.js";
// src/utils/commitEnv.ts
/**
* Environment variables that CI and hosting providers set to the commit being built, in the order
* release detection checks them. The list is in approximate alpha order, separated into 3 categories:
* 1. Git providers
* 2. CI providers with specific environment variables (has the provider name in the variable name)
* 3. CI providers with generic environment variables (checked for last to prevent possible false positives)
*/
const COMMIT_ENV_VARS = [
"GITHUB_SHA",
"CI_MERGE_REQUEST_SOURCE_BRANCH_SHA",
"CI_BUILD_REF",
"CI_COMMIT_SHA",
"BITBUCKET_COMMIT",
"APPVEYOR_PULL_REQUEST_HEAD_COMMIT",
"APPVEYOR_REPO_COMMIT",
"CODEBUILD_RESOLVED_SOURCE_VERSION",
"AWS_COMMIT_ID",
"BUILD_SOURCEVERSION",
"GIT_CLONE_COMMIT_HASH",
"BUDDY_EXECUTION_REVISION",
"BUILDKITE_COMMIT",
"CIRCLE_SHA1",
"CIRRUS_CHANGE_IN_REPO",
"CF_REVISION",
"CM_COMMIT",
"CF_PAGES_COMMIT_SHA",
"DRONE_COMMIT_SHA",
"FC_GIT_COMMIT_SHA",
"HEROKU_TEST_RUN_COMMIT_VERSION",
"HEROKU_BUILD_COMMIT",
"HEROKU_SLUG_COMMIT",
"RAILWAY_GIT_COMMIT_SHA",
"RENDER_GIT_COMMIT",
"SEMAPHORE_GIT_SHA",
"TRAVIS_PULL_REQUEST_SHA",
"VERCEL_GIT_COMMIT_SHA",
"VERCEL_GITHUB_COMMIT_SHA",
"VERCEL_GITLAB_COMMIT_SHA",
"VERCEL_BITBUCKET_COMMIT_SHA",
"ZEIT_GITHUB_COMMIT_SHA",
"ZEIT_GITLAB_COMMIT_SHA",
"ZEIT_BITBUCKET_COMMIT_SHA",
"CI_COMMIT_ID",
"SOURCE_COMMIT",
"SOURCE_VERSION",
"GIT_COMMIT",
"COMMIT_REF",
"BUILD_VCS_NUMBER"
];
function warnAboutPlaceholderCommitEnvVar(name) {
consoleSandbox(() => {
console.warn(`[Sentry] Ignoring the ${name} environment variable as a release name, because its value is its own name. Set the release explicitly, or fix the variable in your build environment.`);
});
}
/**
* Reads an environment variable that should hold a commit SHA. A value that is the variable's own
* name, such as `VERCEL_GIT_COMMIT_SHA=VERCEL_GIT_COMMIT_SHA`, is ignored: it never changes, so as
* a release name it would put every build into one release. `onPlaceholder` is called for such a
* value and warns on the console by default.
*/
function readCommitEnvVar(name, onPlaceholder = warnAboutPlaceholderCommitEnvVar) {
if (typeof process === "undefined") return;
const value = process.env[name];
if (value?.trim() === name) {
onPlaceholder(name);
return;
}
return value || void 0;
}
/**
* Returns the commit that CI or the hosting provider is building, read from the first of the known
* commit environment variables that is set. Variables are read in order and only until one is
* found, so a placeholder value is only reported when it would otherwise have become the release.
*/
function getReleaseFromCommitEnv(onPlaceholder = warnAboutPlaceholderCommitEnvVar) {
for (const name of COMMIT_ENV_VARS) {
const value = readCommitEnvVar(name, onPlaceholder);
if (value) return value;
}
}
export { getReleaseFromCommitEnv, readCommitEnvVar };
//# sourceMappingURL=commitEnv.js.map