Skip to content

Commit 1e211f6

Browse files
authored
Feature: Added support for git based documentation (#33)
1 parent 70a3bc6 commit 1e211f6

File tree

10 files changed

+142
-24
lines changed

10 files changed

+142
-24
lines changed

Dockerfile

+7-4
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@ ENV MKDOCS_VERSION=1.1.0 \
55
LIVE_RELOAD_SUPPORT='false' \
66
ADD_MODULES='false' \
77
FAST_MODE='false' \
8-
PYTHONUNBUFFERED=1
8+
PYTHONUNBUFFERED=1 \
9+
GIT_REPO='false' \
10+
GIT_BRANCH='master' \
11+
AUTO_UPDATE='false' \
12+
UPDATE_INTERVAL=15
913

10-
ADD bootstrap/ /bootstrap
14+
ADD container-files/ /
1115

1216
RUN \
1317
apk add --update \
@@ -20,8 +24,7 @@ RUN \
2024
pip3 install --upgrade pip && \
2125
pip install mkdocs==${MKDOCS_VERSION} && \
2226
cd /bootstrap && pip install -e /bootstrap && \
27+
apk del python3-dev && \
2328
rm -rf /tmp/* /var/tmp/* /var/cache/apk/* /var/cache/distfiles/*
2429

25-
WORKDIR ${DOCS_DIRECTORY}
26-
2730
CMD ["/usr/bin/bootstrap", "start"]

Pipfile.lock

+20
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

+44-13
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,26 @@ MkDocs is a fast, simple and downright gorgeous static site generator that's gea
1919

2020
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.
2121

22-
Workdir is set to `/mkdocs`
23-
24-
**This container requires `workdir` to be set on runtime either using simple `docker run -w /my_workdir` or with `docker-compose.yaml`**
25-
It have to match mounted directory path from `DOCS_DIRECTORY` environmental variable
26-
2722
```yaml
2823
version: '3'
2924

3025
services:
3126
mkdocs:
3227
container_name: mkdocs
33-
image: polinux/mkdocs:latest
28+
image: polinux/mkdocs:1.1.0
29+
restart: always
3430
ports:
3531
- "8000:8000"
32+
environment:
33+
LIVE_RELOAD_SUPPORT: 'true'
34+
ADD_MODULES: 'fontawesome-markdown mkdocs-git-revision-date-localized-plugin mkdocs-material'
35+
FAST_MODE: 'true'
36+
DOCS_DIRECTORY: '/mkdocs'
37+
GIT_REPO: '[email protected]:username/docs.git'
38+
UPDATE_INTERVAL: 15
39+
AUTO_UPDATE: 'true'
3640
volumes:
37-
- ${PWD}/mkdocs:/mkdocs
38-
working_dir: /mkdocs
41+
- $HOME/.ssh/id_rsa:/root/.ssh/id_rsa
3942
```
4043
4144
### Environmental Variables
@@ -46,6 +49,10 @@ services:
4649
|`ADD_MODULES`|List of module to install.|`false`|
4750
|`FAST_MODE`|Enable fast mode. Rebuilds only changed/added files|`false`|
4851
|`DOCS_DIRECTORY`|Directory in which documentation is mounted inside of container|`/mkdocs`|
52+
|`GIT_REPO`|Repository address. Will require ssh key for ssh connection. Example: `-v ${HOME}/.ssh/id_rsa:/root/.ssh/id_rsa`|`false`|
53+
|`GIT_BRANCH`|Self explanatory|`master`|
54+
|`AUTO_UPDATE`|Auto update for git repository support|`false`|
55+
|`UPDATE_INTERVAL`|Update interval in *minutes* - used only when `AUTO_UPDATE` set to `true`|every `15` minutes|
4956

5057
### Usage
5158

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

66+
Custom config with `git` repository as source of documentation
6067
```bash
6168
docker run \
6269
-ti \
@@ -65,12 +72,36 @@ docker run \
6572
-e "ADD_MODULES=mkdocs-bootstrap mkdocs-gitbook mkdocs-bootstrap4" \
6673
-e "LIVE_RELOAD_SUPPORT=true" \
6774
-e "FAST_MODE=true" \
68-
-e "DOCS_DIRECTORY=/workdir" \
69-
-v /my_docs_dir:/workdir \
75+
-e "DOCS_DIRECTORY=/my_docs" \
76+
-e "GIT_REPO=https://my_repos/custom-docs.git" \
77+
-e "GIT_BRANCH=develop" \
78+
-e "AUTO_UPDATE=true" \
79+
-e "UPDATE_INTERVAL=1" \
80+
-v ${HOME}/.ssh/id_rsa:/root/.ssh/id_rsa \
7081
polinux/mkdocs
7182
```
7283

73-
Docker Compose file contains default settings for deploying in local directory and it's set to bind port `8000` to localhost.
84+
See `docker-compose.yaml` for all compose options examples
85+
86+
### Update git repo based deployment
87+
Assuming you are using git repository as source of documentation there are two options available for updating the content of the docs.
88+
89+
#### Manual
90+
91+
Assuming that container name is `mkdocs`
92+
```bash
93+
docker exec -ti mkdocs bootstrap update
94+
Pulled branch: master
95+
Commit: a4000c525f6db977777bf758987c4df0b44f59b4
96+
Commit Message: Updated nodejs
97+
Date: 2020-03-24 18:52:43
98+
Author: Przemek Ozgo
99+
```
100+
101+
#### AUTO_UPDATE
102+
there are two environmental variables that can be used for AUTO UPDATE.
103+
See table above ^^
104+
74105

75106
### Build
76107

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

114145
## License
115-
[![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)
146+
[![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.

bootstrap/app/cli.py container-files/bootstrap/app/cli.py

+5
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,8 @@ def cli() -> None:
2323
@cli.command('start', help='Start Application')
2424
def start():
2525
common.start()
26+
27+
28+
@cli.command('update', help='Update documentation code from repository')
29+
def update():
30+
common.update_repo()

bootstrap/app/mkdocs/common.py container-files/bootstrap/app/mkdocs/common.py

+57-2
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,29 @@
11
import os
22
from termcolor import colored
3+
import git
4+
from datetime import datetime
5+
from crontab import CronTab
36

47
docks_dir = os.environ['DOCS_DIRECTORY']
8+
modules = os.environ['ADD_MODULES']
9+
repo = os.environ['GIT_REPO']
10+
git_branch = os.environ['GIT_BRANCH']
11+
auto_update = os.environ['AUTO_UPDATE']
12+
interval = int(os.environ['UPDATE_INTERVAL'])
513

614

715
def start():
816
"""
917
Start mkdocs server
1018
:return:
1119
"""
12-
_check_previous_installation()
13-
modules = os.environ['ADD_MODULES']
1420
if modules != 'false':
1521
_install_modules(modules)
22+
if repo != 'false':
23+
_clone_repo(repo)
24+
_check_previous_installation()
1625
print('Starting MKDocs')
26+
os.chdir(docks_dir)
1727
os.system(f'mkdocs serve -a 0.0.0.0:8000 {_live_reload()} {_fast_mode()}')
1828

1929

@@ -74,3 +84,48 @@ def _fast_mode():
7484
print(colored(f'FAST_MODE - [ ENABLED ]', 'green'))
7585
fast = '--dirtyreload'
7686
return fast
87+
88+
89+
def _set_auto_update(interval):
90+
"""
91+
Creates cron job for auto updating repository
92+
:param interval: (every x minutes)
93+
:return:
94+
"""
95+
os.system(f'crond')
96+
cron = CronTab(user='root')
97+
cron.remove_all()
98+
job = cron.new(command='bootstrap update', comment='update')
99+
job.minute.every(interval)
100+
cron.write()
101+
102+
103+
def _clone_repo(repo):
104+
"""
105+
Clone Documentation Code from git repository
106+
:return:
107+
"""
108+
if not os.path.exists(docks_dir + '/mkdocs.yml'):
109+
print(colored(f'Getting documentation from: {repo}', 'green'))
110+
git.Repo.clone_from(repo, docks_dir, branch=git_branch)
111+
112+
if auto_update == 'true':
113+
print(colored(f'AUTO_UPDATE - [ ENABLED ]', 'green'))
114+
print(colored(f'UPDATE_INTERVAL set to every {interval} minute/s', 'green'))
115+
_set_auto_update(interval)
116+
117+
118+
def update_repo():
119+
"""
120+
Fetching latest changes
121+
:return:
122+
"""
123+
repo = git.Repo(docks_dir)
124+
for remote in repo.remotes:
125+
remote.fetch()
126+
remote.pull()
127+
headcommit = repo.head.commit
128+
commit_date = datetime.fromtimestamp(headcommit.authored_date)
129+
print(colored(
130+
f'Pulled branch: {git_branch} \nCommit: {headcommit.hexsha} \nCommit Message: {headcommit.message}Date: {commit_date} \nAuthor: {headcommit.committer.name}',
131+
'green'))
File renamed without changes.

bootstrap/setup.py container-files/bootstrap/setup.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
setup(
44
name='bootstrap',
5-
version='0.0.1',
5+
version='0.0.2',
66
py_modules=['bootstrap'],
77
include_package_data=True,
88
install_requires=[
9-
'click', 'termcolor',
9+
'click', 'termcolor', 'GitPython', 'python-crontab'
1010
],
1111
entry_points='''
1212
[console_scripts]

container-files/root/.ssh/config

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Host *
2+
StrictHostKeyChecking no

docker-compose.yaml

+5-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ version: '3'
33
services:
44
mkdocs:
55
container_name: mkdocs
6-
image: polinux/mkdocs:latest
6+
image: polinux/mkdocs:1.1.0
77
restart: always
88
ports:
99
- "8000:8000"
@@ -12,6 +12,8 @@ services:
1212
ADD_MODULES: 'fontawesome-markdown mkdocs-git-revision-date-localized-plugin mkdocs-material'
1313
FAST_MODE: 'true'
1414
DOCS_DIRECTORY: '/mkdocs'
15+
GIT_REPO: '[email protected]:username/docs.git'
16+
UPDATE_INTERVAL: 15
17+
AUTO_UPDATE: 'true'
1518
volumes:
16-
- ${PWD}/mkdocs:/mkdocs
17-
working_dir: /mkdocs
19+
- $HOME/.ssh/id_rsa:/root/.ssh/id_rsa

0 commit comments

Comments
 (0)