Skip to content

Commit cec1600

Browse files
Massimiliano Pippimasci
Massimiliano Pippi
authored andcommitted
backport versioned docs
1 parent ec5c3ed commit cec1600

File tree

4 files changed

+163
-8
lines changed

4 files changed

+163
-8
lines changed

.github/workflows/docs.yaml

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,20 @@ on:
1111
- 'cli/**'
1212
# potential changes to gRPC documentation
1313
- 'rpc/**'
14+
# changes to the workflow itself
15+
- '.github/workflows/docs.yaml'
1416
push:
1517
branches:
1618
- master
19+
# release branches have names like 0.8.x, 0.9.x, ...
20+
- '[0-9]+.[0-9]+.x'
1721
# At this day, GitHub doesn't support YAML anchors, d'oh!
1822
paths:
1923
- 'docs/**'
2024
- 'docsgen/**'
2125
- 'cli/**'
2226
- 'rpc/**'
27+
- '.github/workflows/docs.yaml'
2328

2429
jobs:
2530
build:
@@ -69,12 +74,18 @@ jobs:
6974
python3 -m pip install -r ./requirements_docs.txt
7075
7176
- name: Build docs website
77+
# This runs on every PR to ensure the docs build is sane, these docs
78+
# won't be published
79+
if: github.event_name == 'pull_request'
7280
run: task docs:build
7381

74-
- name: Deploy
75-
# publish docs only when PR is merged on master
82+
- name: Publish docs
83+
# Determine docs version for the commit pushed and publish accordingly using Mike.
84+
# Publishing implies creating a git commit on the gh-pages branch, we let
85+
# ArduinoBot own these commits.
7686
if: github.event_name == 'push'
77-
uses: peaceiris/actions-gh-pages@v3
78-
with:
79-
github_token: ${{ secrets.GITHUB_TOKEN }}
80-
publish_dir: ./public
87+
run: |
88+
git config --global user.email "bot@arduino.cc"
89+
git config --global user.name "ArduinoBot"
90+
git fetch --no-tags --prune --depth=1 origin +refs/heads/gh-pages:refs/remotes/origin/gh-pages
91+
python docs/build.py

Taskfile.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ tasks:
3434
cmds:
3535
- mkdocs build -s
3636

37+
docs:publish:
38+
desc: Use Mike to build and push versioned docs
39+
deps:
40+
- docs:gen:commands
41+
- docs:gen:protobuf
42+
cmds:
43+
- mike deploy -p -r {{.DOCS_REMOTE}} {{.DOCS_VERSION}} {{.DOCS_ALIAS}}
44+
3745
docs:serve:
3846
desc: Run documentation website locally
3947
deps:
@@ -126,3 +134,7 @@ vars:
126134
GOLINTBIN:
127135
sh: go list -f {{"{{"}}".Target{{"}}"}}" golang.org/x/lint/golint
128136
GOLINTFLAGS: "-min_confidence 0.8 -set_exit_status"
137+
# docs versioning
138+
DOCS_VERSION: dev
139+
DOCS_ALIAS: ""
140+
DOCS_REMOTE: "origin"

