Skip to content

Commit 315d441

Browse files
authored
ci: add PyPI publish workflow with trusted publishing (#601)
1 parent 51b5e08 commit 315d441

4 files changed

Lines changed: 605 additions & 13 deletions

File tree

‎.github/update_versions.py‎

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
import pathlib
2+
import re
3+
import click
4+
from packaging.version import Version
5+
6+
PACKAGES = {
7+
"livekit": "livekit-rtc/livekit/rtc/version.py",
8+
"livekit-api": "livekit-api/livekit/api/version.py",
9+
"livekit-protocol": "livekit-protocol/livekit/protocol/version.py",
10+
}
11+
12+
13+
def _esc(*codes: int) -> str:
14+
return "\033[" + ";".join(str(c) for c in codes) + "m"
15+
16+
17+
def read_version(f: pathlib.Path) -> str:
18+
text = f.read_text()
19+
m = re.search(r'__version__\s*=\s*[\'"]([^\'"]+)[\'"]', text)
20+
if not m:
21+
raise ValueError(f"could not find __version__ in {f}")
22+
return m.group(1)
23+
24+
25+
def write_new_version(f: pathlib.Path, new_version: str) -> None:
26+
text = f.read_text()
27+
new_text = re.sub(
28+
r'__version__\s*=\s*[\'"][^\'"]*[\'"]',
29+
f'__version__ = "{new_version}"',
30+
text,
31+
count=1,
32+
)
33+
f.write_text(new_text)
34+
35+
36+
def bump_version(cur: str, bump_type: str) -> str:
37+
v = Version(cur)
38+
if bump_type == "release":
39+
return v.base_version
40+
if bump_type == "patch":
41+
return f"{v.major}.{v.minor}.{v.micro + 1}"
42+
if bump_type == "minor":
43+
return f"{v.major}.{v.minor + 1}.0"
44+
if bump_type == "major":
45+
return f"{v.major + 1}.0.0"
46+
raise ValueError(f"unknown bump type: {bump_type}")
47+
48+
49+
def bump_prerelease(cur: str, bump_type: str) -> str:
50+
v = Version(cur)
51+
base = v.base_version
52+
if bump_type == "rc":
53+
if v.pre and v.pre[0] == "rc":
54+
next_rc = v.pre[1] + 1
55+
else:
56+
next_rc = 1
57+
return f"{base}.rc{next_rc}"
58+
raise ValueError(f"unknown prerelease bump type: {bump_type}")
59+
60+
61+
def update_api_protocol_dependency(new_protocol_version: str) -> None:
62+
"""Update livekit-api's dependency on livekit-protocol."""
63+
pyproject = pathlib.Path("livekit-api/pyproject.toml")
64+
if not pyproject.exists():
65+
return
66+
old_text = pyproject.read_text()
67+
new_text = re.sub(
68+
r'"livekit-protocol>=[\w.\-]+,',
69+
f'"livekit-protocol>={new_protocol_version},',
70+
old_text,
71+
)
72+
if new_text != old_text:
73+
pyproject.write_text(new_text)
74+
print(f"Updated livekit-api dependency on livekit-protocol to >={new_protocol_version}")
75+
76+
77+
def do_bump(package: str, bump_type: str) -> None:
78+
version_path = PACKAGES[package]
79+
vf = pathlib.Path(version_path)
80+
cur = read_version(vf)
81+
new = bump_version(cur, bump_type)
82+
print(f"{package}: {_esc(31)}{cur}{_esc(0)} -> {_esc(32)}{new}{_esc(0)}")
83+
write_new_version(vf, new)
84+
85+
if package == "livekit-protocol":
86+
update_api_protocol_dependency(new)
87+
88+
89+
def do_prerelease(package: str, prerelease_type: str) -> None:
90+
version_path = PACKAGES[package]
91+
vf = pathlib.Path(version_path)
92+
cur = read_version(vf)
93+
new = bump_prerelease(cur, prerelease_type)
94+
print(f"{package}: {_esc(31)}{cur}{_esc(0)} -> {_esc(32)}{new}{_esc(0)}")
95+
write_new_version(vf, new)
96+
97+
if package == "livekit-protocol":
98+
update_api_protocol_dependency(new)
99+
100+
101+
@click.command("bump")
102+
@click.option(
103+
"--package",
104+
type=click.Choice(list(PACKAGES.keys())),
105+
required=True,
106+
help="Package to bump.",
107+
)
108+
@click.option(
109+
"--pre",
110+
type=click.Choice(["rc", "none"]),
111+
default="none",
112+
help="Pre-release type.",
113+
)
114+
@click.option(
115+
"--bump-type",
116+
type=click.Choice(["patch", "minor", "major", "release"]),
117+
default="patch",
118+
help="Type of version bump.",
119+
)
120+
def bump(package: str, pre: str, bump_type: str) -> None:
121+
if pre == "none":
122+
do_bump(package, bump_type)
123+
else:
124+
do_prerelease(package, pre)
125+
126+
127+
if __name__ == "__main__":
128+
bump()

‎.github/workflows/build-docs.yml‎

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
# This workflow will upload a Python Package using Twine when a release is created
2-
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python#publishing-to-package-registries
3-
4-
# This workflow uses actions that are not certified by GitHub.
5-
# They are provided by a third-party and are governed by
6-
# separate terms of service, privacy policy, and support
7-
# documentation.
8-
91
name: Build Docs
102

113
on:
@@ -50,24 +42,31 @@ jobs:
5042
submodules: recursive
5143

5244
- name: Install Package to Document
53-
run: python -m pip install ${{ inputs.package_dir }}/
45+
env:
46+
PACKAGE_DIR: ${{ inputs.package_dir }}
47+
run: python -m pip install "$PACKAGE_DIR/"
5448

5549
- name: Download ffi
50+
env:
51+
PACKAGE_NAME: ${{ inputs.package_name }}
5652
run: |
57-
if [[ '${{ inputs.package_name }}' = 'livekit.rtc' ]]; then
53+
if [[ "$PACKAGE_NAME" = 'livekit.rtc' ]]; then
5854
pip install requests
59-
python livekit-rtc/rust-sdks/download_ffi.py --output $(python -m site --user-site)/livekit/rtc/resources
55+
python livekit-rtc/rust-sdks/download_ffi.py --output "$(python -m site --user-site)/livekit/rtc/resources"
6056
fi
6157
6258
- name: Install pdoc
6359
run: pip install --upgrade pdoc
6460

6561
- name: Build Docs
66-
run: python -m pdoc ${{ inputs.package_name }} --docformat=google --output-dir docs
62+
env:
63+
PACKAGE_NAME: ${{ inputs.package_name }}
64+
run: python -m pdoc "$PACKAGE_NAME" --docformat=google --output-dir docs
6765

6866
- name: S3 Upload
69-
run: aws s3 cp docs/ s3://livekit-docs/${{ inputs.package_dir }} --recursive
7067
env:
68+
PACKAGE_DIR: ${{ inputs.package_dir }}
7169
AWS_ACCESS_KEY_ID: ${{ secrets.DOCS_DEPLOY_AWS_ACCESS_KEY }}
7270
AWS_SECRET_ACCESS_KEY: ${{ secrets.DOCS_DEPLOY_AWS_API_SECRET }}
7371
AWS_DEFAULT_REGION: "us-east-1"
72+
run: aws s3 cp docs/ "s3://livekit-docs/$PACKAGE_DIR" --recursive

0 commit comments

Comments
 (0)