Search code examples
gitvue.jsgithubnuxt.jsnetlify

Nuxt access git version or hash at build time and use in page


I have a Nuxt/Vue app that I am deploying through Netlify. I'm trying to inject the git version number and hash into the body of the page. I'm assuming there is a pretty straightforward way to do this (but I don't know what it is!).


Solution

  • In package.json, you can pass the commit hash before call Nuxt:

    "scripts": {
      "dev": "NUXT_ENV_CURRENT_GIT_SHA=`git rev-parse --short HEAD` nuxt",
      ...
    },
    

    Then, you can recover the value anywhere with process.env.NUXT_ENV_CURRENT_GIT_SHA.