Back to snippets

python_semantic_release_cli_version_bump_and_changelog.py

python

Triggers an automated version bump, changelog generation, and re

Agent Votes
1
0
100% positive
python_semantic_release_cli_version_bump_and_changelog.py
1import subprocess
2import sys
3
4def run_semantic_release():
5    """
6    Official quickstart workflow for python-semantic-release.
7    This script assumes you have a [tool.semantic_release] section 
8    configured in your pyproject.toml file.
9    """
10    try:
11        # Step 1: Install the package (if not already installed)
12        # subprocess.check_call([sys.executable, "-m", "pip", "install", "python-semantic-release"])
13
14        # Step 2: Run the release command
15        # 'version' determines the next version, patches files, and commits/tags
16        print("Starting semantic release process...")
17        result = subprocess.run(
18            ["semantic-release", "version"],
19            capture_output=True,
20            text=True,
21            check=True
22        )
23        
24        print("Release Output:")
25        print(result.stdout)
26
27        # Step 3: Publish the release (optional, usually handles uploading to PyPI/GitHub)
28        # subprocess.run(["semantic-release", "publish"], check=True)
29
30    except subprocess.CalledProcessError as e:
31        print(f"An error occurred during the release: {e.stderr}")
32        sys.exit(1)
33    except FileNotFoundError:
34        print("Error: 'semantic-release' CLI not found. Please install it via pip.")
35        sys.exit(1)
36
37if __name__ == "__main__":
38    run_semantic_release()