Skip to content

Commit

Permalink
add release workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
femnad committed Dec 23, 2023
1 parent 7f04587 commit d6fa732
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
61 changes: 61 additions & 0 deletions .github/workflows/release.py
Original file line number Diff line number Diff line change
@@ -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()
19 changes: 19 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit d6fa732

Please sign in to comment.