Getting a build numer/version available in process.env automatically?

Is there anyway to get a build number into your process.env variable so it can be used in things like sentry error logging etc?

I could of course manually add an environment variable but it’s kind of a pain to increment that manually every release

Sure is! Check out the provided environment variables here:

You can see the $BUILD_ID which may work for you? Also available is $DEPLOY_ID which will match the last part of the URL for the webpage with the build logs. Finally, you also have $COMMIT_REF which may be even more useful, since it’s your id rather than ours :slight_smile:

amazing thank you! Exactly what I needed.

I have a create-react-app deploying via netlify, so to access these its just process.env.COMMIT_REF ? Nothing in particular I have to do?

well, that will work only if you execute it during build, it in javascript, but yes, that is a pattern that can work in that context.

You may need this advice as well if you are trying to access those variables at BROWSE time, which won’t work using that process:

Got it.

So I want to tweak my build script a little to look like this

npm run build && echo $CONTEXT > public/netlify-context.txt

Then include a netlify-context.txt in my public folder?

What does my netlify-context.txt file look like in order to allow me to reference

process.env.COMMIT_REF

in my JS code as a user is on tehre?

For context, I’m trying to pass in the COMMIT_REF to sentry.io to log errors along with whatever version they are from… I need to have it available when I initialize the sentry package (which of course initializes anytime somebody downloads the website to their browser)

npm run build && echo $CONTEXT > public/netlify-context.txt

You can do this successfully, yes!

However, I think since you’re using react and this is not sensitive data, a better/easier pattern would be this:

export REACT_APP_NETLIFY_CONTEXT=$CONTEXT && npm run build

This will “bake in” the $REACT_APP_NETLIFY_CONTEXT so you CAN use it at browse time directly with process.env.REACT_APP_NETLIFY_CONTEXT in your javascript! (saves you a call to fetch the file contents that your pages would all potentially have to make - even though it’s a small file, it takes non-zero time to request and retrieve).

More details on that pattern (will ONLY work for react apps) here: Adding Custom Environment Variables | Create React App

This was what I was looking for thanks.
Also a headsup to those trying to integrate sentry to netlify with the plugin.
IF you want to use BUILD_ID as the releaseVersionID.
you need to have in it

*Client code when you init Sentry function

*Set the same to SENTRY_RELEASE

What I found works if to add SENTRY_RELEASE=$BUILD_ID REACT_APP_BUILD_ID=$BUILD_ID
This makes sure Sentry is informed of the deploy under unique build ID and matching buildId is sent by the client SDK when it is actually published

2 Likes

Thanks so much for coming back and sharing this! We appreciate it, and it will definitely help future forums members who encounter something similar.

I am still facing an issue.
Let me break it down for you

Is the plugin I use to inform sentry whenever a deploy occurs . The plugin also uploads source map files to sentry . Now Sentry excepts an unique ID when it’s informed about a deploy and to correctly tag the sourcemap files. later the compiled React application Code when running in the browser needs to send errors to sentry with the same Identifier. This is how sentry can map erros with their correct build version and help resolve stacktraces with the correct sourcemaps

Now here is my issue .
One of the Environmental variables the plugin expects is SENTRY_RELEASE .
I want to use netlify’s $BUILD_ID to set this .
How can I set an env variable in netfliy to use a generated value like $BUILD_ID ??

I also need to pass the same $BUILD_ID to react by prefixing with REACT_APP_SENTRY_RELEASE (React code inside the application can only access env variables prefixed with REACT_APP)(This I have done succesfully)

My approach is here
$ echo $BUILD_ID && && export SENTRY_RELEASE=$BUILD_ID && export REACT_APP_BUILD_ID=$BUILD_ID && npm run build

React is able to read the Netlify Build ID from REACT_APP_BUILD_ID :+1:
but the Sentry plugin that runs in @sentry/netlify-build-plugin (onPostBuild event)
IS NOT ABLE TO SEE SENTRY_RELEASE value that I set :slightly_frowning_face:.
How do I know this ?
the plugin prints all the Envs while running . The other static ones like my sentry_project which I have set from netlify ENV UI Tab are set correctly .

Hey there, @sainathsr :wave:

Thanks so much for following up, apologies you are still encountering and error. Can you please share your URL to your site as well as your most recent full deploy log so that we can look into this further?