HUGO

Hugo enableGitInfo Git log: fatal: not a git repository error

Hi,

When I set enableGitInfo=true I received below error while building site inside docker…Can you please help how can I fix this issue ?

Failed to read Git log: fatal: not a git repository (or any of the parent directories): .git Total in 5012 ms

Step 15/22 : RUN if [ “$ENVIRONMENT” = “production” ] ; then hugo -e ${AUDIENCE} --minify --baseURL=“https://oneapidocs.mercedes-benz.com/${AUDIENCE}” ; else hugo -e ${AUDIENCE} --minify ; fi —> Running in 5a92419f7520 Start building sites … ERROR 2021/04/30 10:53:18 Failed to read Git log: fatal: not a git repository (or any of the parent directories): .git Total in 5012 ms Error: Error building site: logged 1 error(s) The command ‘/bin/sh -c if [ “$ENVIRONMENT” = “production” ] ; then hugo -e ${AUDIENCE} --minify --baseURL=“https://oneapidocs.mercedes-benz.com/${AUDIENCE}” ; else hugo -e ${AUDIENCE} --minify ; fi’ returned a non-zero code: 255

Here is my dockerfile

FROM alpine:latest as build
ENV HUGO_VERSION=0.78.0 \
HUGO_SITE=/src/hugo
RUN sed -i.bak 's+https://+http://+' /etc/apk/repositories
RUN apk --no-cache add \
curl \
git \
libc6-compat \
libstdc++ \
nodejs \
npm
WORKDIR ${HUGO_SITE}
COPY package.json ./
RUN npm install -D --save postcss
RUN npm install -D autoprefixer
RUN npm install -D --save postcss-cli
RUN curl -L [https://github.com/gohugoio/hugo/releases/download/v${HUGO_VERSION}/hugo_extended_${HUGO_VERSION}_Linux-64bit.tar.gz](https://github.com/gohugoio/hugo/releases/download/v$%7BHUGO_VERSION%7D/hugo_extended_$%7BHUGO_VERSION%7D_Linux-64bit.tar.gz) \
-o /tmp/hugo.tar.gz \
&& tar -xzf /tmp/hugo.tar.gz -C /tmp \
&& cp /tmp/hugo /usr/local/bin/ \
&& mkdir -p ${HUGO_SITE} \
&& rm -rf /tmp/*COPY . ${HUGO_SITE}
RUN if [ "$ENVIRONMENT" = "production" ] ; then hugo -e ${AUDIENCE} --minify --baseURL="[https://oneapidocs.mercedes-benz.com/${AUDIENCE}](https://oneapidocs.mercedes-benz.com/$%7BAUDIENCE%7D)" ; else hugo -e ${AUDIENCE} --minify ; fiFROM nginx:latest
COPY --from=build /src/hugo/public /usr/share/nginx/html
RUN rm /etc/nginx/conf.d/default.conf
COPY ./nginx/nginx.conf /etc/nginx/conf.d/default.conf
RUN chgrp -R root /var/cache/nginx /var/run /var/log/nginx && \
chmod -R 770 /var/cache/nginx /var/run /var/log/nginx
EXPOSE 8081
CMD ["nginx", "-g", "daemon off;"]

Few questions:

  • Is your site initialized as a git repo?
  • If yes, then does your docker image have git installed?

Hi @zwbetz ,

Correct… As I shared dockerfile in my question I installed git in docker as below. I tried to RUN git init but it did not work for me.

RUN apk --no-cache add \
curl \
git \

Let’s backup some. Was your site already in git before docker-izing it?
I ask because doing a git init may not be enough. There may need to be at least some history, say 1 or more commits.

@zwbetz ,
Correct ı had lots of commits… When ı run website locally I can able to see with hugo server -D… But when I dockerize it, I received above error while project is building.

In your Dockerfile, I don’t see where you’re copying over your full hugo site (which has the .git dir will all your git info).

If you’re only copying the public dir… then that is just the generated files.

@zwbetz

Here is folder structure… I tried to copy .git folder but it did not work :frowning:

 => ERROR [build  7/11] COPY ../.git/ ./.git/                                                                                                                                                                                           0.0s
------
 > [build  7/11] COPY ../.git/ ./.git/:
------
failed to compute cache key: "/.git" not found: not found

l$ ls -laf
total 48
drwxr-xr-x   7 semural  EMEA\Domain Users   224 May  4 15:09 .
drwxr-xr-x  22 semural  EMEA\Domain Users   704 May  4 10:03 ..
-rw-r--r--@  1 semural  EMEA\Domain Users  6148 May  4 15:06 .DS_Store
drwxr-xr-x  17 semural  EMEA\Domain Users   544 May  4 15:09 docu
-rw-r--r--   1 semural  EMEA\Domain Users  6055 May  4 13:52 README.md
-rw-r--r--   1 semural  EMEA\Domain Users  6083 May  4 10:06 Jenkinsfile
drwxr-xr-x  12 semural  EMEA\Domain Users   384 May  4 16:38 .git

It builds fine on my end. I would double check your setup.

What I did:

  1. Make new site: hugo new enable_git_info
  2. Change to it: cd enable_git_info
  3. Make homepage content: hugo new _index.md
  4. Edit homepage content by adding a title and content
  5. Make homepage layout: touch layouts/index.html
  6. Edit homepage layout by adding:
    <h1>{{ .Title }}</h1>
    {{ .Content }}
    
  7. Add enableGitInfo = true to config.toml
  8. Add docker ignore file: echo 'public/' > .dockerignore
  9. Add Dockerfile with contents:
    FROM alpine:latest
    WORKDIR /app
    RUN apk --no-cache add curl git libc6-compat libstdc++
    RUN curl -L -o /tmp/hugo.tar.gz https://github.com/gohugoio/hugo/releases/download/v0.83.1/hugo_extended_0.83.1_Linux-64bit.tar.gz \
      && tar xzf /tmp/hugo.tar.gz -C /usr/local/bin/
    COPY . .
    RUN hugo -D
    
  10. Init as git repo: git init
  11. Stage everything: git add .
  12. Commit everything: git commit -m "init"
  13. Build it: docker build -t enable_git_info .
  14. Run temp container: docker run -it --rm enable_git_info sh
  15. Confirm generated site: cat public/index.html

@zwbetz thanks for your awesome clarification. Let me clarify my setup step by step.

  1. With out dockerize, when I run hugo server -e employees in my local pc I can able to see the last modification date.

  1. When I dockerize my Hugo app and deploy to kubernetes I could not able to see Last Modified date.It looks like as below
    image

Here is my folder structure. The only differences against you my .git file is outside of docu site. But I copy and paste this file before building images , still did not worked for me.

Should I run git init inside of docu site?

 ls -a
.               ..    .git            Jenkinsfile     README.md       docu
    $ tree -d
    .
    └── docu
        ├── archetypes
        ├── assets
        ├── config
        │   ├── _default
        │   ├── dealers
        │   └── employees
        ├── content
        │   └── en
        │       ├── docs

Here is Dockerfile again

FROM alpine:latest AS build
ENV HUGO_VERSION=0.78.0 \
HUGO_SITE=/src/hugo
ARG AUDIENCE
ARG ENVIRONMENT
RUN sed -i.bak 's+https://+http://+' /etc/apk/repositories
RUN apk --no-cache add \
    curl \
    git \
    libc6-compat \
    libstdc++ \
    nodejs \
    npm
WORKDIR ${HUGO_SITE}
COPY package.json ./
COPY .git ./.git
RUN npm install -D --save postcss && \
    npm install -D autoprefixer && \
    npm install -D --save postcss-cli
RUN curl -L https://github.com/gohugoio/hugo/releases/download/v${HUGO_VERSION}/hugo_extended_${HUGO_VERSION}_Linux-64bit.tar.gz \
    -o /tmp/hugo.tar.gz \
    && tar -xzf /tmp/hugo.tar.gz -C /tmp \
    && cp /tmp/hugo /usr/local/bin/ \
    && mkdir -p ${HUGO_SITE} \
    && rm -rf /tmp/*
COPY . ${HUGO_SITE}
RUN if [ "$ENVIRONMENT" = "production" ] ; then hugo server -e ${AUDIENCE} --minify --enableGitInfo --baseURL="https://example.com/${AUDIENCE}" ; else hugo server -e ${AUDIENCE} --minify --enableGitInfo; fi

#Prepare nginx
FROM nginx:latest
COPY --from=build /src/hugo/public /usr/share/nginx/html
RUN rm /etc/nginx/conf.d/default.conf
COPY nginx/nginx.conf /etc/nginx/conf.d/default.conf
RUN chgrp -R root /var/cache/nginx /var/run /var/log/nginx && \
    chmod -R 770 /var/cache/nginx /var/run /var/log/nginx
EXPOSE 8081
CMD ["nginx", "-g", "daemon off;"]

No need to repost the Dockerfile. (We can scroll up).

Hmm, not sure on what your particular issue is. Maybe someone else can help.

I don’t think you need to run git init as your site is already a git repo.

@zwbetz

It worked now… Its totally related with .git folder should be inside the site