Skip to content

Commit 15538b0

Browse files
committed
Merge branch 'master' into fix/instant-loading
2 parents a3b619f + 7e6f15b commit 15538b0

File tree

23 files changed

+537
-282
lines changed

23 files changed

+537
-282
lines changed

.github/workflows/build.yml

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ permissions:
3636
contents: read
3737

3838
jobs:
39-
npm:
39+
npm-build:
4040
runs-on: ubuntu-latest
4141
steps:
4242

@@ -59,15 +59,38 @@ jobs:
5959
if: steps.cache.outputs.cache-hit != 'true'
6060
run: npm install
6161

62-
- name: Check project
63-
run: npm run check
64-
6562
- name: Build project
6663
run: |
6764
npm run build
6865
git diff --name-only
6966
70-
pypi:
67+
npm-check:
68+
runs-on: ubuntu-latest
69+
steps:
70+
71+
- name: Checkout repository
72+
uses: actions/checkout@v4
73+
74+
- name: Set up Node.js runtime
75+
uses: actions/setup-node@v3
76+
with:
77+
node-version: ${{ env.NODE_VERSION }}
78+
79+
- name: Set up Node.js dependency cache
80+
uses: actions/cache@v3
81+
id: cache
82+
with:
83+
key: ${{ runner.os }}-${{ hashFiles('**/package-lock.json') }}
84+
path: node_modules
85+
86+
- name: Set up Node.js dependencies
87+
if: steps.cache.outputs.cache-hit != 'true'
88+
run: npm install
89+
90+
- name: Check project
91+
run: npm run check
92+
93+
python:
7194
runs-on: ubuntu-latest
7295
steps:
7396

@@ -78,6 +101,10 @@ jobs:
78101
uses: actions/setup-python@v4
79102
with:
80103
python-version: ${{ env.PYTHON_VERSION }}
104+
cache: pip
105+
cache-dependency-path: |
106+
pyproject.toml
107+
requirements.txt
81108
82109
- name: Set up Python dependencies
83110
run: pip install --upgrade build twine

.github/workflows/documentation.yml

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ env:
2929

3030
permissions:
3131
contents: write
32+
id-token: write
33+
pages: write
3234

3335
jobs:
3436
documentation:
@@ -40,20 +42,25 @@ jobs:
4042
uses: actions/checkout@v4
4143
with:
4244
fetch-depth: 0
45+
sparse-checkout: |
46+
docs
47+
includes
48+
material/overrides
49+
src/templates/partials/languages
4350
4451
- name: Set up Python runtime
4552
uses: actions/setup-python@v4
4653
with:
4754
python-version: ${{ env.PYTHON_VERSION }}
48-
49-
- name: Set the date environmental variable
50-
run: echo "cache_id=$(date --utc '+%V')" >> $GITHUB_ENV
55+
cache: pip
56+
cache-dependency-path: |
57+
pyproject.toml
58+
requirements.txt
5159
5260
- name: Set up build cache
53-
uses: actions/cache@v3
54-
id: cache
61+
uses: actions/cache/restore@v3
5562
with:
56-
key: mkdocs-material-${{ env.cache_id }}
63+
key: mkdocs-material-${{ hashfiles('.cache/**') }}
5764
path: .cache
5865
restore-keys: |
5966
mkdocs-material-
@@ -63,15 +70,8 @@ jobs:
6370

