Python Semantic Release

10.5.3 · active · verified Sun Apr 12

python-semantic-release automates semantic versioning, changelog generation, and project releases for Python projects based on Git commit messages. It integrates with various CI/CD pipelines and supports multiple version control systems and distribution platforms. The current version is 10.5.3, with releases typically following breaking changes or significant feature additions.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to programmatically invoke the `semantic-release` CLI using `subprocess.run` in Python. It sets up a temporary Git repository and a minimal `pyproject.toml` to make the `semantic-release publish --dry-run` command runnable in isolation. The primary way to use `python-semantic-release` is via its `semantic-release` CLI command, typically integrated into CI/CD pipelines. Ensure your project has a properly configured `pyproject.toml` and a suitable Git history for real releases.

import subprocess
import os
import shutil

def run_semantic_release_demo():
    # Setup a temporary directory for the demo
    demo_dir = "semantic_release_demo"
    if os.path.exists(demo_dir):
        shutil.rmtree(demo_dir)
    os.makedirs(demo_dir)
    os.chdir(demo_dir)

    try:
        # 1. Initialize a Git repository
        subprocess.run(['git', 'init', '-b', 'main'], check=True, capture_output=True)
        subprocess.run(['git', 'config', 'user.email', 'test@example.com'], check=True, capture_output=True)
        subprocess.run(['git', 'config', 'user.name', 'Test User'], check=True, capture_output=True)
        
        # 2. Create a minimal pyproject.toml for configuration
        pyproject_content = '''
[tool.semantic_release]
branch = "main"
version_source = "tag"
changelog_file = "CHANGELOG.md"
'''
        with open("pyproject.toml", "w") as f:
            f.write(pyproject_content)

        # 3. Add some initial content and commit
        with open('README.md', 'w') as f:
            f.write('# My Project\n\nThis is a demo project.')
        subprocess.run(['git', 'add', 'README.md', 'pyproject.toml'], check=True, capture_output=True)
        subprocess.run(['git', 'commit', '-m', 'feat: Initial commit and project setup'], check=True, capture_output=True)
        print("Successfully set up demo Git repository and pyproject.toml.\n")

        # 4. Run semantic-release in dry-run mode
        print("--- Running semantic-release publish (dry run) ---")
        result = subprocess.run(
            ['semantic-release', 'publish', '--dry-run'],
            capture_output=True,
            text=True,
            check=True
        )
        print("STDOUT:\n", result.stdout)
        print("STDERR:\n", result.stderr)
        print("\nSemantic Release Dry Run completed successfully! Above is the simulated output.")
        print("\nTo perform a real release, remove '--dry-run' and ensure a remote Git repository is configured.")

    except subprocess.CalledProcessError as e:
        print(f"Error during semantic-release demo: {e}")
        print("STDOUT:\n", e.stdout)
        print("STDERR:\n", e.stderr)
    finally:
        os.chdir('..')
        shutil.rmtree(demo_dir, ignore_errors=True)
        print(f"\nCleaned up temporary directory: {demo_dir}")

if __name__ == '__main__':
    run_semantic_release_demo()

view raw JSON →