Skip to content

Commit 3b989fa

Browse files
feat(plugin): Add support for .env.sentry-build-plugin (#4281)
1 parent 08cee3d commit 3b989fa

File tree

8 files changed

+38
-0
lines changed

8 files changed

+38
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,4 @@ node_modules.bak
9090

9191
# Sentry React Native Monorepo
9292
/packages/core/README.md
93+
.env.sentry-build-plugin

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@
1010

1111
### Features
1212

13+
- Add support for `.env.sentry-build-plugin` ([#4281](https://github.com/getsentry/sentry-react-native/pull/4281))
14+
15+
Don't commit the file to your repository. Use it to set your Sentry Auth Token.
16+
17+
```
18+
SENTRY_AUTH_TOKEN=your_token_here
19+
```
20+
1321
- Add Sentry Metro Server Source Context middleware ([#4287](https://github.com/getsentry/sentry-react-native/pull/4287))
1422

1523
This enables the SDK to add source context to locally symbolicated events using the Metro Development Server.

packages/core/scripts/expo-upload-sourcemaps.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,20 @@ function groupAssets(assetPaths) {
102102
return groups;
103103
}
104104

105+
function loadDotenv(dotenvPath) {
106+
try {
107+
const dotenvFile = fs.readFileSync(dotenvPath, 'utf-8');
108+
// 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.
109+
// dotenv is dependency of @expo/env, so we can just require it here
110+
const dotenvResult = require('dotenv').parse(dotenvFile);
111+
112+
Object.assign(process.env, dotenvResult);
113+
} catch (error) {
114+
console.warn('⚠️ Failed to load environment variables using dotenv.');
115+
console.warn(error);
116+
}
117+
}
118+
105119
process.env.NODE_ENV = process.env.NODE_ENV || 'development'; // Ensures precedence .env.development > .env (the same as @expo/cli)
106120
const projectRoot = '.'; // Assume script is run from the project root
107121
try {
@@ -111,6 +125,8 @@ try {
111125
console.warn(error);
112126
}
113127

128+
loadDotenv(path.join(projectRoot, '.env.sentry-build-plugin'));
129+
114130
let sentryOrg = getEnvVar(SENTRY_ORG);
115131
let sentryUrl = getEnvVar(SENTRY_URL);
116132
let sentryProject = getEnvVar(SENTRY_PROJECT);

packages/core/scripts/sentry-xcode-debug-files.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ set -e
1717

1818
LOCAL_NODE_BINARY=${NODE_BINARY:-node}
1919

20+
# The project root by default is one level up from the ios directory
21+
RN_PROJECT_ROOT="${PROJECT_DIR}/.."
22+
2023
[ -z "$SENTRY_PROPERTIES" ] && export SENTRY_PROPERTIES=sentry.properties
24+
[ -z "$SENTRY_DOTENV_PATH" ] && export SENTRY_DOTENV_PATH="$RN_PROJECT_ROOT/.env.sentry-build-plugin"
2125

2226
[ -z "$SENTRY_CLI_EXECUTABLE" ] && SENTRY_CLI_PACKAGE_PATH=$("$LOCAL_NODE_BINARY" --print "require('path').dirname(require.resolve('@sentry/cli/package.json'))")
2327
[ -z "$SENTRY_CLI_EXECUTABLE" ] && SENTRY_CLI_EXECUTABLE="${SENTRY_CLI_PACKAGE_PATH}/bin/sentry-cli"

packages/core/scripts/sentry-xcode.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ set -x -e
99

1010
LOCAL_NODE_BINARY=${NODE_BINARY:-node}
1111

12+
# The project root by default is one level up from the ios directory
13+
RN_PROJECT_ROOT="${PROJECT_DIR}/.."
14+
1215
[ -z "$SENTRY_PROPERTIES" ] && export SENTRY_PROPERTIES=sentry.properties
16+
[ -z "$SENTRY_DOTENV_PATH" ] && export SENTRY_DOTENV_PATH="$RN_PROJECT_ROOT/.env.sentry-build-plugin"
1317
[ -z "$SOURCEMAP_FILE" ] && export SOURCEMAP_FILE="$DERIVED_FILE_DIR/main.jsbundle.map"
1418

1519
[ -z "$SENTRY_CLI_EXECUTABLE" ] && SENTRY_CLI_PACKAGE_PATH=$("$LOCAL_NODE_BINARY" --print "require('path').dirname(require.resolve('@sentry/cli/package.json'))")

packages/core/sentry.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,9 @@ gradle.projectsEvaluated {
203203

204204
project.logger.lifecycle("Sentry-CLI arguments: ${args}")
205205
def osCompatibility = Os.isFamily(Os.FAMILY_WINDOWS) ? ['cmd', '/c', 'node'] : []
206+
if (!System.getenv('SENTRY_DOTENV_PATH')) {
207+
environment('SENTRY_DOTENV_PATH', "$reactRoot/.env.sentry-build-plugin")
208+
}
206209
commandLine(*osCompatibility, *args)
207210
}
208211
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SENTRY_AUTH_TOKEN=your_token_here
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SENTRY_AUTH_TOKEN=your_token_here

0 commit comments

Comments
 (0)