Skip to content

Commit 637f50f

Browse files
authored
feat: fix get-next-version in CI (#16)
1 parent 503fea6 commit 637f50f

File tree

5 files changed

+87
-60
lines changed

5 files changed

+87
-60
lines changed

.github/workflows/release.yml

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,44 @@ jobs:
2828
2929
- name: Run tests
3030
run: pytest tests/ -v
31+
32+
get-next-version:
33+
needs: test
34+
uses: semantic-release-action/next-release-version/.github/workflows/next-release-version.yml@v4
3135

36+
update-version:
37+
needs: get-next-version
38+
runs-on: ubuntu-latest
39+
if: needs.get-next-version.outputs.new-release-published == 'true'
40+
steps:
41+
- uses: actions/checkout@v4
42+
with:
43+
token: ${{ secrets.GITHUB_TOKEN }}
44+
45+
- name: Set up Python
46+
uses: actions/setup-python@v4
47+
with:
48+
python-version: 3.11
49+
50+
- name: Install dependencies
51+
run: |
52+
python -m pip install --upgrade pip
53+
pip install -r requirements.txt
54+
55+
- name: Update version
56+
run: |
57+
echo "Updating version..."
58+
python scripts/update_version.py --next-version ${{ needs.get-next-version.outputs.new-release-version }}
59+
60+
- name: Upload updated version file
61+
uses: actions/upload-artifact@v4
62+
with:
63+
name: version-file
64+
path: casbin_cli/__version__.py
65+
3266
build-binaries:
33-
needs: test
67+
needs: update-version
68+
if: needs.get-next-version.outputs.new-release-published == 'true'
3469
runs-on: ${{ matrix.os }}
3570
strategy:
3671
matrix:
@@ -54,6 +89,17 @@ jobs:
5489
python -m pip install --upgrade pip
5590
pip install -r requirements.txt
5691
pip install pyinstaller
92+
93+
- name: Download version file
94+
uses: actions/download-artifact@v4
95+
with:
96+
name: version-file
97+
path: casbin_cli/
98+
99+
- name: Verify file content
100+
run: |
101+
echo "Content of __version__.py after update:"
102+
cat casbin_cli/__version__.py
57103
58104
- name: Build binary
59105
run: python scripts/build_binaries.py --platform ${{ matrix.platform }}
@@ -85,8 +131,14 @@ jobs:
85131
python -m pip install --upgrade pip
86132
pip install -r requirements.txt
87133
pip install wheel twine
88-
npm install -g semantic-release @semantic-release/changelog @semantic-release/exec @semantic-release/github
89-
134+
npm install -g semantic-release @semantic-release/changelog @semantic-release/exec @semantic-release/github @semantic-release/git
135+
136+
- name: Download version file
137+
uses: actions/download-artifact@v4
138+
with:
139+
name: version-file
140+
path: casbin_cli/
141+
90142
- name: Download all artifacts
91143
uses: actions/download-artifact@v4
92144
with:

.releaserc.json

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@
1010
"changelogFile": "CHANGELOG.md"
1111
}
1212
],
13-
[
14-
"@semantic-release/exec",
15-
{
16-
"prepareCmd": "python scripts/update_version.py ${nextRelease.version}"
17-
}
18-
],
13+
[
14+
"@semantic-release/git",
15+
{
16+
"assets": ["casbin_cli/__version__.py"],
17+
"message": "chore(release): ${nextRelease.version} [skip ci]\n\n${nextRelease.notes}"
18+
}
19+
],
1920
[
2021
"@semantic-release/github",
2122
{

casbin_cli/__version__.py

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,2 @@
1-
import subprocess
2-
3-
4-
def get_base_tag():
5-
try:
6-
command = ["git", "describe", "--tags", "--abbrev=0"]
7-
8-
tag = subprocess.check_output(
9-
command, text=True, stderr=subprocess.PIPE
10-
).strip()
11-
12-
if tag.startswith("v"):
13-
return tag[1:]
14-
return tag
15-
16-
except subprocess.CalledProcessError:
17-
print("Error: Failed to find any git tags.")
18-
return None
19-
20-
21-
__version__ = get_base_tag()
1+
__version__ = "1.6.0"
2+
__pycasbin_version__ = "2.3.0"

casbin_cli/client.py

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
from casbin_cli.enforcer_factory import EnforcerFactory
66
from casbin_cli.utils import process_line_breaks
77
from casbin_cli.__version__ import __version__
8+
from casbin_cli.__version__ import __pycasbin_version__
9+
810

911
class Client:
1012
@staticmethod
@@ -26,18 +28,7 @@ def run(args=None):
2628
return ""
2729
elif command_name in ['-v', '--version']:
2830
print(f"casbin-python-cli {__version__}")
29-
try:
30-
from importlib.metadata import version
31-
32-
pycasbin_version = version("pycasbin")
33-
except ImportError:
34-
try:
35-
from importlib_metadata import version
36-
37-
pycasbin_version = version("pycasbin")
38-
except (ImportError, Exception):
39-
pycasbin_version = "unknown"
40-
print(f"pycasbin {pycasbin_version}")
31+
print(f"pycasbin {__pycasbin_version__}")
4132
return ""
4233
elif command_name == 'completion':
4334
if len(args) < 2:

scripts/update_version.py

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,33 @@
1515
import sys
1616
import re
1717
import os
18+
import argparse
1819

19-
def update_version(new_version):
20+
def update_version():
2021
"""Update version in all relevant files"""
22+
parser = argparse.ArgumentParser()
23+
parser.add_argument('--next-version', help='next release version(x.x.x)')
24+
args = parser.parse_args()
2125

22-
# Update setup.py
23-
setup_py_path = "setup.py"
24-
with open(setup_py_path, 'r') as f:
25-
content = f.read()
26-
27-
# Update client.py version display
28-
client_py_path = "casbin_cli/client.py"
29-
with open(client_py_path, 'r') as f:
30-
content = f.read()
31-
26+
new_version = args.next_version
27+
try:
28+
from importlib.metadata import version
29+
30+
pycasbin_version = version("pycasbin")
31+
except ImportError:
32+
try:
33+
from importlib_metadata import version
34+
35+
pycasbin_version = version("pycasbin")
36+
except (ImportError, Exception):
37+
pycasbin_version = "unknown"
3238
# Create __version__.py
3339
version_py_path = "casbin_cli/__version__.py"
3440
with open(version_py_path, 'w') as f:
3541
f.write(f'__version__ = "{new_version}"\n')
42+
f.write(f'__pycasbin_version__ = "{pycasbin_version}"\n')
3643

3744
print(f"Updated version to {new_version}")
3845

39-
if __name__ == "__main__":
40-
if len(sys.argv) != 2:
41-
print("Usage: python update_version.py <version>")
42-
sys.exit(1)
43-
44-
new_version = sys.argv[1]
45-
update_version(new_version)
46+
if __name__ == "__main__":
47+
update_version()

0 commit comments

Comments
 (0)