Reading Env Variables During Deployment

So I am trying to get Sentry + Remix to work. Right now I am struggling to get environment variables to load. Here is my repo.

I have these variables set in my deployment:

But then when I build, the Sentry CLI doesn’t pick up the environment variable:

 => ERROR [build 6/6] RUN npm run build                                                                                                                                           6.6s
------                                                                                                                                                                                 
 > [build 6/6] RUN npm run build:                                                                                                                                                      
#18 0.574                                                                                                                                                                              
#18 0.574 > build                                                                                                                                                                      
#18 0.574 > run-s build:*                                                                                                                                                              
#18 0.574                                                                                                                                                                              
#18 1.041 
#18 1.041 > build:remix
#18 1.041 > remix build --sourcemap
#18 1.041 
#18 1.980 Building Remix app in production mode...
#18 1.980 
#18 1.980 ⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️
#18 1.980 You have enabled source maps in production. This will make your server-side code visible to the public and is highly discouraged! If you insist, please ensure you are using environment variables for secrets and not hard-coding them into your source!
#18 1.980 ⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️
#18 1.980 
#18 4.926 Built in 2.9s
#18 5.668 
#18 5.668 > build:server
#18 5.668 > tsx ./other/build-server.ts
#18 5.668 
#18 6.083 
#18 6.083 building...
#18 6.136 
#18 6.136   server-build/index.js  4.3kb
#18 6.136 
#18 6.136 ⚡ Done in 4ms
#18 6.514 
#18 6.514 > build:sentry
#18 6.514 > sentry-upload-sourcemaps
#18 6.514 
#18 6.595 node:internal/errors:867
#18 6.595   const err = new Error(message);
#18 6.595               ^
#18 6.595 
#18 6.595 Error: Command failed: /myapp/node_modules/@sentry/cli/sentry-cli releases new 072723aaad54a05109d88b2dba9ed10460fc3a35
#18 6.595 error: An organization slug is required (provide with --org)
#18 6.595 
#18 6.595 Add --log-level=[info|debug] or export SENTRY_LOG_LEVEL=[info|debug] to see more output.
#18 6.595 Please attach the full debug log to all bug reports.
#18 6.595 
#18 6.595     at ChildProcess.exithandler (node:child_process:419:12)
#18 6.595     at ChildProcess.emit (node:events:513:28)
#18 6.595     at maybeClose (node:internal/child_process:1091:16)
#18 6.595     at ChildProcess._handle.onexit (node:internal/child_process:302:5) {
#18 6.595   code: 1,
#18 6.595   killed: false,
#18 6.595   signal: null,
#18 6.595   cmd: '/myapp/node_modules/@sentry/cli/sentry-cli releases new 072723aaad54a05109d88b2dba9ed10460fc3a35'
#18 6.595 }
#18 6.595 
#18 6.595 Node.js v18.16.0
#18 6.606 ERROR: "build:sentry" exited with 1.

Any idea why I can’t get the CLI to pick up the environment variable? When I run the command on my machine (npm run build) it works just fine when I set the variables in my .env file.

Thanks for posting a link to your repository, it really helps!

Your .env file is listed in .dockerignore so it doesn’t get uploaded. I wouldn’t recommend changing that.

Secrets are not available at build time, and it looks like the build step is set up to run all scripts that start with build, so I would recommend changing the script name in package.json from build:sentry to something like upload:sentry.

Now to get the upload to run before your server is started, try adding the following to your fly.toml:

[deploy]
  release_command = "npm run upload:sentry"

@rubys Thanks for the quick reply. If I have a secret token that is needed for auth, how can I use it in a build step? The auth token is sensitive so I can have it in my .env or Fly secret store but I cannot put it in my fly.toml file.

EDIT: Oh I see…I make it not a build step. Thanks!