Skip to content

Commit 68f8ee4

Browse files
authored
Merge pull request #310 from ynput/enhancement/ci-for-release
CI: Add CI action to release new version
2 parents 832af13 + cdbe566 commit 68f8ee4

2 files changed

Lines changed: 190 additions & 0 deletions

File tree

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
name: 🚀 Create Release
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
bump_minor:
7+
description: 'Bump minor version'
8+
required: false
9+
type: boolean
10+
default: false
11+
version:
12+
description: 'Release version (optional, if not provided will use version from develop without -dev)'
13+
required: false
14+
type: string
15+
16+
jobs:
17+
release:
18+
runs-on: ubuntu-latest
19+
if: github.ref == 'refs/heads/develop'
20+
permissions:
21+
contents: write
22+
steps:
23+
- uses: actions/checkout@v4
24+
with:
25+
fetch-depth: 0
26+
token: ${{ secrets.GITHUB_TOKEN }}
27+
28+
- name: Set up Python
29+
uses: actions/setup-python@v5
30+
with:
31+
python-version: '3.9'
32+
33+
- name: Configure Git
34+
run: |
35+
git config --global user.name "${{ secrets.CI_USER }}"
36+
git config --global user.email "${{ secrets.CI_EMAIL }}"
37+
38+
- name: Get release version
39+
id: get_version
40+
run: |
41+
if [ -n "${{ github.event.inputs.version }}" ]; then
42+
echo "version=${{ github.event.inputs.version }}" >> $GITHUB_OUTPUT
43+
else
44+
ARGS=""
45+
if [ "${{ github.event.inputs.bump_minor }}" = "true" ]; then
46+
ARGS="--bump-minor"
47+
fi
48+
VERSION=$(python manage_version.py get-release-version $ARGS)
49+
echo "version=$VERSION" >> $GITHUB_OUTPUT
50+
fi
51+
52+
- name: Update version for release
53+
run: |
54+
python manage_version.py update --version ${{ steps.get_version.outputs.version }}
55+
56+
- name: Commit release version
57+
run: |
58+
git add ayon_api/version.py pyproject.toml
59+
git commit -m "Release version ${{ steps.get_version.outputs.version }}"
60+
git push origin develop
61+
62+
- name: Rebase main on develop
63+
run: |
64+
git checkout main
65+
git rebase develop
66+
git push origin main
67+
68+
- name: Create and push tag
69+
run: |
70+
git tag ${{ steps.get_version.outputs.version }}
71+
git push origin ${{ steps.get_version.outputs.version }}
72+
73+
- name: Bump version on develop
74+
run: |
75+
git checkout develop
76+
NEW_VERSION=$(python manage_version.py get-dev-version)
77+
python manage_version.py update --version $NEW_VERSION
78+
git add ayon_api/version.py pyproject.toml
79+
git commit -m "Bump version to $NEW_VERSION"
80+
git push origin develop
81+
82+
- name: Create GitHub Release
83+
uses: softprops/action-gh-release@v2
84+
with:
85+
tag_name: ${{ steps.get_version.outputs.version }}
86+
generate_release_notes: true
87+
token: ${{ secrets.GITHUB_TOKEN }}

manage_version.py

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import re
2+
import sys
3+
import argparse
4+
from pathlib import Path
5+
6+
SEMVER_REGEX = re.compile(
7+
r"^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)"
8+
r"(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?"
9+
r"(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$"
10+
)
11+
12+
13+
def get_current_version(version_file: Path) -> str:
14+
content = version_file.read_text(encoding="utf-8")
15+
match = re.search(r'__version__\s*=\s*"(.*)"', content)
16+
if match:
17+
return match.group(1)
18+
raise ValueError(f"Version not found in {version_file}")
19+
20+
21+
def update_version_in_src(
22+
version_file: Path,
23+
pyproject_file: Path,
24+
new_version: str,
25+
):
26+
content = version_file.read_text(encoding="utf-8")
27+
new_content = re.sub(
28+
r'(__version__\s*=\s*").*(")',
29+
rf'\g<1>{new_version}\g<2>',
30+
content
31+
)
32+
version_file.write_text(new_content, encoding="utf-8")
33+
34+
# Update pyproject.toml
35+
content = pyproject_file.read_text(encoding="utf-8")
36+
new_lines = []
37+
for line in content.splitlines():
38+
if line.startswith("version"):
39+
line = f"version = \"{new_version}\""
40+
new_lines.append(line)
41+
42+
pyproject_file.write_text("\n".join(new_lines), encoding="utf-8")
43+
44+
45+
def bump_to_dev_version(version: str) -> str:
46+
version_parts = SEMVER_REGEX.match(version).groups()
47+
major, minor, patch = version_parts[0:3]
48+
patch = str(int(patch) + 1)
49+
return f"{major}.{minor}.{patch}-dev"
50+
51+
52+
def bump_to_release_version(version: str, bump_minor: bool = False) -> str:
53+
version_parts = SEMVER_REGEX.match(version).groups()
54+
major, minor, patch = version_parts[0:3]
55+
if bump_minor:
56+
minor = str(int(minor) + 1)
57+
patch = "0"
58+
return f"{major}.{minor}.{patch}"
59+
60+
61+
def main():
62+
parser = argparse.ArgumentParser()
63+
parser.add_argument(
64+
"command",
65+
choices=["get-release-version", "update", "get-dev-version"]
66+
)
67+
parser.add_argument(
68+
"--version",
69+
help="Version to set for update command",
70+
)
71+
parser.add_argument(
72+
"--bump-minor",
73+
action="store_true",
74+
help="Bump minor version for get-release-version command",
75+
)
76+
args = parser.parse_args()
77+
78+
repo_root = Path(__file__).parent
79+
version_file = repo_root / "ayon_api" / "version.py"
80+
pyproject_file = repo_root / "pyproject.toml"
81+
82+
current_version = get_current_version(version_file)
83+
84+
if args.command == "get-release-version":
85+
if current_version:
86+
print(bump_to_release_version(current_version, args.bump_minor))
87+
else:
88+
sys.exit(1)
89+
elif args.command == "get-dev-version":
90+
if current_version:
91+
print(bump_to_dev_version(current_version))
92+
else:
93+
sys.exit(1)
94+
elif args.command == "update":
95+
if not args.version:
96+
print("Error: --version is required for update command")
97+
sys.exit(1)
98+
update_version_in_src(version_file, pyproject_file, args.version)
99+
print(f"Updated version to {args.version}")
100+
101+
102+
if __name__ == "__main__":
103+
main()

0 commit comments

Comments
 (0)