Skip to content

Commit b58b149

Browse files
committed
Use website versioning helper script only for generating parameters
Determining the correct versioning parameters for the versioned website system is relatively complex. For this reason, it's helpful to do this in a Python script, which provides a more capable framework for this code and its testing. Previously, the system worked like this: 1. Repository event triggers workflow. 2. Workflow executes helper script. 3. Helper script executes task. 4. Task executes mike. For me, this long chain of scripts, each in a different language, makes the system very difficult to understand and maintain. Although the helper script is useful for determining the versioning parameters, executing a task is well within the capabilities of a workflow. The process is run exclusively on GitHub Actions, with no reason to ever run it locally, to there is no harm to basing significant amounts of it in the workflow. So it's better for the helper script to have a single job and the workflow to act as the orchestrator of the tools.
1 parent b62dbc6 commit b58b149

File tree

5 files changed

+43
-44
lines changed

5 files changed

+43
-44
lines changed

workflow-templates/assets/deploy-mkdocs-versioned/build/build.py

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@
1313
# Arduino software without disclosing the source code of your own applications.
1414
# To purchase a commercial license, send an email to [email protected].
1515
import os
16-
import sys
1716
import re
18-
import subprocess
17+
import json
1918

20-
import click
2119
from git import Repo
2220

2321
# In order to provide support for multiple project releases, Documentation is versioned so that visitors can select
@@ -41,15 +39,15 @@
4139

4240
def get_docs_version(ref_name, release_branches):
4341
if ref_name in DEV_BRANCHES:
44-
return "dev", ""
42+
return {"version": "dev", "alias": ""}
4543

4644
if ref_name in release_branches:
4745
# if version is latest, add an alias
4846
alias = "latest" if ref_name == release_branches[0] else ""
4947
# strip `.x` suffix from the branch name to get the version: 0.3.x -> 0.3
50-
return ref_name[:-2], alias
48+
return {"version": ref_name[:-2], "alias": alias}
5149

52-
return None, None
50+
return {"version": None, "alias": None}
5351

5452

5553
def get_rel_branch_names(blist):
@@ -71,10 +69,7 @@ def get_rel_branch_names(blist):
7169
return sorted(names, key=lambda x: int(x.split(".")[1]), reverse=True)
7270

7371

74-
@click.command()
75-
@click.option("--dry", is_flag=True)
76-
@click.option("--remote", default="origin", help="The git remote where to push.")
77-
def main(dry, remote):
72+
def main():
7873
# Detect repo root folder
7974
here = os.path.dirname(os.path.realpath(__file__))
8075
repo_dir = os.path.join(here, "..", "..")
@@ -85,26 +80,16 @@ def main(dry, remote):
8580
# Get the list of release branch names
8681
rel_br_names = get_rel_branch_names(repo.refs)
8782

88-
# Deduce docs version from current branch. Use the 'latest' alias if
89-
# version is the most recent
90-
docs_version, alias = get_docs_version(repo.active_branch.name, rel_br_names)
91-
if docs_version is None:
92-
print(f"Can't get version from current branch '{repo.active_branch}', skip docs generation")
93-
return 0
83+
# Deduce docs version from current branch.
84+
versioning_data = get_docs_version(repo.active_branch.name, rel_br_names)
9485

95-
# Taskfile args aren't regular args so we put everything in one string
96-
cmd = (f"task docs:publish DOCS_REMOTE={remote} DOCS_VERSION={docs_version} DOCS_ALIAS={alias}",)
97-
98-
if dry:
99-
print(cmd)
100-
return 0
101-
102-
subprocess.run(cmd, shell=True, check=True, cwd=repo_dir)
86+
# Return the data as JSON on stdout
87+
print(json.dumps(versioning_data))
10388

10489

10590
# Usage:
10691
# To run the script (must be run from within the repo tree):
10792
# $python build.py
10893
#
10994
if __name__ == "__main__":
110-
sys.exit(main())
95+
main()

workflow-templates/assets/deploy-mkdocs-versioned/build/tests/test_all.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,18 @@
1616

1717

1818
def test_get_docs_version():
19-
ver, alias = build.get_docs_version("main", [])
20-
assert ver == "dev"
21-
assert alias == ""
19+
data = build.get_docs_version("main", [])
20+
assert data["version"] == "dev"
21+
assert data["alias"] == ""
2222

