COMMIT_REF not available in snippets

answered
#1

Hi,

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:
<script>window.commitID='';</script>

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

Any ideas?

#2

After some investigation:

Environment variables NOT working in snippets

  • REPOSITORY_URL
  • PULL_REQUEST
  • HEAD
  • COMMIT_REF
  • REVIEW_ID

Environment variables working in snippets

  • BRANCH
  • CONTEXT
  • URL
  • DEPLOY_URL
  • DEPLOY_PRIME_URL

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

Any possibility of getting support for all variables in snippets?

#3

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