|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * EAS Build Hook: on-complete |
| 4 | + * |
| 5 | + * This script captures EAS build completion events and reports them to Sentry. |
| 6 | + * It uses the EAS_BUILD_STATUS environment variable to determine whether |
| 7 | + * the build succeeded or failed. |
| 8 | + * |
| 9 | + * Add it to your package.json scripts: |
| 10 | + * |
| 11 | + * "eas-build-on-complete": "sentry-eas-build-on-complete" |
| 12 | + * |
| 13 | + * Required environment variables: |
| 14 | + * - SENTRY_DSN: Your Sentry DSN |
| 15 | + * |
| 16 | + * Optional environment variables: |
| 17 | + * - SENTRY_EAS_BUILD_CAPTURE_SUCCESS: Set to 'true' to also capture successful builds |
| 18 | + * - SENTRY_EAS_BUILD_TAGS: JSON string of additional tags |
| 19 | + * - SENTRY_EAS_BUILD_ERROR_MESSAGE: Custom error message for failed builds |
| 20 | + * - SENTRY_EAS_BUILD_SUCCESS_MESSAGE: Custom success message for successful builds |
| 21 | + * |
| 22 | + * EAS Build provides: |
| 23 | + * - EAS_BUILD_STATUS: 'finished' or 'errored' |
| 24 | + * |
| 25 | + * @see https://docs.expo.dev/build-reference/npm-hooks/ |
| 26 | + * @see https://docs.sentry.io/platforms/react-native/ |
| 27 | + */ |
| 28 | + |
| 29 | +const path = require('path'); |
| 30 | +const fs = require('fs'); |
| 31 | + |
| 32 | +// Try to load environment variables |
| 33 | +function loadEnv() { |
| 34 | + // Try @expo/env first |
| 35 | + try { |
| 36 | + require('@expo/env').load('.'); |
| 37 | + } catch (_e) { |
| 38 | + // Fallback to dotenv if available |
| 39 | + try { |
| 40 | + const dotenvPath = path.join(process.cwd(), '.env'); |
| 41 | + if (fs.existsSync(dotenvPath)) { |
| 42 | + const dotenvFile = fs.readFileSync(dotenvPath, 'utf-8'); |
| 43 | + const dotenv = require('dotenv'); |
| 44 | + Object.assign(process.env, dotenv.parse(dotenvFile)); |
| 45 | + } |
| 46 | + } catch (_e2) { |
| 47 | + // No dotenv available, continue with existing env vars |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + // Also load .env.sentry-build-plugin if it exists |
| 52 | + try { |
| 53 | + const sentryEnvPath = path.join(process.cwd(), '.env.sentry-build-plugin'); |
| 54 | + if (fs.existsSync(sentryEnvPath)) { |
| 55 | + const dotenvFile = fs.readFileSync(sentryEnvPath, 'utf-8'); |
| 56 | + const dotenv = require('dotenv'); |
| 57 | + Object.assign(process.env, dotenv.parse(dotenvFile)); |
| 58 | + } |
| 59 | + } catch (_e) { |
| 60 | + // Continue without .env.sentry-build-plugin |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +async function main() { |
| 65 | + loadEnv(); |
| 66 | + |
| 67 | + // Dynamically import the hooks module (it's compiled to dist/) |
| 68 | + let captureEASBuildComplete; |
| 69 | + try { |
| 70 | + // Try the compiled output first |
| 71 | + const hooks = require('../dist/js/tools/easBuildHooks.js'); |
| 72 | + captureEASBuildComplete = hooks.captureEASBuildComplete; |
| 73 | + } catch (_e) { |
| 74 | + console.error('[Sentry] Could not load EAS build hooks module. Make sure @sentry/react-native is properly installed.'); |
| 75 | + process.exit(1); |
| 76 | + } |
| 77 | + |
| 78 | + // Parse options from environment variables |
| 79 | + const options = { |
| 80 | + dsn: process.env.SENTRY_DSN, |
| 81 | + errorMessage: process.env.SENTRY_EAS_BUILD_ERROR_MESSAGE, |
| 82 | + successMessage: process.env.SENTRY_EAS_BUILD_SUCCESS_MESSAGE, |
| 83 | + captureSuccessfulBuilds: process.env.SENTRY_EAS_BUILD_CAPTURE_SUCCESS === 'true', |
| 84 | + }; |
| 85 | + |
| 86 | + // Parse additional tags if provided |
| 87 | + if (process.env.SENTRY_EAS_BUILD_TAGS) { |
| 88 | + try { |
| 89 | + options.tags = JSON.parse(process.env.SENTRY_EAS_BUILD_TAGS); |
| 90 | + } catch (_e) { |
| 91 | + console.warn('[Sentry] Could not parse SENTRY_EAS_BUILD_TAGS as JSON. Ignoring.'); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + try { |
| 96 | + await captureEASBuildComplete(options); |
| 97 | + console.log('[Sentry] EAS build complete hook finished.'); |
| 98 | + } catch (error) { |
| 99 | + console.error('[Sentry] Error in eas-build-on-complete hook:', error); |
| 100 | + // Don't fail the build hook itself |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +main(); |
0 commit comments