pozgo / docker-mkdocs

MkDocs in a Docker. (Alpine)
MIT License
86 stars 34 forks source link

Handling git safe directory in some way #67

Open jurgenhaas opened 1 year ago

jurgenhaas commented 1 year ago

Thanks a lot for making version 1.5.2 available, really much appreciated.

Now, I run into a new issue due to the latest git version contained in that docker image, which, of course, is a good thing. The problem is that git introduced the concept of safe directories, i.e. no git command can be executed unless the working directory is declared safe in the global git settings.

Now, in mkdocs there are some nice plugins that use the git logs of the current project to extract the date and author of each page. But they now fail due to the missing configuration on the host, i.e. the docker container. The plugins can't fix this themselves, it needs to be resolved before actually installing such plugins.

As a workaround I cloned this project and tested it by adding the following line into the dockerfile:

git config --global --add safe.directory /mkdocs

right after the apk add --update ... command. That solves the issue, but of course only because my working directory is /mkdocs. I'm aware, this won't work for other working directories. So, that command should ideally be executed as part of the command that executes when the container gets built when we know the working directory of that container.

Is that even possible?

pozgo commented 1 year ago

Good catch @jurgenhaas :D

We can have that command executed on runtime when the repo is specified by GIT_REPO as part of pull.

I mean this section in common.py

https://github.com/pozgo/docker-mkdocs/blob/1bc2937b6dfcd57995d7a0d443f2244033eb11bf/container-files/bootstrap/app/mkdocs/common.py#L109-L122

jurgenhaas commented 1 year ago

Hmm, I don'T think that the repo will always be cloned. In our workflow, we start the docker container in the context of the already cloned git repo. So, we don't even have the GIT_REPO env variable set. But it could be called as first thing in the start method:

def start():
    """
    Start mkdocs server
    :return:
    """
    if modules != 'false':
        _install_modules(modules)
    if repo != 'false':
        _clone_repo(repo)
    _check_previous_installation()
    print('Starting MKDocs')
    os.chdir(docks_dir)
    if "DEV_ADDR" in os.environ:
        _dev_addr = os.environ['DEV_ADDR']
    else:
        _dev_addr = '0.0.0.0:8000'
    os.system(f'mkdocs serve -a {_dev_addr} {_live_reload()} {_fast_mode()}')

But I don't know if the git Python package allows to set global configuration? I couldn't find anything, so we may have to execute the git command from within Python?

jurgenhaas commented 1 year ago

Ah, found it after locally installing GitPython:

    writer = git.Repo.config_writer(None, 'global')
    writer.set('safe', 'directory', docks_dir)

Not sure, if config_writer accepts None as the first argument though.

pozgo commented 1 year ago

good point with the start as not everyone will use the built-in GIT_REPO env. I'll try to add your example into the method