54

In development, I want to be able to see the build information (git commit hash, author, last commit message, etc) from the web. I have tried:

  • use child_process to execute a git command line, and read the result (Does not work because browser environment)
  • generate a buildInfo.txt file during npm build and read from the file (Does not work because fs is also unavailable in browser environment)
  • use external libraries such as "git-rev"

The only thing left to do seems to be doing npm run eject and applying https://www.npmjs.com/package/git-revision-webpack-plugin , but I really don't want to eject out of create-react-app. Anyone got any ideas?

NearHuscarl
  • 66,950
  • 18
  • 261
  • 230
Yifei Xu
  • 777
  • 1
  • 6
  • 14

7 Answers7

98

On a slight tangent (no need to eject and works in develop), this may be of help to other folk looking to add their current git commit SHA into their index.html as a meta-tag by adding:

REACT_APP_GIT_SHA=`git rev-parse --short HEAD`

to the build script in the package.json and then adding (note it MUST start with REACT_APP... or it will be ignored):

<meta name="ui-version" content="%REACT_APP_GIT_SHA%">

into the index.html in the public folder.

Within react components, do it like this:

<Component>{process.env.REACT_APP_GIT_SHA}</Component>
Mario Petrovic
  • 7,500
  • 14
  • 42
  • 62
uidevthing
  • 1,781
  • 2
  • 12
  • 24
  • I like it, even though it does not work on localhost when developing, only after building a production version. – Bugs Bunny Oct 16 '18 at 21:59
  • 8
    @BugsBunny Should work the same way. Just add the assignment to `start` script as well. `"start": "REACT_APP_GIT_SHA=\`git rev-parse --short HEAD\` react-scripts start"` – Bohdan Ganicky Nov 21 '18 at 10:18
  • @uidevthing how did you know to use backticks to get substitution working in a package.json script?? I just blew a couple of hours pulling my hair out trying to get this to work before stumbling on your solution. It worked perfectly, btw! – IAmKale Jan 19 '19 at 00:35
  • 9
    This is not a cross-platform solution. Will not work on Windows with either Powershell or cmd. – Abhyudaya Sharma Jan 17 '20 at 05:58
  • @AbhyudayaSharma [cross-env](https://www.npmjs.com/package/cross-env) could very easily replace the mentioned script – amrit Aug 12 '20 at 16:58
  • 1
    `cross-env` will not work for this without having to change your `npm` config; see here: https://github.com/kentcdodds/cross-env/issues/192#issuecomment-513341729 – TranquilMarmot Aug 30 '20 at 04:59
  • This syntax does not work with yarn 2, however the $() syntax does - see https://unix.stackexchange.com/questions/48392/understanding-backtick – rolznz Sep 27 '21 at 03:42
  • "REACT_APP_GIT_SHA=`git rev-parse --short HEAD` react-scripts start". How can I run this command in windows cmd? – Rohit Vyas Dec 22 '21 at 06:56
  • @RohitVyas not really sure what you mean exactly but if you type: git rev-parse --short HEAD into the CLI you will get that git commit SHA like "17c0b12". for "REACT_APP_GIT_SHA=git rev-parse --short HEAD I run it by assigning it to a npm script in the package.json file and for example I call it build So then in the CLI if I run npm build it will execute this script. Hope this is of some help, thanks. – uidevthing Dec 22 '21 at 11:52
  • I asked because this command is working in mac system but not working in windows cmd. – Rohit Vyas Dec 23 '21 at 09:44
15

I created another option inspired by Yifei Xu's response that utilizes es6 modules with create-react-app. This option creates a javascript file and imports it as a constant inside of the build files. While having it as a text file makes it easy to update, this option ensures it is a js file packaged into the javascript bundle. The name of this file is _git_commit.js

package.json scripts:

"git-info": "echo export default \"{\\\"logMessage\\\": \\\"$(git log -1 --oneline)\\\"}\"  > src/_git_commit.js",
"precommit": "lint-staged",
"start": "yarn git-info; react-scripts start",
"build": "yarn git-info; react-scripts build",

A sample component that consumes this commit message:

import React from 'react';

/**
 * This is the commit message of the last commit before building or running this project
 * @see ./package.json git-info for how to generate this commit
 */
import GitCommit from './_git_commit';

const VersionComponent = () => (
  <div>
    <h1>Git Log: {GitCommit.logMessage}</h1>
  </div>
);

export default VersionComponent;

Note that this will automatically put your commit message in the javascript bundle, so do ensure no secure information is ever entered into the commit message. I also add the created _git_commit.js to .gitignore so it's not checked in (and creates a crazy git commit loop).

11

It was impossible to be able to do this without ejecting until Create React App 2.0 (Release Notes) which brought with it automatic configuration of Babel Plugin Macros which run at compile time. To make the job simpler for everyone, I wrote one of those macros and published an NPM package that you can import to get git information into your React pages: https://www.npmjs.com/package/react-git-info

With it, you can do it like this:

import GitInfo from 'react-git-info/macro';

const gitInfo = GitInfo();

...

render() {
  return (
    <p>{gitInfo.commit.hash}</p>
  );
}

The project README has some more information. You can also see a live demo of the package working here.

Abhyudaya Sharma
  • 1,173
  • 12
  • 18
9

So, turns out there is no way to achieve this without ejecting, so the workaround I used is:

1) in package.json, define a script "git-info": "git log -1 --oneline > src/static/gitInfo.txt"

