Skip to content

Commit

Permalink
Feature: Added support for git based documentation (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
pozgo authored Mar 24, 2020
1 parent 70a3bc6 commit 1e211f6
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 24 deletions.
11 changes: 7 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ ENV MKDOCS_VERSION=1.1.0 \
LIVE_RELOAD_SUPPORT='false' \
ADD_MODULES='false' \
FAST_MODE='false' \
PYTHONUNBUFFERED=1
PYTHONUNBUFFERED=1 \
GIT_REPO='false' \
GIT_BRANCH='master' \
AUTO_UPDATE='false' \
UPDATE_INTERVAL=15

ADD bootstrap/ /bootstrap
ADD container-files/ /

RUN \
apk add --update \
Expand All @@ -20,8 +24,7 @@ RUN \
pip3 install --upgrade pip && \
pip install mkdocs==${MKDOCS_VERSION} && \
cd /bootstrap && pip install -e /bootstrap && \
apk del python3-dev && \
rm -rf /tmp/* /var/tmp/* /var/cache/apk/* /var/cache/distfiles/*

WORKDIR ${DOCS_DIRECTORY}

CMD ["/usr/bin/bootstrap", "start"]
20 changes: 20 additions & 0 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 44 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,26 @@ MkDocs is a fast, simple and downright gorgeous static site generator that's gea

Purpose of this image was to simplify the process of deploying MkDocs. This image is based on Alpine Linux to minimize the size of the image.

Workdir is set to `/mkdocs`

**This container requires `workdir` to be set on runtime either using simple `docker run -w /my_workdir` or with `docker-compose.yaml`**
It have to match mounted directory path from `DOCS_DIRECTORY` environmental variable

```yaml
version: '3'

services:
mkdocs:
container_name: mkdocs
image: polinux/mkdocs:latest
image: polinux/mkdocs:1.1.0
restart: always
ports:
- "8000:8000"
environment:
LIVE_RELOAD_SUPPORT: 'true'
ADD_MODULES: 'fontawesome-markdown mkdocs-git-revision-date-localized-plugin mkdocs-material'
FAST_MODE: 'true'
DOCS_DIRECTORY: '/mkdocs'
GIT_REPO: 'git@github.com:username/docs.git'
UPDATE_INTERVAL: 15
AUTO_UPDATE: 'true'
volumes:
- ${PWD}/mkdocs:/mkdocs
working_dir: /mkdocs
- $HOME/.ssh/id_rsa:/root/.ssh/id_rsa
```
### Environmental Variables
Expand All @@ -46,6 +49,10 @@ services:
|`ADD_MODULES`|List of module to install.|`false`|
|`FAST_MODE`|Enable fast mode. Rebuilds only changed/added files|`false`|
|`DOCS_DIRECTORY`|Directory in which documentation is mounted inside of container|`/mkdocs`|
|`GIT_REPO`|Repository address. Will require ssh key for ssh connection. Example: `-v ${HOME}/.ssh/id_rsa:/root/.ssh/id_rsa`|`false`|
|`GIT_BRANCH`|Self explanatory|`master`|
|`AUTO_UPDATE`|Auto update for git repository support|`false`|
|`UPDATE_INTERVAL`|Update interval in *minutes* - used only when `AUTO_UPDATE` set to `true`|every `15` minutes|

### Usage

Expand All @@ -55,8 +62,8 @@ docker run \
--name mkdocs \
polinux/mkdocs
```
Mount Volume into working directory and make it available on port `80` on `localhost` and additional themes.

Custom config with `git` repository as source of documentation
```bash
docker run \
-ti \
Expand All @@ -65,12 +72,36 @@ docker run \
-e "ADD_MODULES=mkdocs-bootstrap mkdocs-gitbook mkdocs-bootstrap4" \
-e "LIVE_RELOAD_SUPPORT=true" \
-e "FAST_MODE=true" \
-e "DOCS_DIRECTORY=/workdir" \
-v /my_docs_dir:/workdir \
-e "DOCS_DIRECTORY=/my_docs" \
-e "GIT_REPO=https://my_repos/custom-docs.git" \
-e "GIT_BRANCH=develop" \
-e "AUTO_UPDATE=true" \
-e "UPDATE_INTERVAL=1" \
-v ${HOME}/.ssh/id_rsa:/root/.ssh/id_rsa \
polinux/mkdocs
```

Docker Compose file contains default settings for deploying in local directory and it's set to bind port `8000` to localhost.
See `docker-compose.yaml` for all compose options examples

### Update git repo based deployment
Assuming you are using git repository as source of documentation there are two options available for updating the content of the docs.

#### Manual

Assuming that container name is `mkdocs`
```bash
docker exec -ti mkdocs bootstrap update
Pulled branch: master
Commit: a4000c525f6db977777bf758987c4df0b44f59b4
Commit Message: Updated nodejs
Date: 2020-03-24 18:52:43
Author: Przemek Ozgo
```

#### AUTO_UPDATE
there are two environmental variables that can be used for AUTO UPDATE.
See table above ^^


### Build

Expand Down Expand Up @@ -112,4 +143,4 @@ docker history --no-trunc=true polinux/mkdocs | tr -s ' ' | tail -n+2 | awk -F "
Przemyslaw Ozgo

## License
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fpozgo%2Fdocker-mkdocs.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2Fpozgo%2Fdocker-mkdocs?ref=badge_large)
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fpozgo%2Fdocker-mkdocs.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2Fpozgo%2Fdocker-mkdocs?ref=badge_large)
File renamed without changes.
5 changes: 5 additions & 0 deletions bootstrap/app/cli.py → container-files/bootstrap/app/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,8 @@ def cli() -> None:
@cli.command('start', help='Start Application')
def start():
common.start()


@cli.command('update', help='Update documentation code from repository')
def update():
common.update_repo()
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
import os
from termcolor import colored
import git
from datetime import datetime
from crontab import CronTab

docks_dir = os.environ['DOCS_DIRECTORY']
modules = os.environ['ADD_MODULES']
repo = os.environ['GIT_REPO']
git_branch = os.environ['GIT_BRANCH']
auto_update = os.environ['AUTO_UPDATE']
interval = int(os.environ['UPDATE_INTERVAL'])


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


Expand Down Expand Up @@ -74,3 +84,48 @@ def _fast_mode():
print(colored(f'FAST_MODE - [ ENABLED ]', 'green'))
fast = '--dirtyreload'
return fast


def _set_auto_update(interval):
"""
Creates cron job for auto updating repository
:param interval: (every x minutes)
:return:
"""
os.system(f'crond')
cron = CronTab(user='root')
cron.remove_all()
job = cron.new(command='bootstrap update', comment='update')
job.minute.every(interval)
cron.write()


def _clone_repo(repo):
"""
Clone Documentation Code from git repository
:return:
"""
if not os.path.exists(docks_dir + '/mkdocs.yml'):
print(colored(f'Getting documentation from: {repo}', 'green'))
git.Repo.clone_from(repo, docks_dir, branch=git_branch)

if auto_update == 'true':
print(colored(f'AUTO_UPDATE - [ ENABLED ]', 'green'))
print(colored(f'UPDATE_INTERVAL set to every {interval} minute/s', 'green'))
_set_auto_update(interval)


def update_repo():
"""
Fetching latest changes
:return:
"""
repo = git.Repo(docks_dir)
for remote in repo.remotes:
remote.fetch()
remote.pull()
headcommit = repo.head.commit
commit_date = datetime.fromtimestamp(headcommit.authored_date)
print(colored(
f'Pulled branch: {git_branch} \nCommit: {headcommit.hexsha} \nCommit Message: {headcommit.message}Date: {commit_date} \nAuthor: {headcommit.committer.name}',
'green'))
File renamed without changes.
4 changes: 2 additions & 2 deletions bootstrap/setup.py → container-files/bootstrap/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

setup(
name='bootstrap',
version='0.0.1',
version='0.0.2',
py_modules=['bootstrap'],
include_package_data=True,
install_requires=[
'click', 'termcolor',
'click', 'termcolor', 'GitPython', 'python-crontab'
],
entry_points='''
[console_scripts]
Expand Down
2 changes: 2 additions & 0 deletions container-files/root/.ssh/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Host *
StrictHostKeyChecking no
8 changes: 5 additions & 3 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version: '3'
services:
mkdocs:
container_name: mkdocs
image: polinux/mkdocs:latest
image: polinux/mkdocs:1.1.0
restart: always
ports:
- "8000:8000"
Expand All @@ -12,6 +12,8 @@ services:
ADD_MODULES: 'fontawesome-markdown mkdocs-git-revision-date-localized-plugin mkdocs-material'
FAST_MODE: 'true'
DOCS_DIRECTORY: '/mkdocs'
GIT_REPO: 'git@github.com:username/docs.git'
UPDATE_INTERVAL: 15
AUTO_UPDATE: 'true'
volumes:
- ${PWD}/mkdocs:/mkdocs
working_dir: /mkdocs
- $HOME/.ssh/id_rsa:/root/.ssh/id_rsa

0 comments on commit 1e211f6

Please sign in to comment.