COMMIT_REF not available in snippets


New to Netlify but loving it so far. I would like to include the COMMIT_REF environment variable in a post processing snippet but it doesn’t seem to be working.

My snippet HTML looks like this:
<script>window.commitID='{{ COMMIT_REF }}';</script>

After deployment, I see the following in my page:

But if I replace COMMIT_REF by BRANCH, it works as expected (inserting ‘master’ into the commitID)

Any ideas?

After some investigation:

Environment variables NOT working in snippets

  • HEAD

Environment variables working in snippets

  • URL

(I didn’t test the Incoming Webhook related variables)

Any possibility of getting support for all variables in snippets?

I’m not entirely surprised to hear that, Kelle.

Not sure if it’s a bug or intended design, but there is a workaround.

Since those are all available during build, you could set a global variable on your page to hold the value (a very naive example would be a build command of echo "<script>var commitref=\"$COMMIT_REF\";" > index.html ; obviously you’ll do something more clever with process.env or let your framework help interpolate things :slight_smile: )

Then, your snippet could refer to that variable that you’ve pre-set.

1 Like