Sentry CLI

3.3.5 · active · verified Thu Apr 16

sentry-cli is a powerful command-line utility for interacting with Sentry, enabling tasks like creating releases, uploading source maps, managing projects, and configuring your Sentry instance. The Python package primarily serves as a convenient wrapper to install and execute the Rust-based binary. It is actively maintained with frequent snapshot builds and regular stable releases.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to programmatically interact with `sentry-cli` from Python using the `subprocess` module. It includes examples for checking the version, creating, and finalizing a Sentry release. Ensure you have `SENTRY_AUTH_TOKEN`, `SENTRY_ORG`, and `SENTRY_PROJECT` environment variables configured or set `sentry-cli login` interactively. Replace placeholder slugs with your actual Sentry organization and project details.

import os
import subprocess

# Ensure SENTRY_AUTH_TOKEN is set for non-interactive use (e.g., CI/CD)
# It can be obtained from your Sentry 'API Keys' settings.
# For interactive use, run 'sentry-cli login' once in your terminal.
os.environ['SENTRY_AUTH_TOKEN'] = os.environ.get('SENTRY_AUTH_TOKEN', '')
os.environ['SENTRY_ORG'] = os.environ.get('SENTRY_ORG', 'your-org-slug') # Replace with your Sentry organization slug
os.environ['SENTRY_PROJECT'] = os.environ.get('SENTRY_PROJECT', 'your-project-slug') # Replace with your Sentry project slug

def run_sentry_cli_command(command_args):
    full_command = ['sentry-cli'] + command_args
    print(f"\nRunning: {' '.join(full_command)}")
    try:
        result = subprocess.run(
            full_command,
            capture_output=True,
            text=True,
            check=True, # Raises CalledProcessError for non-zero exit codes
            env=os.environ # Pass current environment including SENTRY_AUTH_TOKEN
        )
        print("STDOUT:\n" + result.stdout)
        if result.stderr:
            print("STDERR:\n" + result.stderr)
        return result.stdout
    except subprocess.CalledProcessError as e:
        print(f"Error executing command: {e.cmd}")
        print(f"Return Code: {e.returncode}")
        print(f"STDOUT:\n{e.stdout}")
        print(f"STDERR:\n{e.stderr}")
        raise # Re-raise the error to stop execution
    except FileNotFoundError:
        print("Error: 'sentry-cli' command not found. Please ensure it's installed and in your system PATH.")
        print("You can install it via 'pip install sentry-cli' or by following instructions at https://docs.sentry.io/product/cli/installation/")
        raise

if __name__ == "__main__":
    try:
        # 1. Check the Sentry CLI version
        run_sentry_cli_command(['--version'])

        # 2. Get the current organization's status
        run_sentry_cli_command(['info'])

        # 3. Create a Sentry release (replace with your actual release name)
        release_name = f"my-app@{os.getenv('BUILD_VERSION', '1.0.0-test')}"
        run_sentry_cli_command(['releases', 'new', release_name])

        # 4. Set the release to committed (optional, for connecting to SCM)
        # run_sentry_cli_command(['releases', 'set-commits', release_name, '--auto'])

        # 5. Finalize the release
        run_sentry_cli_command(['releases', 'finalize', release_name])

        print("\nSentry CLI commands executed successfully!")

    except Exception as e:
        print(f"\nAn error occurred during quickstart execution: {e}")

view raw JSON →