6471
- name: Install Python dependencies
6572
run: |
66-
pip install \
67-
"cairosvg>=2.5" \
68-
"mkdocs-git-committers-plugin-2>=1.1.1" \
69-
"mkdocs-git-revision-date-localized-plugin>=1.0" \
70-
"mkdocs-minify-plugin>=0.3" \
71-
"mkdocs-rss-plugin>=1.2" \
72-
"mkdocs-redirects>=1.0" \
73-
"lxml" \
74-
"pillow<10"
73+
pip install mkdocs-material
74+
pip install mkdocs-material[recommended,git,imaging]
7575
7676
- name: Install Insiders build
7777
if: github.event.repository.fork == false
@@ -88,10 +88,30 @@ jobs:
8888
rm -rf material
8989
cp -r mkdocs-material-insiders/material material
9090
91-
- name: Deploy documentation
91+
- name: Build documentation
9292
env:
9393
GH_TOKEN: ${{ secrets.GH_TOKEN }}
9494
GOOGLE_ANALYTICS_KEY: ${{ secrets.GOOGLE_ANALYTICS_KEY }}
9595
run: |
96-
mkdocs gh-deploy --force
96+
mkdocs build --clean
9797
mkdocs --version
98+
99+
- name: Adjust permissions
100+
run: |
101+
chmod -c -R +rX site/ | while read line; do
102+
echo "::warning title=Invalid file permissions automatically fixed::$line"
103+
done
104+
105+
- name: Upload to GitHub Pages
106+
uses: actions/upload-pages-artifact@v2
107+
with:
108+
path: site
109+
110+
- name: Deploy to GitHub Pages
111+
uses: actions/deploy-pages@v2
112+
113+
- name: Save build cache
114+
uses: actions/cache/save@v3
115+
with:
116+
key: mkdocs-material-${{ hashfiles('.cache/**') }}
117+
path: .cache
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
---
2+
date: 2023-09-22
3+
authors: [squidfunk]
4+
categories:
5+
- Build
6+
- Performance
7+
links:
8+
- publishing-your-site.md#with-github-actions
9+
- creating-your-site.md#building-your-site
10+
---
11+
12+
# Using `git sparse-checkout` for faster documentation builds
13+
14+
__Leveraging `git sparse-checkout` in GitHub Actions enabled us to speed up
15+
documentation builds in our repository, cutting checkout times from 20 to 30
16+
seconds to just 2 seconds.__
17+
18+
Developing an efficient approach to build documentation in CI workflows is
19+
essential, especially when working in large repositories with thousands of
20+
commits, like ours. Of course, we want to build documentation quickly and
21+
efficiently, ensuring fast and productive workflows. When using both the
22+
wonderful [`git-committers`][git-committers] and [`git-revision-date-localized`]
23+
[git-revision-date-localized] plugins to display [document contributors] and
24+
[dates] at the bottom of each page, we are required to set `fetch-depth: 0`,
25+
which resulted in checkout times of 20 to 30 seconds on our repository. By
26+
leveraging [`git sparse-checkout`][git sparse-checkout] within [GitHub Actions],
27+
check out time was brought down to 2 seconds.
28+
29+
[git sparse-checkout]: https://git-scm.com/docs/git-sparse-checkout
30+
[GitHub Actions]: ../../publishing-your-site.md#with-github-actions
31+
[git-revision-date-localized]: https://github.com/timvink/mkdocs-git-revision-date-localized-plugin
32+
[git-committers]: https://github.com/ojacques/mkdocs-git-committers-plugin-2
33+
[document contributors]: ../../setup/adding-a-git-repository.md#document-contributors
34+
[dates]: ../../setup/adding-a-git-repository.md#document-dates
35+
36+
<!-- more -->
37+
38+
## A Primer
39+
40+
[`git sparse-checkout`][git sparse-checkout] allows you to check out only a
41+
subset of the files in a repository, making it incredibly useful for large
42+
repositories where a full checkout takes long and includes many files that are
43+
not relevant when building documentation.
44+
45+
## GitHub Actions
46+
47+
To enable [`git sparse-checkout`][git sparse-checkout] within [GitHub Actions]
48+
and ensure that you are only building the documentation that you need, add the
49+
following lines to your workflow file:
50+
51+
``` yaml
52+
- uses: actions/checkout@v4
53+
with:
54+
fetch-depth: 0
55+
sparse-checkout: |
56+
docs
57+
includes
58+
```
59+
60+
[`git sparse-checkout`][git sparse-checkout] always checks out all files
61+
residing in the repository’s root. This means that regardless of the specified
62+
paths or directories for sparse checkout, the files located in the root of the
63+
repository will always be included in the checkout process.
64+
65+
Thus, you only need to specify the directories that are necessary for building
66+
documentation. In our case, we only need the `docs` and `includes` folders,
67+
but if you need additional directories, you can just add them to the end of the
68+
list. A complete example workflow for [GitHub Actions]:
69+
70+
``` yaml hl_lines="13-18"
71+
name: documentation
72+
on:
73+
push:
74+
branches:
75+
- master
76+
- main
77+
permissions:
78+
contents: write
79+
jobs:
80+
deploy:
81+
runs-on: ubuntu-latest
82+
steps:
83+
- uses: actions/checkout@v4
84+
with:
85+
fetch-depth: 0
86+
sparse-checkout: |
87+
docs
88+
includes
89+
- uses: actions/setup-python@v4
90+
with:
91+
python-version: 3.x
92+
- run: pip install mkdocs-material
93+
- run: mkdocs gh-deploy --force
94+
```
95+
96+
## Conclusion
97+
98+
That's all there is! We're super happy with the results and hope that this will
99+
help you to speed up your documentation builds in [GitHub Actions] as well. As
100+
always, feel free to share your thoughts and experiences in the comments below.

material/overrides/hooks/translations.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import os
2222
import re
2323

24-
from glob import glob
24+
from glob import iglob
2525
from mkdocs.config.defaults import MkDocsConfig
2626
from mkdocs.structure.pages import Page
2727
from urllib.parse import urlencode, urlparse
@@ -40,7 +40,7 @@ def on_page_markdown(markdown: str, *, page: Page, config: MkDocsConfig, files):
4040
# Collect all existing languages
4141
names: dict[str, str] = {}
4242
known: dict[str, dict[str, str]] = {}
43-
for path in glob("src/templates/partials/languages/*.html"):
43+
for path in iglob("src/templates/partials/languages/*.html"):
4444
with open(path, "r", encoding = "utf-8") as f:
4545
data = f.read()
4646

0 commit comments

Comments
 (0)