2) add npm run git-info for both start and build

3) In the config js file (or whenever you need the git info), i have

const data = require('static/gitInfo.txt')
fetch(data).then(result => {
    return result.text()
})
Yifei Xu
  • 777
  • 1
  • 6
  • 14
9

My approach is slightly different from @uidevthing's answer. I don't want to pollute package.json file with environment variable settings.

You simply have to run another script that save those environment variables into .env file at the project root. That's it.

In the example below, I'll use typescript but it should be trivial to convert to javascript anyway.

package.json

If you use javascript it's node scripts/start.js

  ...
  "start": "ts-node scripts/start.ts && react-scripts start",

scripts/start.ts

Create a new script file scripts/start.ts

const childProcess = require("child_process");
const fs = require("fs");

function writeToEnv(key: string = "", value: string = "") {
  const empty = key === "" && value === "";

  if (empty) {
    fs.writeFile(".env", "", () => {});
  } else {
    fs.appendFile(".env", `${key}='${value.trim()}'\n`, (err) => {
      if (err) console.log(err);
    });
  }
}

// reset .env file
writeToEnv();

childProcess.exec("git rev-parse --abbrev-ref HEAD", (err, stdout) => {
  writeToEnv("REACT_APP_GIT_BRANCH", stdout);
});
childProcess.exec("git rev-parse --short HEAD", (err, stdout) => {
  writeToEnv("REACT_APP_GIT_SHA", stdout);
});

// trick typescript to think it's a module
// https://stackoverflow.com/a/56577324/9449426
export {};

The code above will setup environment variables and save them to .env file at the root folder. They must start with REACT_APP_. React script then automatically reads .env at build time and then defines them in process.env.

App.tsx

...
console.log('REACT_APP_GIT_BRANCH', process.env.REACT_APP_GIT_BRANCH)
console.log('REACT_APP_GIT_SHA', process.env.REACT_APP_GIT_SHA)

Result

REACT_APP_GIT_BRANCH master
REACT_APP_GIT_SHA 042bbc6

More references:

NearHuscarl
  • 66,950
  • 18
  • 261
  • 230
2

If your package.json scripts are always executed in a unix environment you can achieve the same as in @NearHuscarl answer, but with fewer lines of code by initializing your .env dotenv file from a shell script. The generated .env is then picked up by the react-scripts in the subsequent step.

"scripts": {
  "start": "sh ./env.sh && react-scripts start"
  "build": "sh ./env.sh && react-scripts build",
}

where .env.sh is placed in your project root and contains code similar to the one below to override you .env file content on each build or start.

{
  echo BROWSER=none
  echo REACT_APP_FOO=bar
  echo REACT_APP_VERSION=$(git rev-parse --short HEAD)
  echo REACT_APP_APP_BUILD_DATE=$(date)
  # ...
} > .env

Since the .env is overridden on each build, you may consider putting it on the .gitignore list to avoid too much noise in your commit diffs.

Again the disclaimer: This solution only works for environments where a bourne shell interpreter or similar exists, i.e. unix.

Felix K.
  • 14,171
  • 9
  • 58
  • 72
0

You can easily inject your git information like commit hash into your index.html using CRACO and craco-interpolate-html-plugin. Such way you won't have to use yarn eject and it also works for development server environment along with production builds.

After installing CRACO the following config in craco.config.js worked for me:

const interpolateHtml = require('craco-interpolate-html-plugin');

module.exports = {
  plugins: [
    {
      plugin: interpolateHtml,
      // Enter the variable to be interpolated in the html file
      options: {
        BUILD_VERSION: require('child_process')
          .execSync('git rev-parse HEAD', { cwd: __dirname })
          .toString().trim(),
      },
    },
  ],
};

and in your index.html: <meta name="build-version" content="%BUILD_VERSION%" />

Here are the lines of code to add in package.json to make it all work:

"scripts": {
    "start": "craco start",
    "build": "craco build"
}
Kutalia
  • 519
  • 8
  • 10