Skip to content

Commit 7558bb8

Browse files
committed
trying the andruino way of versioning
1 parent 33f825e commit 7558bb8

File tree

3 files changed

+114
-22
lines changed

3 files changed

+114
-22
lines changed

Diff for: .github/workflows/deploy-documents.yml

+43-21
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ env:
99
on:
1010
push:
1111
branches:
12-
# Branch to base "dev" website on. Set in siteversion.py also.
12+
# Branch to base "dev" website on.
1313
- master
1414
# Release branches have names like 0.8.x, 0.9.x, ...
1515
- "[0-9]+.[0-9]+.x"
@@ -66,34 +66,56 @@ jobs:
6666
6767
- name: Determine versioning parameters
6868
id: determine-versioning
69-
run: |
70-
# Read the first line from the VERSION file
71-
LINE=$(head -n 1 ./VERSION)
72-
73-
# Extract version and alias using parameter expansion
74-
VERSION="${LINE%%|*}"
75-
ALIAS="${LINE#*|}"
76-
77-
# Print the extracted values for verification
78-
echo "Version: $VERSION"
79-
echo "Alias: $ALIAS"
80-
81-
echo "PH_DOCS_VERSION=$VERSION" >> $GITHUB_ENV
82-
echo "PH_DOCS_ALIAS=$ALIAS" >> $GITHUB_ENV
69+
run: echo "data=$(poetry run python siteversion.py)" >> $GITHUB_OUTPUT
8370

84-
- name: Deploy
85-
if: ${{ env.PH_DOCS_VERSION }} != null
71+
- name: Publish documentation
72+
if: fromJson(steps.determine-versioning.outputs.data).version != null
8673
run: |
8774
# Publishing implies creating a git commit on the production branch,
8875
# We will need to create a user for this at some point
8976
echo "Deploying with Mike"
9077
git config user.name niden
9178
git config user.email [email protected]
9279
git fetch --no-tags --prune --depth=1 origin +refs/heads/production:refs/remotes/origin/production
93-
mike deploy \
80+
poetry run mike deploy \
9481
--update-aliases \
95-
--branch production \
9682
--push \
97-
${{ env.PH_DOCS_VERSION }} \
98-
${{ env.PH_DOCS_ALIAS }}
83+
--remote origin \
84+
${{ fromJson(steps.determine-versioning.outputs.data).version }} \
85+
${{ fromJson(steps.determine-versioning.outputs.data).alias }}
9986
echo "Deployed"
87+
88+
#
89+
# - name: Determine versioning parameters
90+
# id: determine-versioning
91+
# run: |
92+
# # Read the first line from the VERSION file
93+
# LINE=$(head -n 1 ./VERSION)
94+
#
95+
# # Extract version and alias using parameter expansion
96+
# VERSION="${LINE%%|*}"
97+
# ALIAS="${LINE#*|}"
98+
#
99+
# # Print the extracted values for verification
100+
# echo "Version: $VERSION"
101+
# echo "Alias: $ALIAS"
102+
#
103+
# echo "PH_DOCS_VERSION=$VERSION" >> $GITHUB_ENV
104+
# echo "PH_DOCS_ALIAS=$ALIAS" >> $GITHUB_ENV
105+
#
106+
# - name: Deploy
107+
# if: ${{ env.PH_DOCS_VERSION }} != null
108+
# run: |
109+
# # Publishing implies creating a git commit on the production branch,
110+
# # We will need to create a user for this at some point
111+
# echo "Deploying with Mike"
112+
# git config user.name niden
113+
# git config user.email [email protected]
114+
# git fetch --no-tags --prune --depth=1 origin +refs/heads/production:refs/remotes/origin/production
115+
# mike deploy \
116+
# --update-aliases \
117+
# --remote origin \
118+
# --push \
119+
# ${{ env.PH_DOCS_VERSION }} \
120+
# ${{ env.PH_DOCS_ALIAS }}
121+
# echo "Deployed"

Diff for: VERSION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
5.8|
1+
5.8|latest

Diff for: siteversion.py

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Workflow inspiration and adaptation came from Andruino-Cli
2+
# https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/deploy-mkdocs-versioned/siteversion/siteversion.py
3+
4+
import os
5+
import re
6+
import json
7+
8+
from git import Repo
9+
10+
# `master` is never to be touched. We can use that as a dev version.
11+
#
12+
# As with andruino-cli, we have the same versioning scheme for the documentation.
13+
# The branches that are for deployment are the ones that have the format `0.99.x`.
14+
15+
16+
DEV_BRANCHES = ["master"] # Name of the branch used for the "dev" website source content
17+
18+
# Get the docs version, setting the latest release as the 'latest' alias.
19+
def get_docs_version(ref_name, release_branches):
20+
if ref_name in DEV_BRANCHES:
21+
return {"version": "dev", "alias": ""}
22+
23+
if ref_name in release_branches:
24+
# if version is latest, add an alias
25+
alias = "latest" if ref_name == release_branches[0] else ""
26+
# strip `.x` suffix from the branch name to get the version: 0.3.x -> 0.3
27+
return {"version": ref_name[:-2], "alias": alias}
28+
29+
return {"version": None, "alias": None}
30+
31+
32+
# Get the names of the release branches, sorted from newest to older.
33+
def get_rel_branch_names(blist):
34+
pattern = re.compile(r"origin/(\d+\.\d+\.x)")
35+
names = []
36+
for b in blist:
37+
res = pattern.search(b.name)
38+
if res is not None:
39+
names.append(res.group(1))
40+
41+
# Since sorting is stable, first sort by major...
42+
names = sorted(names, key=lambda x: int(x.split(".")[0]), reverse=True)
43+
# ...then by minor
44+
return sorted(names, key=lambda x: int(x.split(".")[1]), reverse=True)
45+
46+
47+
def main():
48+
# Detect repo root folder
49+
here = os.path.dirname(os.path.realpath(__file__))
50+
repo_dir = os.path.join(here, "..", "..")
51+
52+
# Get current repo
53+
repo = Repo(repo_dir)
54+
55+
# Get the list of release branch names
56+
rel_br_names = get_rel_branch_names(repo.refs)
57+
58+
# Deduce docs version from current branch.
59+
versioning_data = get_docs_version(repo.active_branch.name, rel_br_names)
60+
61+
# Return the data as JSON on stdout
62+
print(json.dumps(versioning_data))
63+
64+
65+
# Usage:
66+
# To run the script (must be run from within the repo tree):
67+
# $python siteversion.py
68+
#
69+
if __name__ == "__main__":
70+
main()

0 commit comments

Comments
 (0)