11# DiscoverVersion
22
3- This package automatically discover version information for a package. It
4- tries the following option to get the version of the package:
3+ This package automatically discovers version information for a package. It
4+ tries the following options to get the version of the package (in order) :
55
6+ * Check the ` DISCOVER_VERSION ` environment variable
67* Inspect a ` PKG-INFO ` file
78* Ask ` git ` , if the current directory is a git-repository
89* Ask ` importlib.metadata `
9-
10+
1011It is intended as a lightweight replacement for
1112` setuptools_scm ` .
1213
@@ -26,7 +27,7 @@ dynamic = ['version']
2627dependencies = [' DiscoverVersion' ]
2728```
2829
29- The add the following to your toplevel ` __init__.py ` :
30+ Then add the following to your toplevel ` __init__.py ` :
3031
3132``` python3
3233from DiscoverVersion import get_version
@@ -37,6 +38,58 @@ __version__ = get_version('my_package_name')
3738Note that it is important to hard code the name of your package in the call
3839to ` get_version ` .
3940
41+ ## Environment Variable Override
42+
43+ You can override version discovery by setting the ` DISCOVER_VERSION ` environment
44+ variable. This is useful in CI/CD pipelines where git may not be available or
45+ when building in isolated environments:
46+
47+ ``` bash
48+ DISCOVER_VERSION=1.2.3 python -m build
49+ ```
50+
51+ ## Command Line Interface
52+
53+ DiscoverVersion provides a CLI for discovering and outputting version information:
54+
55+ ``` bash
56+ # Print the discovered version
57+ python -m DiscoverVersion
58+
59+ # Or use the entry point
60+ discover-version
61+
62+ # Write version to a Python file
63+ python -m DiscoverVersion --write-to version.py
64+
65+ # Write version to a plain text file (useful for Meson builds)
66+ python -m DiscoverVersion --write-to version.txt --plain
67+
68+ # Specify a fallback version if discovery fails
69+ python -m DiscoverVersion --fallback 0.0.0
70+
71+ # Disable specific discovery methods
72+ python -m DiscoverVersion --no-git --no-env
73+ ```
74+
75+ ### CI/CD Example (GitHub Actions)
76+
77+ Here's an example of using DiscoverVersion in a GitHub Actions workflow with
78+ ` cibuildwheel ` :
79+
80+ ``` yaml
81+ - name : Get version from DiscoverVersion
82+ id : get_version
83+ run : |
84+ pip install DiscoverVersion
85+ echo "version=$(python -m DiscoverVersion)" >> $GITHUB_OUTPUT
86+
87+ - name : Build wheels
88+ uses : pypa/cibuildwheel@v2
89+ env :
90+ CIBW_ENVIRONMENT : DISCOVER_VERSION=${{ steps.get_version.outputs.version }}
91+ ` ` `
92+
4093## Tests
4194
4295Before being able to run tests, you need to execute
0 commit comments