-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |