diff --git a/.github/workflows/build-on-publish-release.yaml b/.github/workflows/build-on-publish-release.yaml index ede6b02d..710e1e62 100644 --- a/.github/workflows/build-on-publish-release.yaml +++ b/.github/workflows/build-on-publish-release.yaml @@ -28,6 +28,12 @@ jobs: run: |- gcloud config get-value project + - name: Update package version + run: | + sed -i 's/__version__ = .*/__version__ = "${{github.ref_name}}"/g' holmes/__init__.py + sed -i 's/0.0.0/${{github.ref_name}}/g' helm/holmes/Chart.yaml helm/holmes/values.yaml + sed -i 's/0.0.1/${{github.ref_name}}/g' helm/holmes/Chart.yaml + - name: Set up Docker Buildx uses: docker/setup-buildx-action@v1 @@ -41,11 +47,6 @@ jobs: build-args: | BUILDKIT_INLINE_CACHE=1 - - name: Update package version - run: | - sed -i 's/0.0.0/${{github.ref_name}}/g' helm/holmes/Chart.yaml helm/holmes/values.yaml - sed -i 's/0.0.1/${{github.ref_name}}/g' helm/holmes/Chart.yaml - - name: Save artifact with helm chart uses: actions/upload-artifact@v2 with: diff --git a/.github/workflows/build-on-release.yaml b/.github/workflows/build-on-release.yaml index b944eedb..14d916a3 100644 --- a/.github/workflows/build-on-release.yaml +++ b/.github/workflows/build-on-release.yaml @@ -30,6 +30,23 @@ jobs: if: matrix.os == 'ubuntu-latest' run: | sudo apt-get install -y binutils + + - name: Update package version (Linux) + if: matrix.os == 'ubuntu-latest' + run: sed -i 's/__version__ = .*/__version__ = "${{ github.ref_name }}"/g' holmes/__init__.py + + # mac has BSD style sed command where you specify -i '' and not just -i + - name: Update package version (macOS) + if: matrix.os == 'macos-latest' + run: sed -i '' 's/__version__ = .*/__version__ = "${{ github.ref_name }}"/g' holmes/__init__.py + + # windows has no sed, so we use powershell + - name: Update package version (Windows) + if: matrix.os == 'windows-latest' + run: | + $filePath = 'holmes/__init__.py' + (Get-Content $filePath) -replace '__version__ = .+', '__version__ = "${{ github.ref_name }}"' | Set-Content $filePath + shell: pwsh - name: Build with PyInstaller shell: bash diff --git a/holmes.py b/holmes.py index 81403152..0caa9af2 100644 --- a/holmes.py +++ b/holmes.py @@ -16,7 +16,7 @@ from holmes.config import Config, LLMType from holmes.plugins.destinations import DestinationType from holmes.plugins.prompts import load_prompt -from holmes import __version__ +from holmes import get_version app = typer.Typer(add_completion=False, pretty_exceptions_show_locals=False) investigate_app = typer.Typer( @@ -337,7 +337,7 @@ def jira( @app.command() def version() -> None: - typer.echo(__version__) + typer.echo(get_version()) if __name__ == "__main__": app() diff --git a/holmes/__init__.py b/holmes/__init__.py index 50107534..4a50a454 100644 --- a/holmes/__init__.py +++ b/holmes/__init__.py @@ -1,5 +1,32 @@ -# For relative imports to work in Python 3.6 -# See https://stackoverflow.com/a/49375740 -import os, sys; sys.path.append(os.path.dirname(os.path.realpath(__file__))) +import os +import subprocess +import sys -__version__ = "0.2.1" +# For relative imports to work in Python 3.6 - see https://stackoverflow.com/a/49375740 +sys.path.append(os.path.dirname(os.path.realpath(__file__))) + +# This is patched by github actions during release +__version__ = "0.0.0" + + +def get_version() -> str: + # the version string was patched by a release - return __version__ which will be correct + if not __version__.startswith("0.0.0"): + return __version__ + + # we are running from an unreleased dev version + try: + # Get the latest git tag + tag = subprocess.check_output(["git", "describe", "--tags"]).decode().strip() + + # Get the current branch name + branch = subprocess.check_output(["git", "rev-parse", "--abbrev-ref", "HEAD"]).decode().strip() + + # Check if there are uncommitted changes + status = subprocess.check_output(["git", "status", "--porcelain"]).decode().strip() + dirty = "-dirty" if status else "" + + return f"{tag}-{branch}{dirty}" + + except Exception: + return f"dev-version"