Skip to content

Commit 4cff405

Browse files
Massimiliano Pippimasci
Massimiliano Pippi
authored andcommitted
expand build.py CLI to simplify action's code
1 parent 15a569b commit 4cff405

File tree

4 files changed

+29
-27
lines changed

4 files changed

+29
-27
lines changed

.github/workflows/docs.yaml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,8 @@ jobs:
8080
- name: Publish docs
8181
# determine docs version for the commit pushed and publish accordingly using Mike
8282
if: github.event_name == 'push'
83-
env:
84-
REMOTE: https://x-access-token:${{secrets.GITHUB_TOKEN}}@github.com/${{github.repository}}.git
8583
run: |
8684
git config --global user.email "bot@arduino.cc"
8785
git config --global user.name "ArduinoBot"
88-
python docs/build.py
86+
git remote add upstream https://${{ github.actor }}:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git
87+
python docs/build.py --remote upstream

Taskfile.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ tasks:
4040
- docs:gen:commands
4141
- docs:gen:protobuf
4242
cmds:
43-
- mike deploy -r {{.DOCS_REMOTE}} {{.DOCS_VERSION}} {{.DOCS_ALIAS}}
43+
- mike deploy -p -r {{.DOCS_REMOTE}} {{.DOCS_VERSION}} {{.DOCS_ALIAS}}
4444

4545
docs:serve:
4646
desc: Run documentation website locally

docs/build.py

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import unittest
1919
import subprocess
2020

21+
import click
2122
from git import Repo
2223

2324

@@ -75,11 +76,19 @@ def get_rel_branch_names(blist):
7576
return sorted(names, key=lambda x: int(x.split(".")[1]), reverse=True)
7677

7778

78-
def main(repo_dir):
79-
# Git remote must be set to publish docs
80-
remote = os.environ.get("REMOTE")
81-
if not remote:
82-
print("REMOTE env var must be set to publish, running dry mode")
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, "..")
8392

8493
# Get current repo
8594
repo = Repo(repo_dir)
@@ -96,18 +105,16 @@ def main(repo_dir):
96105
)
97106
return 0
98107

99-
args = [
100-
"task docs:publish",
101-
f"DOCS_REMOTE={remote}",
102-
f"DOCS_VERSION={docs_version}",
103-
f"DOCS_ALIAS={alias}",
104-
]
105-
if remote:
106-
subprocess.run(args, shell=True, check=True, cwd=repo_dir)
107-
else:
108-
print(" ".join(args))
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
109116

110-
return 0
117+
subprocess.run(cmd, shell=True, check=True, cwd=repo_dir)
111118

112119

113120
# Usage:
@@ -119,9 +126,4 @@ def main(repo_dir):
119126
# $python build.py
120127
#
121128
if __name__ == "__main__":
122-
if len(sys.argv) > 1 and sys.argv[1] == "test":
123-
unittest.main(argv=[""], exit=False)
124-
sys.exit(0)
125-
126-
here = os.path.dirname(os.path.realpath(__file__))
127-
sys.exit(main(os.path.join(here, "..")))
129+
sys.exit(main())

requirements_docs.txt

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

0 commit comments

Comments
 (0)