1
0
Fork 0
mirror of synced 2024-05-01 11:13:27 +12:00

github release on tag

This commit is contained in:
Nikita Prokopov 2021-11-06 18:11:55 +01:00
parent 776770e5f1
commit 4905eb5151
5 changed files with 116 additions and 31 deletions

View file

@ -1,14 +1,27 @@
on: [push]
on:
push:
branches:
- 'master'
tags:
- '[0-9]+.[0-9]+'
paths:
- '.github/workflows/**'
- 'FiraCode.glyphs'
- 'script/**'
jobs:
build:
runs-on: ubuntu-latest
container: tonsky/firacode:latest
steps:
- uses: actions/checkout@v2
- run: echo ${GITHUB_REF#refs/heads/}
- run: echo "version=$(./script/update_version.py)" >> $GITHUB_ENV
- if: startsWith(github.ref, 'refs/tags/')
run: python3 ./script/update_version.py
- run: ./script/build.sh
- run: echo "hash=$(git rev-parse --short $GITHUB_SHA)" >> $GITHUB_ENV
- uses: actions/upload-artifact@v2
with:
name: Fira_Code_${{ env.version }}
path: distr
name: Fira_Code_${{ env.hash }}
path: distr
- if: startsWith(github.ref, 'refs/tags/')
run: python3 ./script/release.py

17
script/common.py Normal file
View file

@ -0,0 +1,17 @@
#! /usr/bin/env python3
import argparse, os, re, subprocess
root = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))
def version():
parser = argparse.ArgumentParser()
parser.add_argument('--version')
(args, _) = parser.parse_known_args()
if args.version:
return args.version
ref = os.getenv('GITHUB_REF')
if ref and ref.startswith('refs/tags/'):
return ref[len('refs/tags/'):]
raise Exception("Cant identify version")

View file

@ -1,7 +0,0 @@
#!/bin/bash -euo pipefail
cd "`dirname $0`/.."
npm publish
# Update https://github.com/Homebrew/homebrew-cask-fonts
# Update https://github.com/matthewjberger/scoop-nerd-fonts/blob/master/bucket/FiraCode.json

74
script/release.py Executable file
View file

@ -0,0 +1,74 @@
#! /usr/bin/env python3
import argparse, base64, common, glob, os, platform, re, subprocess, sys, urllib.request, zipfile
def log_errors(name):
def wrap(f):
def result(*args, **kwargs):
try:
f(*args, **kwargs)
except Exception as e:
print(f"{name}: Failed {e}")
return result
return wrap
def package(version):
zip = f"Fira_Code_v{version}.zip"
print('Package:', zip)
with zipfile.ZipFile(zip, 'w', compression = zipfile.ZIP_DEFLATED, compresslevel = 9) as archive:
for f in glob.glob("distr/**", recursive = True):
arcname = f[len("distr/"):]
if arcname and not os.path.basename(arcname).startswith("."):
archive.write(f, arcname)
def github_headers():
if os.environ.get('GITHUB_BASIC'):
auth = 'Basic ' + base64.b64encode(os.environ.get('GITHUB_BASIC').encode('utf-8')).decode('utf-8')
else:
auth = 'token ' + os.environ.get('API_TOKEN')
return {
'Accept': 'application/vnd.github.v3+json',
'Authorization': auth
}
@log_errors("github_release")
def github_release(version):
zip = f"Fira_Code_v{version}.zip"
data = '{"tag_name":"' + version + '","name":"' + version + '"}'
headers = github_headers()
resp = urllib.request.urlopen(urllib.request.Request('https://api.github.com/repos/tonsky/FiraCode/releases', data=data.encode('utf-8'), headers=headers)).read()
upload_url = re.match('https://.*/assets', json.loads(resp.decode('utf-8'))['upload_url']).group(0)
print('github_release: Uploading', zip, 'to', upload_url)
headers['Content-Type'] = 'application/zip'
headers['Content-Length'] = os.path.getsize(zip)
with open(zip, 'rb') as data:
urllib.request.urlopen(urllib.request.Request(upload_url + '?name=' + zip, data=data, headers=headers))
@log_errors("npm_publish")
def npm_publish(version):
print("npm_publish: Skip")
@log_errors("update_homebrew")
def update_homebrew(version):
print("update_homebrew: Skip") # Update https://github.com/Homebrew/homebrew-cask-fonts
@log_errors("update_scoop")
def update_scoop(version):
print("update_scoop: Skip") # Update https://github.com/matthewjberger/scoop-nerd-fonts/blob/master/bucket/FiraCode.json
@log_errors("update_google_fonts")
def update_google_fonts(version):
print("update_google_fonts: Skip")
if __name__ == '__main__':
os.chdir(common.root)
version = common.version()
package(version)
github_release(version)
npm_publish(version)
update_homebrew(version)
update_scoop(version)
update_google_fonts(version)
sys.exit(0)

View file

@ -1,19 +1,8 @@
#! /usr/bin/env python3
import common, os, re, subprocess, sys
import os, re, subprocess, sys
def version():
desc = subprocess.check_output(["git", "describe", "--tags"], cwd = os.path.dirname(__file__)).decode("utf-8")
match = re.match(r"([0-9]+)\.([0-9]+)-([0-9]+)-([a-z0-9]+)", desc)
if match:
major = int(match.group(1))
minor = int(match.group(2)) + int(match.group(3))
sha = match.group(4)
return (major, minor, sha)
else:
raise Exception("Cant parse version from: " + desc)
def update_version(major, minor, src, dst):
def update_version(major, minor, src):
print(f"update_version: {major}.{minor} in {src}")
with open(src, 'r') as f:
contents = f.read()
contents = re.sub(r"versionMajor\s+=\s+\d+;",
@ -22,12 +11,11 @@ def update_version(major, minor, src, dst):
contents = re.sub(r"versionMinor\s+=\s+\d+;",
f"versionMinor = {minor};",
contents)
with open(dst, 'w') as f:
with open(src, 'w') as f:
f.write(contents)
if __name__ == '__main__':
os.chdir(os.path.abspath(os.path.dirname(__file__) + '/..'))
(major, minor, sha) = version()
update_version(major, minor, 'FiraCode.glyphs', 'FiraCode.glyphs')
print(f"{major}.{minor}-{sha}")
os.chdir(common.root)
(major, minor) = common.version().split(".")
update_version(major, minor, 'FiraCode.glyphs')
sys.exit(0)