From f25cbd633bf3f06b33c3a0be893503cfdbcd58c0 Mon Sep 17 00:00:00 2001 From: Nicholas Wiltsie Date: Mon, 29 Jul 2024 16:06:17 -0700 Subject: [PATCH] Get the next version --- .github/workflows/create-release-pr.yaml | 8 +++++- get_next_version.py | 34 ++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 get_next_version.py diff --git a/.github/workflows/create-release-pr.yaml b/.github/workflows/create-release-pr.yaml index 909cac5..5f0fcb6 100644 --- a/.github/workflows/create-release-pr.yaml +++ b/.github/workflows/create-release-pr.yaml @@ -30,7 +30,6 @@ jobs: # sidecar scripts - id: workflow-parsing name: Get SHA of reusuable workflow - shell: bash env: REPO: ${{ github.repository }} RUN_ID: ${{ github.run_id }} @@ -53,6 +52,7 @@ jobs: uses: actions/checkout@v4 with: path: caller + fetch-tags: true - name: Set up python uses: actions/setup-python@v5 @@ -61,3 +61,9 @@ jobs: # Install the semver package - run: pip install semver==3.0.2 + + - run: python reusable/get_next_version.py + env: + REPO_DIR: caller + BUMP_TYPE: ${{ inputs.bump_type }} + EXACT_VERSION: ${{ inputs.exact_version }} diff --git a/get_next_version.py b/get_next_version.py new file mode 100644 index 0000000..c34e66b --- /dev/null +++ b/get_next_version.py @@ -0,0 +1,34 @@ +"Get the next tag version." + +import os +import subprocess +from pathlib import Path + +import semver + + +def get_next_tag(): + "Return the next tag after the appropriate bump type." + repo_dir = os.environ["REPO_DIR"] + bump_type = os.environ["BUMP_TYPE"] + exact_version = os.environ["EXACT_VERSION"] + output_file = Path(os.environ["GITHUB_OUTPUT"]) + + if bump_type == "exact": + return exact_version + + # Get the most recent ancestor tag + last_tag = subprocess.check_output( + ["git", "describe", "--tags", "--abbrev=0"], + cwd=repo_dir + ).decode("utf-8") + + last_version = semver.Version.parse(last_tag) + next_version = last_version.next_version(part=bump_type) + print(f"{last_version} -> {bump_type} -> {next_version}") + + with output_file.open(mode="w", encoding="utf-8") as outfile: + outfile.write(f"next_version={next_version}\n") + +if __name__ == "__main__": + get_next_tag()