-
-
Notifications
You must be signed in to change notification settings - Fork 359
EAS Build Hooks #5666
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
EAS Build Hooks #5666
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ea7825d
EAS Build Hooks
alwx be197ae
Fixes
alwx c82726a
Code separation
alwx 697410e
Better code separation
alwx a8c5438
Fixes, yarn fix
alwx 1106d45
Fixes for build hooks
alwx f651efb
PR post-review fixes
alwx 2d1bd0b
Fixed hooks files
alwx 432693e
Dynamically determine EAS build hook name
alwx 5536bab
Rollback of deps changes
alwx f22dd26
Typo
alwx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| #!/usr/bin/env node | ||
| /** | ||
| * EAS Build Hook: on-complete | ||
| * | ||
| * This script captures EAS build completion events and reports them to Sentry. | ||
| * It uses the EAS_BUILD_STATUS environment variable to determine whether | ||
| * the build succeeded or failed. | ||
| * | ||
| * Add it to your package.json scripts: | ||
| * | ||
| * "eas-build-on-complete": "sentry-eas-build-on-complete" | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be nice to have docs for these features and add a small mention on how to use them on changelog. |
||
| * | ||
| * NOTE: Use EITHER this hook OR the separate on-error/on-success hooks, not both. | ||
| * Using both will result in duplicate events being sent to Sentry. | ||
| * | ||
| * Required environment variables: | ||
| * - SENTRY_DSN: Your Sentry DSN | ||
| * | ||
| * Optional environment variables: | ||
| * - SENTRY_EAS_BUILD_CAPTURE_SUCCESS: Set to 'true' to also capture successful builds | ||
| * - SENTRY_EAS_BUILD_TAGS: JSON string of additional tags | ||
| * - SENTRY_EAS_BUILD_ERROR_MESSAGE: Custom error message for failed builds | ||
| * - SENTRY_EAS_BUILD_SUCCESS_MESSAGE: Custom success message for successful builds | ||
| * | ||
| * EAS Build provides: | ||
| * - EAS_BUILD_STATUS: 'finished' or 'errored' | ||
| * | ||
| * @see https://docs.expo.dev/build-reference/npm-hooks/ | ||
| * @see https://docs.sentry.io/platforms/react-native/ | ||
| * | ||
| */ | ||
|
|
||
| const { loadEnv, loadHooksModule, parseBaseOptions, runHook } = require('./utils'); | ||
|
|
||
| async function main() { | ||
| loadEnv(); | ||
|
|
||
| const hooks = loadHooksModule(); | ||
| const options = { | ||
| ...parseBaseOptions(), | ||
| errorMessage: process.env.SENTRY_EAS_BUILD_ERROR_MESSAGE, | ||
| successMessage: process.env.SENTRY_EAS_BUILD_SUCCESS_MESSAGE, | ||
| captureSuccessfulBuilds: process.env.SENTRY_EAS_BUILD_CAPTURE_SUCCESS === 'true', | ||
| }; | ||
|
|
||
| await runHook('on-complete', () => hooks.captureEASBuildComplete(options)); | ||
| } | ||
|
|
||
| main().catch(error => { | ||
| // eslint-disable-next-line no-console | ||
| console.error('[Sentry] Unexpected error in eas-build-on-complete hook:', error); | ||
| process.exit(1); | ||
| }); | ||
|
alwx marked this conversation as resolved.
Outdated
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| #!/usr/bin/env node | ||
| /** | ||
| * EAS Build Hook: on-error | ||
| * | ||
| * This script captures EAS build failures and reports them to Sentry. | ||
| * Add it to your package.json scripts: | ||
| * | ||
| * "eas-build-on-error": "sentry-eas-build-on-error" | ||
|
alwx marked this conversation as resolved.
Outdated
|
||
| * | ||
| * NOTE: Use EITHER this hook (with on-success) OR the on-complete hook, not both. | ||
| * Using both will result in duplicate events being sent to Sentry. | ||
| * | ||
| * Required environment variables: | ||
| * - SENTRY_DSN: Your Sentry DSN | ||
| * | ||
| * Optional environment variables: | ||
| * - SENTRY_EAS_BUILD_TAGS: JSON string of additional tags | ||
| * - SENTRY_EAS_BUILD_ERROR_MESSAGE: Custom error message | ||
| * | ||
| * @see https://docs.expo.dev/build-reference/npm-hooks/ | ||
| * @see https://docs.sentry.io/platforms/react-native/ | ||
| */ | ||
|
|
||
| const { loadEnv, loadHooksModule, parseBaseOptions, runHook } = require('./utils'); | ||
|
|
||
| async function main() { | ||
| loadEnv(); | ||
|
|
||
| const hooks = loadHooksModule(); | ||
| const options = { | ||
| ...parseBaseOptions(), | ||
| errorMessage: process.env.SENTRY_EAS_BUILD_ERROR_MESSAGE, | ||
| }; | ||
|
|
||
| await runHook('on-error', () => hooks.captureEASBuildError(options)); | ||
| } | ||
|
|
||
| main().catch(error => { | ||
| // eslint-disable-next-line no-console | ||
| console.error('[Sentry] Unexpected error in eas-build-on-error hook:', error); | ||
| process.exit(1); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| #!/usr/bin/env node | ||
| /** | ||
| * EAS Build Hook: on-success | ||
| * | ||
| * This script captures EAS build successes and reports them to Sentry. | ||
| * Add it to your package.json scripts: | ||
| * | ||
| * "eas-build-on-success": "sentry-eas-build-on-success" | ||
|
alwx marked this conversation as resolved.
Outdated
|
||
| * | ||
| * NOTE: Use EITHER this hook (with on-error) OR the on-complete hook, not both. | ||
| * Using both will result in duplicate events being sent to Sentry. | ||
| * | ||
| * Required environment variables: | ||
| * - SENTRY_DSN: Your Sentry DSN | ||
| * - SENTRY_EAS_BUILD_CAPTURE_SUCCESS: Set to 'true' to capture successful builds | ||
| * | ||
| * Optional environment variables: | ||
| * - SENTRY_EAS_BUILD_TAGS: JSON string of additional tags | ||
| * - SENTRY_EAS_BUILD_SUCCESS_MESSAGE: Custom success message | ||
| * | ||
| * @see https://docs.expo.dev/build-reference/npm-hooks/ | ||
| * @see https://docs.sentry.io/platforms/react-native/ | ||
| */ | ||
|
|
||
| const { loadEnv, loadHooksModule, parseBaseOptions, runHook } = require('./utils'); | ||
|
|
||
| async function main() { | ||
|
alwx marked this conversation as resolved.
Outdated
|
||
| loadEnv(); | ||
|
|
||
| const hooks = loadHooksModule(); | ||
| const options = { | ||
| ...parseBaseOptions(), | ||
| successMessage: process.env.SENTRY_EAS_BUILD_SUCCESS_MESSAGE, | ||
| captureSuccessfulBuilds: process.env.SENTRY_EAS_BUILD_CAPTURE_SUCCESS === 'true', | ||
|
cursor[bot] marked this conversation as resolved.
Outdated
|
||
| }; | ||
|
|
||
| await runHook('on-success', () => hooks.captureEASBuildSuccess(options)); | ||
| } | ||
|
|
||
| main().catch(error => { | ||
| // eslint-disable-next-line no-console | ||
| console.error('[Sentry] Unexpected error in eas-build-on-success hook:', error); | ||
| process.exit(1); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| /** | ||
| * Shared utilities for EAS Build Hook scripts. | ||
| * | ||
| * @see https://docs.expo.dev/build-reference/npm-hooks/ | ||
| */ | ||
|
|
||
| /* eslint-disable no-console */ | ||
|
|
||
| const path = require('path'); | ||
| const fs = require('fs'); | ||
|
|
||
| /** | ||
| * Merges parsed env vars into process.env without overwriting existing values. | ||
| * This preserves EAS secrets and other pre-set environment variables. | ||
| * @param {object} parsed - Parsed environment variables from dotenv | ||
| */ | ||
| function mergeEnvWithoutOverwrite(parsed) { | ||
| for (const key of Object.keys(parsed)) { | ||
| if (process.env[key] === undefined) { | ||
| process.env[key] = parsed[key]; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Loads environment variables from various sources: | ||
| * - @expo/env (if available) | ||
| * - .env file (via dotenv, if available) | ||
| * - .env.sentry-build-plugin file | ||
| * | ||
| * NOTE: Existing environment variables (like EAS secrets) are NOT overwritten. | ||
| */ | ||
| function loadEnv() { | ||
| // Try @expo/env first | ||
| try { | ||
| require('@expo/env').load('.'); | ||
| } catch (_e) { | ||
| // Fallback to dotenv if available | ||
| try { | ||
| const dotenvPath = path.join(process.cwd(), '.env'); | ||
| if (fs.existsSync(dotenvPath)) { | ||
| const dotenvFile = fs.readFileSync(dotenvPath, 'utf-8'); | ||
| const dotenv = require('dotenv'); | ||
| mergeEnvWithoutOverwrite(dotenv.parse(dotenvFile)); | ||
| } | ||
| } catch (_e2) { | ||
| // No dotenv available, continue with existing env vars | ||
| } | ||
| } | ||
|
|
||
| // Also load .env.sentry-build-plugin if it exists | ||
| try { | ||
| const sentryEnvPath = path.join(process.cwd(), '.env.sentry-build-plugin'); | ||
| if (fs.existsSync(sentryEnvPath)) { | ||
| const dotenvFile = fs.readFileSync(sentryEnvPath, 'utf-8'); | ||
| const dotenv = require('dotenv'); | ||
| mergeEnvWithoutOverwrite(dotenv.parse(dotenvFile)); | ||
| } | ||
| } catch (_e) { | ||
| // Continue without .env.sentry-build-plugin | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Loads the EAS build hooks module from the compiled output. | ||
| * @returns {object} The hooks module exports | ||
| * @throws {Error} If the module cannot be loaded | ||
| */ | ||
| function loadHooksModule() { | ||
| try { | ||
| return require('../../dist/js/tools/easBuildHooks.js'); | ||
| } catch (_e) { | ||
| console.error('[Sentry] Could not load EAS build hooks module. Make sure @sentry/react-native is properly installed.'); | ||
| process.exit(1); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Parses common options from environment variables. | ||
| * @returns {object} Parsed options object | ||
| */ | ||
| function parseBaseOptions() { | ||
| const options = { | ||
| dsn: process.env.SENTRY_DSN, | ||
| }; | ||
|
|
||
| // Parse additional tags if provided | ||
| if (process.env.SENTRY_EAS_BUILD_TAGS) { | ||
| try { | ||
| const parsed = JSON.parse(process.env.SENTRY_EAS_BUILD_TAGS); | ||
| if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) { | ||
| options.tags = parsed; | ||
| } else { | ||
| console.warn('[Sentry] SENTRY_EAS_BUILD_TAGS must be a JSON object (e.g., {"key":"value"}). Ignoring.'); | ||
| } | ||
| } catch (_e) { | ||
| console.warn('[Sentry] Could not parse SENTRY_EAS_BUILD_TAGS as JSON. Ignoring.'); | ||
| } | ||
| } | ||
|
cursor[bot] marked this conversation as resolved.
Outdated
|
||
|
|
||
| return options; | ||
| } | ||
|
|
||
| /** | ||
| * Wraps an async hook function with error handling. | ||
| * @param {string} hookName - Name of the hook for logging | ||
| * @param {Function} hookFn - Async function to execute | ||
| */ | ||
| async function runHook(hookName, hookFn) { | ||
| try { | ||
| await hookFn(); | ||
| console.log(`[Sentry] EAS build ${hookName} hook completed.`); | ||
| } catch (error) { | ||
| console.error(`[Sentry] Error in eas-build-${hookName} hook:`, error); | ||
| // Don't fail the build hook itself | ||
| } | ||
| } | ||
|
|
||
| module.exports = { | ||
| loadEnv, | ||
| loadHooksModule, | ||
| parseBaseOptions, | ||
| runHook, | ||
| }; | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That all would require documentation changes; will make sure to handle them next.