Skip to content

Commit

Permalink
feat(plugin): Add support for .env.sentry-build-plugin (#4281)
Browse files Browse the repository at this point in the history
  • Loading branch information
krystofwoldrich authored Nov 19, 2024
1 parent 08cee3d commit 3b989fa
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,4 @@ node_modules.bak

# Sentry React Native Monorepo
/packages/core/README.md
.env.sentry-build-plugin
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
16 changes: 16 additions & 0 deletions packages/core/scripts/expo-upload-sourcemaps.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions packages/core/scripts/sentry-xcode-debug-files.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 4 additions & 0 deletions packages/core/scripts/sentry-xcode.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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'))")
Expand Down
3 changes: 3 additions & 0 deletions packages/core/sentry.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
1 change: 1 addition & 0 deletions samples/expo/.env.sentry-build-plugin.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SENTRY_AUTH_TOKEN=your_token_here
1 change: 1 addition & 0 deletions samples/react-native/.env.sentry-build-plugin.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SENTRY_AUTH_TOKEN=your_token_here

0 comments on commit 3b989fa

Please sign in to comment.