2323
release_names = ["1.4.x", "0.13.x"]
24-
ver, alias = build.get_docs_version("0.13.x", release_names)
25-
assert ver == "0.13"
26-
assert alias == ""
27-
ver, alias = build.get_docs_version("1.4.x", release_names)
28-
assert ver == "1.4"
29-
assert alias == "latest"
24+
data = build.get_docs_version("0.13.x", release_names)
25+
assert data["version"] == "0.13"
26+
assert data["alias"] == ""
27+
data = build.get_docs_version("1.4.x", release_names)
28+
assert data["version"] == "1.4"
29+
assert data["alias"] == "latest"
3030

31-
ver, alias = build.get_docs_version("0.1.x", [])
32-
assert ver is None
33-
assert alias is None
31+
data = build.get_docs_version("0.1.x", [])
32+
assert data["version"] is None
33+
assert data["alias"] is None

workflow-templates/dependabot/workflow-template-copies/.github/workflows/deploy-mkdocs-versioned-poetry.yml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,18 @@ jobs:
6464
- name: Install Python dependencies
6565
run: poetry install --no-root
6666

67+
- name: Determine versioning parameters
68+
id: determine-versioning
69+
run: echo "::set-output name=data::$(poetry run python docs/build/build.py)"
70+
6771
- name: Publish documentation
68-
# Determine docs version for the commit pushed and publish accordingly using Mike.
69-
# Publishing implies creating a git commit on the gh-pages branch, we let @ArduinoBot own these commits.
72+
if: fromJson(steps.determine-versioning.outputs.data).version != null
7073
run: |
74+
# Publishing implies creating a git commit on the gh-pages branch, we let @ArduinoBot own these commits.
7175
git config --global user.email "[email protected]"
7276
git config --global user.name "ArduinoBot"
7377
git fetch --no-tags --prune --depth=1 origin +refs/heads/gh-pages:refs/remotes/origin/gh-pages
74-
poetry run python docs/build/build.py
78+
task docs:publish \
79+
DOCS_REMOTE=origin \
80+
DOCS_VERSION=${{ fromJson(steps.determine-versioning.outputs.data).version }} \
81+
DOCS_ALIAS=${{ fromJson(steps.determine-versioning.outputs.data).alias }}

workflow-templates/deploy-mkdocs-versioned-poetry.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ See the ["Deploy Website" workflow (MkDocs, Poetry) documentation](deploy-mkdocs
7070

7171
1. Run this command:
7272
```
73-
poetry add --dev "click@<7.2" "gitpython@^3.1.1" "mike@^1.0.1"
73+
poetry add --dev "gitpython@^3.1.1" "mike@^1.0.1"
7474
```
7575
1. Commit the resulting `pyproject.toml` and `poetry.lock` files.
7676

workflow-templates/deploy-mkdocs-versioned-poetry.yml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,18 @@ jobs:
6464
- name: Install Python dependencies
6565
run: poetry install --no-root
6666

67+
- name: Determine versioning parameters
68+
id: determine-versioning
69+
run: echo "::set-output name=data::$(poetry run python docs/build/build.py)"
70+
6771
- name: Publish documentation
68-
# Determine docs version for the commit pushed and publish accordingly using Mike.
69-
# Publishing implies creating a git commit on the gh-pages branch, we let @ArduinoBot own these commits.
72+
if: fromJson(steps.determine-versioning.outputs.data).version != null
7073
run: |
74+
# Publishing implies creating a git commit on the gh-pages branch, we let @ArduinoBot own these commits.
7175
git config --global user.email "[email protected]"
7276
git config --global user.name "ArduinoBot"
7377
git fetch --no-tags --prune --depth=1 origin +refs/heads/gh-pages:refs/remotes/origin/gh-pages
74-
poetry run python docs/build/build.py
78+
task docs:publish \
79+
DOCS_REMOTE=origin \
80+
DOCS_VERSION=${{ fromJson(steps.determine-versioning.outputs.data).version }} \
81+
DOCS_ALIAS=${{ fromJson(steps.determine-versioning.outputs.data).alias }}

0 commit comments

Comments
 (0)