From 3b989fa0c7e93ce3586735a1ab234f7dd36275f3 Mon Sep 17 00:00:00 2001 From: Krystof Woldrich <31292499+krystofwoldrich@users.noreply.github.com> Date: Tue, 19 Nov 2024 12:22:14 +0100 Subject: [PATCH] feat(plugin): Add support for `.env.sentry-build-plugin` (#4281) --- .gitignore | 1 + CHANGELOG.md | 8 ++++++++ packages/core/scripts/expo-upload-sourcemaps.js | 16 ++++++++++++++++ .../core/scripts/sentry-xcode-debug-files.sh | 4 ++++ packages/core/scripts/sentry-xcode.sh | 4 ++++ packages/core/sentry.gradle | 3 +++ samples/expo/.env.sentry-build-plugin.example | 1 + .../.env.sentry-build-plugin.example | 1 + 8 files changed, 38 insertions(+) create mode 100644 samples/expo/.env.sentry-build-plugin.example create mode 100644 samples/react-native/.env.sentry-build-plugin.example diff --git a/.gitignore b/.gitignore index 1419bce38d..1870007b3f 100644 --- a/.gitignore +++ b/.gitignore @@ -90,3 +90,4 @@ node_modules.bak # Sentry React Native Monorepo /packages/core/README.md +.env.sentry-build-plugin diff --git a/CHANGELOG.md b/CHANGELOG.md index 72fb0248a6..4a0ce21803 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,14 @@ ### Features +- Add support for `.env.sentry-build-plugin` ([#4281](https://github.com/getsentry/sentry-react-native/pull/4281)) + + Don't commit the file to your repository. Use it to set your Sentry Auth Token. + + ``` + SENTRY_AUTH_TOKEN=your_token_here + ``` + - Add Sentry Metro Server Source Context middleware ([#4287](https://github.com/getsentry/sentry-react-native/pull/4287)) This enables the SDK to add source context to locally symbolicated events using the Metro Development Server. diff --git a/packages/core/scripts/expo-upload-sourcemaps.js b/packages/core/scripts/expo-upload-sourcemaps.js index 0f244f2ac1..997f2c84d3 100755 --- a/packages/core/scripts/expo-upload-sourcemaps.js +++ b/packages/core/scripts/expo-upload-sourcemaps.js @@ -102,6 +102,20 @@ function groupAssets(assetPaths) { return groups; } +function loadDotenv(dotenvPath) { + try { + const dotenvFile = fs.readFileSync(dotenvPath, 'utf-8'); + // NOTE: Do not use the dotenv.config API directly to read the dotenv file! For some ungodly reason, it falls back to reading `${process.cwd()}/.env` which is absolutely not what we want. + // dotenv is dependency of @expo/env, so we can just require it here + const dotenvResult = require('dotenv').parse(dotenvFile); + + Object.assign(process.env, dotenvResult); + } catch (error) { + console.warn('⚠️ Failed to load environment variables using dotenv.'); + console.warn(error); + } +} + process.env.NODE_ENV = process.env.NODE_ENV || 'development'; // Ensures precedence .env.development > .env (the same as @expo/cli) const projectRoot = '.'; // Assume script is run from the project root try { @@ -111,6 +125,8 @@ try { console.warn(error); } +loadDotenv(path.join(projectRoot, '.env.sentry-build-plugin')); + let sentryOrg = getEnvVar(SENTRY_ORG); let sentryUrl = getEnvVar(SENTRY_URL); let sentryProject = getEnvVar(SENTRY_PROJECT); diff --git a/packages/core/scripts/sentry-xcode-debug-files.sh b/packages/core/scripts/sentry-xcode-debug-files.sh index 60cca3661f..dc6e48aed7 100755 --- a/packages/core/scripts/sentry-xcode-debug-files.sh +++ b/packages/core/scripts/sentry-xcode-debug-files.sh @@ -17,7 +17,11 @@ set -e LOCAL_NODE_BINARY=${NODE_BINARY:-node} +# The project root by default is one level up from the ios directory +RN_PROJECT_ROOT="${PROJECT_DIR}/.." + [ -z "$SENTRY_PROPERTIES" ] && export SENTRY_PROPERTIES=sentry.properties +[ -z "$SENTRY_DOTENV_PATH" ] && export SENTRY_DOTENV_PATH="$RN_PROJECT_ROOT/.env.sentry-build-plugin" [ -z "$SENTRY_CLI_EXECUTABLE" ] && SENTRY_CLI_PACKAGE_PATH=$("$LOCAL_NODE_BINARY" --print "require('path').dirname(require.resolve('@sentry/cli/package.json'))") [ -z "$SENTRY_CLI_EXECUTABLE" ] && SENTRY_CLI_EXECUTABLE="${SENTRY_CLI_PACKAGE_PATH}/bin/sentry-cli" diff --git a/packages/core/scripts/sentry-xcode.sh b/packages/core/scripts/sentry-xcode.sh index e7b9d5479e..78970c4c60 100755 --- a/packages/core/scripts/sentry-xcode.sh +++ b/packages/core/scripts/sentry-xcode.sh @@ -9,7 +9,11 @@ set -x -e LOCAL_NODE_BINARY=${NODE_BINARY:-node} +# The project root by default is one level up from the ios directory +RN_PROJECT_ROOT="${PROJECT_DIR}/.." + [ -z "$SENTRY_PROPERTIES" ] && export SENTRY_PROPERTIES=sentry.properties +[ -z "$SENTRY_DOTENV_PATH" ] && export SENTRY_DOTENV_PATH="$RN_PROJECT_ROOT/.env.sentry-build-plugin" [ -z "$SOURCEMAP_FILE" ] && export SOURCEMAP_FILE="$DERIVED_FILE_DIR/main.jsbundle.map" [ -z "$SENTRY_CLI_EXECUTABLE" ] && SENTRY_CLI_PACKAGE_PATH=$("$LOCAL_NODE_BINARY" --print "require('path').dirname(require.resolve('@sentry/cli/package.json'))") diff --git a/packages/core/sentry.gradle b/packages/core/sentry.gradle index f0adc88a7f..fbbf567412 100644 --- a/packages/core/sentry.gradle +++ b/packages/core/sentry.gradle @@ -203,6 +203,9 @@ gradle.projectsEvaluated { project.logger.lifecycle("Sentry-CLI arguments: ${args}") def osCompatibility = Os.isFamily(Os.FAMILY_WINDOWS) ? ['cmd', '/c', 'node'] : [] + if (!System.getenv('SENTRY_DOTENV_PATH')) { + environment('SENTRY_DOTENV_PATH', "$reactRoot/.env.sentry-build-plugin") + } commandLine(*osCompatibility, *args) } } diff --git a/samples/expo/.env.sentry-build-plugin.example b/samples/expo/.env.sentry-build-plugin.example new file mode 100644 index 0000000000..82f38b516b --- /dev/null +++ b/samples/expo/.env.sentry-build-plugin.example @@ -0,0 +1 @@ +SENTRY_AUTH_TOKEN=your_token_here diff --git a/samples/react-native/.env.sentry-build-plugin.example b/samples/react-native/.env.sentry-build-plugin.example new file mode 100644 index 0000000000..82f38b516b --- /dev/null +++ b/samples/react-native/.env.sentry-build-plugin.example @@ -0,0 +1 @@ +SENTRY_AUTH_TOKEN=your_token_here