Skip to content

Commit 6d3f4ed

Browse files
author
Massimiliano Pippi
authored
[skip changelog] Configure git push from CI (#717)
* config git in publish step, remove magic string from build script * expand build.py CLI to simplify action's code * build when workflow changes * fetch gh-pages branch * fix version selector * Remove testing code * document who owns commits on gh-pages
1 parent a7d7de0 commit 6d3f4ed

File tree

5 files changed

+53
-40
lines changed

5 files changed

+53
-40
lines changed

.github/workflows/docs.yaml

+12-5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ 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
@@ -22,6 +24,7 @@ on:
2224
- 'docsgen/**'
2325
- 'cli/**'
2426
- 'rpc/**'
27+
- '.github/workflows/docs.yaml'
2528

2629
jobs:
2730
build:
@@ -71,14 +74,18 @@ jobs:
7174
python3 -m pip install -r ./requirements_docs.txt
7275
7376
- name: Build docs website
74-
# this runs on every PR to ensure the docs build is sane, these docs
77+
# This runs on every PR to ensure the docs build is sane, these docs
7578
# won't be published
7679
if: github.event_name == 'pull_request'
7780
run: task docs:build
7881

7982
- name: Publish docs
80-
# determine docs version for the commit pushed and publish accordingly using Mike
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.
8186
if: github.event_name == 'push'
82-
env:
83-
REMOTE: https://x-access-token:${{secrets.GITHUB_TOKEN}}@github.com/${{github.repository}}.git
84-
run: python docs/build.py
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

+1-1
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

+28-23
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,13 @@
1818
import unittest
1919
import subprocess
2020

21+
import click
2122
from git import Repo
2223

2324

25+
DEV_BRANCHES = ["master"]
26+
27+
2428
class TestScript(unittest.TestCase):
2529
def test_get_docs_version(self):
2630
ver, alias = get_docs_version("master", [])
@@ -41,7 +45,7 @@ def test_get_docs_version(self):
4145

4246

4347
def get_docs_version(ref_name, release_branches):
44-
if ref_name == "master":
48+
if ref_name in DEV_BRANCHES:
4549
return "dev", ""
4650

4751
if ref_name in release_branches:
@@ -72,11 +76,19 @@ def get_rel_branch_names(blist):
7276
return sorted(names, key=lambda x: int(x.split(".")[1]), reverse=True)
7377

7478

75-
def main(repo_dir):
76-
# Git remote must be set to publish docs
77-
remote = os.environ.get("REMOTE")
78-
if not remote:
79-
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, "..")
8092

8193
# Get current repo
8294
repo = Repo(repo_dir)
@@ -93,18 +105,16 @@ def main(repo_dir):
93105
)
94106
return 0
95107

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

107-
return 0
113+
if dry:
114+
print(cmd)
115+
return 0
116+
117+
subprocess.run(cmd, shell=True, check=True, cwd=repo_dir)
108118

109119

110120
# Usage:
@@ -116,9 +126,4 @@ def main(repo_dir):
116126
# $python build.py
117127
#
118128
if __name__ == "__main__":
119-
if len(sys.argv) > 1 and sys.argv[1] == "test":
120-
unittest.main(argv=[""], exit=False)
121-
sys.exit(0)
122-
123-
here = os.path.dirname(os.path.realpath(__file__))
124-
sys.exit(main(os.path.join(here, "..")))
129+
sys.exit(main())

docs/js/version-select.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
window.addEventListener("DOMContentLoaded", function() {
1+
window.addEventListener("DOMContentLoaded", function () {
22
// This is a bit hacky. Figure out the base URL from a known CSS file the
33
// template refers to...
44
var ex = new RegExp("/?assets/fonts/material-icons.css$");
@@ -12,29 +12,29 @@ window.addEventListener("DOMContentLoaded", function() {
1212
var select = document.createElement("select");
1313
select.classList.add("form-control");
1414

15-
options.forEach(function(i) {
15+
options.forEach(function (i) {
1616
var option = new Option(i.text, i.value, undefined,
17-
i.value === selected);
17+
i.value === selected);
1818
select.add(option);
1919
});
2020

2121
return select;
2222
}
2323

2424
var xhr = new XMLHttpRequest();
25-
xhr.open("GET", REL_BASE_URL + "/../versions.json");
26-
xhr.onload = function() {
25+
xhr.open("GET", ABS_BASE_URL + "/../versions.json");
26+
xhr.onload = function () {
2727
var versions = JSON.parse(this.responseText);
2828

29-
var realVersion = versions.find(function(i) {
29+
var realVersion = versions.find(function (i) {
3030
return i.version === CURRENT_VERSION ||
31-
i.aliases.includes(CURRENT_VERSION);
31+
i.aliases.includes(CURRENT_VERSION);
3232
}).version;
3333

34-
var select = makeSelect(versions.map(function(i) {
35-
return {text: i.title, value: i.version};
34+
var select = makeSelect(versions.map(function (i) {
35+
return { text: i.title, value: i.version };
3636
}), realVersion);
37-
select.addEventListener("change", function(event) {
37+
select.addEventListener("change", function (event) {
3838
window.location.href = REL_BASE_URL + "/../" + this.value;
3939
});
4040

requirements_docs.txt

+2-1
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)