From 5afe42a081c2da97abbfb7658e5996d0d79be804 Mon Sep 17 00:00:00 2001 From: Florian Wilhelm Date: Thu, 14 Dec 2023 15:30:53 +0100 Subject: [PATCH 1/4] Release Workflow --- .github/workflows/build.yml | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 745d270..f85ff37 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -1,4 +1,11 @@ -on: push +on: + push: + workflow_dispatch: + inputs: + component: + description: 'Version component to increment (major or minor)' + required: false + default: 'minor' jobs: build: runs-on: ubuntu-latest @@ -33,6 +40,7 @@ jobs: steps: - uses: actions/checkout@v3 - name: tag latest + if: github.event.inputs.component == '' run: | git tag --force latest git push --force origin latest @@ -40,7 +48,27 @@ jobs: with: name: build path: download - - name: create release + - run: echo Version Component to Increase is ${{ github.event.inputs.component }} + - name: create release (latest) + if: github.event.inputs.component == '' run: | release="$(.github/workflows/release.sh ${{ secrets.GITHUB_TOKEN }} ${{ github.repository }} create latest Builder)" .github/workflows/release.sh ${{ secrets.GITHUB_TOKEN }} ${{ github.repository }} upload "$release" download/build + - name: Get Version Number + if: github.event.inputs.component != '' + uses: gardenlinux/bump-version@main + id: bump + with: + component: ${{ github.event.inputs.component }} + - run: echo New version number ${{ steps.bump.outputs.newVersion }} + if: github.event.inputs.component != '' + - name: tag version + if: github.event.inputs.component != '' + run: | + git tag ${{ steps.bump.outputs.newVersion }} + git push origin ${{ steps.bump.outputs.newVersion }} + - name: create release (semantic) + if: github.event.inputs.component != '' + run: | + release="$(.github/workflows/release.sh ${{ secrets.GITHUB_TOKEN }} ${{ github.repository }} create ${{ steps.bump.outputs.newVersion }} Builder)" + .github/workflows/release.sh ${{ secrets.GITHUB_TOKEN }} ${{ github.repository }} upload "$release" download/build From 7dc14cf08f3e5e3a90e3286eb8dd8ddd186dcdb1 Mon Sep 17 00:00:00 2001 From: Florian Wilhelm Date: Mon, 18 Dec 2023 13:37:03 +0100 Subject: [PATCH 2/4] new approach with local script --- .github/workflows/build.yml | 47 +++++++++++------- .github/workflows/bump.py | 96 +++++++++++++++++++++++++++++++++++++ 2 files changed, 126 insertions(+), 17 deletions(-) create mode 100755 .github/workflows/bump.py diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index f85ff37..ac8ca2f 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -1,11 +1,15 @@ +name: Build and Release on: push: workflow_dispatch: inputs: component: - description: 'Version component to increment (major or minor)' + description: 'Version component to increment (Use *minor* unless we have breaking changes)' required: false - default: 'minor' + type: choice + options: + - minor + - major jobs: build: runs-on: ubuntu-latest @@ -33,14 +37,15 @@ jobs: with: name: build path: build - release: + + # Run for new commits on the main branch + release-latest: runs-on: ubuntu-latest needs: build - if: github.ref == 'refs/heads/main' + if: github.ref == 'refs/heads/main' && github.event.inputs.component == '' steps: - uses: actions/checkout@v3 - name: tag latest - if: github.event.inputs.component == '' run: | git tag --force latest git push --force origin latest @@ -48,27 +53,35 @@ jobs: with: name: build path: download - - run: echo Version Component to Increase is ${{ github.event.inputs.component }} - - name: create release (latest) - if: github.event.inputs.component == '' + - name: create release run: | - release="$(.github/workflows/release.sh ${{ secrets.GITHUB_TOKEN }} ${{ github.repository }} create latest Builder)" + release="$(.github/workflows/release.sh ${{ secrets.GITHUB_TOKEN }} ${{ github.repository }} create latest "Builder (latest)")" .github/workflows/release.sh ${{ secrets.GITHUB_TOKEN }} ${{ github.repository }} upload "$release" download/build + + # Run for new intentional versions, bumping the major or minor version + release-semver: + runs-on: ubuntu-latest + permissions: write-all + needs: build + if: github.ref == 'refs/heads/main' && github.event.inputs.component != '' + steps: + - uses: actions/checkout@v3 + with: + fetch-depth: 0 + - uses: actions/download-artifact@v3 + with: + name: build + path: download + - run: echo Version Component to Increase is ${{ github.event.inputs.component }} - name: Get Version Number - if: github.event.inputs.component != '' - uses: gardenlinux/bump-version@main + run: .github/workflows/bump.py ${{ github.event.inputs.component }} id: bump - with: - component: ${{ github.event.inputs.component }} - run: echo New version number ${{ steps.bump.outputs.newVersion }} - if: github.event.inputs.component != '' - name: tag version - if: github.event.inputs.component != '' run: | git tag ${{ steps.bump.outputs.newVersion }} git push origin ${{ steps.bump.outputs.newVersion }} - name: create release (semantic) - if: github.event.inputs.component != '' run: | - release="$(.github/workflows/release.sh ${{ secrets.GITHUB_TOKEN }} ${{ github.repository }} create ${{ steps.bump.outputs.newVersion }} Builder)" + release="$(.github/workflows/release.sh ${{ secrets.GITHUB_TOKEN }} ${{ github.repository }} create ${{ steps.bump.outputs.newVersion }} "Builder (${{ steps.bump.outputs.newVersion }})")" .github/workflows/release.sh ${{ secrets.GITHUB_TOKEN }} ${{ github.repository }} upload "$release" download/build diff --git a/.github/workflows/bump.py b/.github/workflows/bump.py new file mode 100755 index 0000000..cc47d07 --- /dev/null +++ b/.github/workflows/bump.py @@ -0,0 +1,96 @@ +#!/usr/bin/python + +""" +Determine next version number for versions of a schema like v1.0 +based on existing git tags and which component to bump (minor/major). +""" + +import subprocess +import re +import sys +import os + + +def convert_version_to_sortable_int(major, minor): + return major * 1000 + minor + + +def determine_most_recent_existing_version(): + tags = subprocess.run(["git", "tag"], capture_output=True).stdout.splitlines() + + versions = [] + + for t in tags: + tag = t.decode() + if re.match(r"v[0-9]+.[0-9]+", tag): + tag_without_prefix = tag[1:] + components = tag_without_prefix.split(".") + assert len(components) == 2 + major_int = int(components[0]) + minor_int = int(components[1]) + versions.append( + { + "tag": tag, + "sortNumber": convert_version_to_sortable_int(major_int, minor_int), + "major": major_int, + "minor": minor_int, + } + ) + + if len(versions) == 0: + print("No existing versions found") + return { + "tag": "v0.0", + "sortNumber": convert_version_to_sortable_int(0, 0), + "major": 0, + "minor": 0, + } + + def keyToSortVersions(v): + return v["sortNumber"] + + versions.sort(key=keyToSortVersions, reverse=True) + print(f"Sorted list of versions: {versions}") + highest_existing_version_number = versions[0] + + return highest_existing_version_number + + +def bump(most_recent_version, component_to_bump): + new_version = "" + + if component_to_bump == "major": + new_major = most_recent_version["major"] + 1 + new_version = f"v{new_major}.0" + elif component_to_bump == "minor": + new_minor = most_recent_version["minor"] + 1 + major = most_recent_version["major"] + new_version = f"v{major}.{new_minor}" + else: + raise ( + f"Invalid component provided: {component_to_bump}, only major or minor are supported." + ) + + return new_version + + +def determine_component_to_bump(): + if sys.argv[1] not in ["major", "minor"]: + raise ("Usage: bump.py (major|minor)") + return sys.argv[1] + + +def main(): + component_to_bump = determine_component_to_bump() + most_recent_version = determine_most_recent_existing_version() + new_version = bump(most_recent_version, component_to_bump) + + if os.getenv("GITHUB_OUTPUT"): + with open(os.environ["GITHUB_OUTPUT"], "a") as file_handle: + print(f"newVersion={new_version}", file=file_handle) + else: + print(f"No GitHub env found. New version is {new_version}") + + +if __name__ == "__main__": + main() From 77b9b667cf148c9d04cd86ad5d56ff88ec5cfd94 Mon Sep 17 00:00:00 2001 From: Florian Wilhelm Date: Mon, 18 Dec 2023 13:39:29 +0100 Subject: [PATCH 3/4] permissions --- .github/workflows/build.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index ac8ca2f..510a67e 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -61,7 +61,6 @@ jobs: # Run for new intentional versions, bumping the major or minor version release-semver: runs-on: ubuntu-latest - permissions: write-all needs: build if: github.ref == 'refs/heads/main' && github.event.inputs.component != '' steps: From bca9e4220a383b2f443573efac658ebbde16db68 Mon Sep 17 00:00:00 2001 From: Florian Wilhelm Date: Mon, 18 Dec 2023 14:29:38 +0100 Subject: [PATCH 4/4] fix naming --- .github/workflows/build.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 510a67e..5b46954 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -59,7 +59,7 @@ jobs: .github/workflows/release.sh ${{ secrets.GITHUB_TOKEN }} ${{ github.repository }} upload "$release" download/build # Run for new intentional versions, bumping the major or minor version - release-semver: + release-new-version: runs-on: ubuntu-latest needs: build if: github.ref == 'refs/heads/main' && github.event.inputs.component != '' @@ -80,7 +80,7 @@ jobs: run: | git tag ${{ steps.bump.outputs.newVersion }} git push origin ${{ steps.bump.outputs.newVersion }} - - name: create release (semantic) + - name: create release (new version) run: | release="$(.github/workflows/release.sh ${{ secrets.GITHUB_TOKEN }} ${{ github.repository }} create ${{ steps.bump.outputs.newVersion }} "Builder (${{ steps.bump.outputs.newVersion }})")" .github/workflows/release.sh ${{ secrets.GITHUB_TOKEN }} ${{ github.repository }} upload "$release" download/build