Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release Workflow #47

Merged
merged 4 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 44 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
on: push
name: Build and Release
on:
push:
workflow_dispatch:
inputs:
component:
description: 'Version component to increment (Use *minor* unless we have breaking changes)'
required: false
type: choice
options:
- minor
- major
jobs:
build:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -26,10 +37,12 @@ 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
Expand All @@ -42,5 +55,32 @@ jobs:
path: download
- 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-new-version:
runs-on: ubuntu-latest
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
run: .github/workflows/bump.py ${{ github.event.inputs.component }}
id: bump
- run: echo New version number ${{ steps.bump.outputs.newVersion }}
- name: tag version
run: |
git tag ${{ steps.bump.outputs.newVersion }}
git push origin ${{ steps.bump.outputs.newVersion }}
- 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
96 changes: 96 additions & 0 deletions .github/workflows/bump.py
Original file line number Diff line number Diff line change
@@ -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",
Vincinator marked this conversation as resolved.
Show resolved Hide resolved
"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()
Loading