Skip to content

Commit

Permalink
Allow specifying version directly during build
Browse files Browse the repository at this point in the history
Sometimes the Git metadata is not available during build. In this case, we can rely on externally specified version using the `KARAPACE_VERSION` environment variable.
  • Loading branch information
ivanyu committed Nov 21, 2022
1 parent bc0983a commit 2549e3e
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions version.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,23 @@ def get_project_version(version_file: str) -> str:
module = importlib.util.module_from_spec(module_spec)
file_ver = getattr(module, "__version__", None)

os.chdir(os.path.dirname(__file__) or ".")
try:
git_out = subprocess.check_output(
["git", "describe", "--always", "--tags"], stderr=getattr(subprocess, "DEVNULL", None)
)
except (OSError, subprocess.CalledProcessError):
pass
else:
git_ver = git_out.splitlines()[0].strip().decode("utf-8")
if "." not in git_ver:
git_ver = "0.0.1-0-unknown-{}".format(git_ver)
if save_version(git_ver, file_ver, version_file):
return git_ver
version = os.getenv("KARAPACE_VERSION")
if version is None:
os.chdir(os.path.dirname(__file__) or ".")
try:
git_out = subprocess.check_output(
["git", "describe", "--always", "--tags"], stderr=getattr(subprocess, "DEVNULL", None)
)
except (OSError, subprocess.CalledProcessError):
pass
else:
git_ver = git_out.splitlines()[0].strip().decode("utf-8")
if "." not in git_ver:
git_ver = "0.0.1-0-unknown-{}".format(git_ver)
version = git_ver

if save_version(version, file_ver, version_file):
return version

if not file_ver:
raise Exception("version not available from git or from file {!r}".format(version_file))
Expand Down

0 comments on commit 2549e3e

Please sign in to comment.