docs/build.py

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# This file is part of arduino-cli.
2+
3+
# Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
4+
5+
# This software is released under the GNU General Public License version 3,
6+
# which covers the main part of arduino-cli.
7+
# The terms of this license can be found at:
8+
# https://www.gnu.org/licenses/gpl-3.0.en.html
9+
10+
# You can be released from the requirements of the above licenses by purchasing
11+
# a commercial license. Buying such a license is mandatory if you want to
12+
# modify or otherwise use the software for commercial activities involving the
13+
# Arduino software without disclosing the source code of your own applications.
14+
# To purchase a commercial license, send an email to license@arduino.cc.
15+
import os
16+
import sys
17+
import re
18+
import unittest
19+
import subprocess
20+
21+
import click
22+
from git import Repo
23+
24+
25+
DEV_BRANCHES = ["master"]
26+
27+
28+
class TestScript(unittest.TestCase):
29+
def test_get_docs_version(self):
30+
ver, alias = get_docs_version("master", [])
31+
self.assertEqual(ver, "dev")
32+
self.assertEqual(alias, "")
33+
34+
release_names = ["1.4.x", "0.13.x"]
35+
ver, alias = get_docs_version("0.13.x", release_names)
36+
self.assertEqual(ver, "0.13")
37+
self.assertEqual(alias, "")
38+
ver, alias = get_docs_version("1.4.x", release_names)
39+
self.assertEqual(ver, "1.4")
40+
self.assertEqual(alias, "latest")
41+
42+
ver, alias = get_docs_version("0.1.x", [])
43+
self.assertIsNone(ver)
44+
self.assertIsNone(alias)
45+
46+
47+
def get_docs_version(ref_name, release_branches):
48+
if ref_name in DEV_BRANCHES:
49+
return "dev", ""
50+
51+
if ref_name in release_branches:
52+
# if version is latest, add an alias
53+
alias = "latest" if ref_name == release_branches[0] else ""
54+
# strip `.x` suffix from the branch name to get the version: 0.3.x -> 0.3
55+
return ref_name[:-2], alias
56+
57+
return None, None
58+
59+
60+
def get_rel_branch_names(blist):
61+
"""Get the names of the release branches, sorted from newest to older.
62+
63+
Only process remote refs so we're sure to get all of them and clean up the
64+
name so that we have a list of strings like 0.6.x, 0.7.x, ...
65+
"""
66+
pattern = re.compile(r"origin/(\d+\.\d+\.x)")
67+
names = []
68+
for b in blist:
69+
res = pattern.search(b.name)
70+
if res is not None:
71+
names.append(res.group(1))
72+
73+
# Since sorting is stable, first sort by major...
74+
names = sorted(names, key=lambda x: int(x.split(".")[0]), reverse=True)
75+
# ...then by minor
76+
return sorted(names, key=lambda x: int(x.split(".")[1]), reverse=True)
77+
78+
79+
@click.command()
80+
@click.option("--test", is_flag=True)
81+
@click.option("--dry", is_flag=True)
82+
@click.option("--remote", default="origin", help="The git remote where to push.")
83+
def main(test, dry, remote):
84+
# Run tests if requested
85+
if test:
86+
unittest.main(argv=[""], exit=False)
87+
sys.exit(0)
88+
89+
# Detect repo root folder
90+
here = os.path.dirname(os.path.realpath(__file__))
91+
repo_dir = os.path.join(here, "..")
92+
93+
# Get current repo
94+
repo = Repo(repo_dir)
95+
96+
# Get the list of release branch names
97+
rel_br_names = get_rel_branch_names(repo.refs)
98+
99+
# Deduce docs version from current branch. Use the 'latest' alias if
100+
# version is the most recent
101+
docs_version, alias = get_docs_version(repo.active_branch.name, rel_br_names)
102+
if docs_version is None:
103+
print(
104+
f"Can't get version from current branch '{repo.active_branch}', skip docs generation"
105+
)
106+
return 0
107+
108+
# Taskfile args aren't regular args so we put everything in one string
109+
cmd = (
110+
f"task docs:publish DOCS_REMOTE={remote} DOCS_VERSION={docs_version} DOCS_ALIAS={alias}",
111+
)
112+
113+
if dry:
114+
print(cmd)
115+
return 0
116+
117+
subprocess.run(cmd, shell=True, check=True, cwd=repo_dir)
118+
119+
120+
# Usage:
121+
#
122+
# To run the tests:
123+
# $python build.py test
124+
#
125+
# To run the script (must be run from within the repo tree):
126+
# $python build.py
127+
#
128+
if __name__ == "__main__":
129+
sys.exit(main())

requirements_docs.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1-
mkdocs
2-
mkdocs-material
1+
mkdocs<1.2
2+
mkdocs-material<5
3+
mike
4+
gitpython
5+
click<7.2

0 commit comments

Comments
 (0)