diff --git a/.github/workflows/release.py b/.github/workflows/release.py new file mode 100644 index 0000000..54b6bcb --- /dev/null +++ b/.github/workflows/release.py @@ -0,0 +1,61 @@ +#!/usr/bin/env python3 +import os +import re +import subprocess +import shlex + +MAIN_FILE = 'main.go' +VERSION_LINE = re.compile('version = "([0-9]+.[0-9]+.[0-9]+)"') + + +def sh(cmd: str, env: dict | None = None) -> str: + cmd_parsed = shlex.split(cmd) + output = subprocess.run(cmd_parsed, check=True, text=True, capture_output=True, env=env) + return output.stdout.strip() + + +def get_current_version() -> str: + with open(MAIN_FILE) as fd: + for line in fd: + if m := VERSION_LINE.match(line.strip()): + version = m.group(1) + return version if version.startswith('v') else 'v' + version + + raise Exception('Unable to determine version') + + +def tag(new_tag: str): + sh(f'git tag {new_tag}') + sh('git push --tags') + + +def create_release(version: str): + sh(f'gh release create -n "Release {version}" -t "{version}" "version"') + + +def release(version: str): + repo_name = os.environ['GITHUB_REPOSITORY'].split('/')[-1] + platform = sh('uname') + architecture = sh('uname -m') + asset_name = f'{repo_name}-{version}-{platform}-{architecture}'.lower() + sh(f'go build -o {asset_name}', env={'CGO_ENABLED': '0'}) + sh(f'gh release upload "{version}" "{asset_name}"') + + +def tag_and_release(): + version = get_current_version() + versions = set(sh('git tag').split('\n')) + if version in versions: + return + + tag(version) + create_release(version) + release(version) + + +def main(): + tag_and_release() + + +if __name__ == '__main__': + main() diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..ec24089 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,19 @@ +name: Release new version + +on: + push: + branches: + - main + workflow_dispatch: + inputs: + +jobs: + release: + name: Release new version if no tag exists for it + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - name: Create new tag and release + run: | + python3 .github/workflows/release.py