Docker build: `fatal: not a git repository (or any of the parent directories): .git`

1.8k views Asked by At

Trying to implement git info as part of a create-react-app where the .git directory is in my profject folder above the client directory:

[project]
 - .git
 - [client]
 - [server]

My start and build commands:

"start": "REACT_APP_GIT_SHORTHASH=`git log -1 --pretty=%h` REACT_APP_BUILD_TIMESTAMP=`date '+%F_%H:%M:%S'` react-scripts start",
"build": "REACT_APP_GIT_SHORTHASH=`git log -1 --pretty=%h` REACT_APP_BUILD_TIMESTAMP=`date '+%F_%H:%M:%S'` react-scripts build",

Locally I am able to issue these commands no problem while I'm in the client directory, however when attempting to deploy with docker using Github Actions I'm getting the following:

> [email protected] build
> REACT_APP_GIT_SHORTHASH=`git log -1 --pretty=%h` REACT_APP_BUILD_TIMESTAMP=`date '+%F_%H:%M:%S'` react-scripts build

fatal: not a git repository (or any of the parent directories): .git 

client Dockerfile

FROM tarampampam/node:17-alpine

ARG VERSION
ENV VERSION $VERSION
ARG BUILD_TIMESTAMP
ENV BUILD_TIMESTAMP $BUILD_TIMESTAMP


# Setting working directory. All the path will be relative to WORKDIR
WORKDIR /client

# Copying source files
COPY . .

# Installing dependencies 
RUN npm i -g npm@8 && npm i 

# Building app
RUN npm run build

EXPOSE 3000

# Running the app
CMD [ "npm", "run", "start" ]

Do I have to clone my git repo (private) and implement ssh just to issue this git command?

Am I missing something?

The REACT_APP_BUILD_TIMESTAMP is working flawlessly.

1

There are 1 answers

1
Gaëtan Boyals On

The COPY . . instruction will NOT copy dot folders/files at the root level (/foo/.bar will be copied, but not .foo).

Just add COPY .git . and it'll work.