Skip to content

Commit

Permalink
Add Release workflow (#80)
Browse files Browse the repository at this point in the history
* add pep 517 build

* add release workflow

* add step to publish to pypi

* add some comments to the release action

* Limit who can run the release action
  • Loading branch information
CaselIT authored Apr 29, 2021
1 parent 07570c5 commit 116dfdc
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 1 deletion.
103 changes: 103 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
name: Release to pypi

on:
workflow_dispatch:
inputs:
version:
description: 'Version to use. Leave default to use the current version'
required: true
default: '~~version~~'


env:
# comment TWINE_REPOSITORY_URL to use the real pypi. NOTE: change also the secret used in TWINE_PASSWORD
# TWINE_REPOSITORY_URL: https://test.pypi.org/legacy/

jobs:
release:
name: Release
if: github.actor == 'CaselIT' || github.actor == 'zzzeek'
runs-on: "ubuntu-latest"

steps:
- name: Checkout repo
uses: actions/checkout@v2

- name: Set up python
uses: actions/setup-python@v2
with:
python-version: "3.9"

- name: Set version
# A specific version was set in the action trigger.
# Change the version in setup.cfg to that version
if: ${{ github.event.inputs.version != '~~version~~' }}
run: |
python .github/workflows/scripts/update_version.py --set-version ${{ github.event.inputs.version }}
- name: Commit version change
# If the setup.cfg version was changed commit it.
if: ${{ github.event.inputs.version != '~~version~~' }}
uses: stefanzweifel/git-auto-commit-action@v4
with:
commit_message: Version ${{ github.event.inputs.version }}
file_pattern: setup.cfg

- name: Get version
# Get current version
id: get-version
run: |
version=`grep 'version =' setup.cfg | awk '{print $NF}'`
echo $version
echo "::set-output name=version::$version"
- name: Create distribution
# Create wheel and sdist
run: |
python -m pip install --upgrade pip
pip --version
pip install build
pip list
python -m build --sdist --wheel .
- name: Create release
# Create github tag and release and upload the distribution wheels and sdist
uses: softprops/action-gh-release@v1
with:
body: Release ${{ steps.get-version.outputs.version }}
files: dist/*
name: 'v${{ steps.get-version.outputs.version }}'
tag_name: v${{ steps.get-version.outputs.version }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Next version
# Update the version to the next version
run: |
python .github/workflows/scripts/update_version.py --update-version
- name: Get version
# Get new current version
id: get-new-version
run: |
version=`grep 'version =' setup.cfg | awk '{print $NF}'`
echo $version
echo "::set-output name=version::$version"
- name: Commit next version update
# Commit new current version
uses: stefanzweifel/git-auto-commit-action@v4
with:
commit_message: Start work on ${{ steps.get-new-version.outputs.version }}
file_pattern: setup.cfg

- name: Publish distribution
# Publish to pypi
env:
TWINE_USERNAME: __token__
# replace TWINE_PASSWORD with token for real pypi
# TWINE_PASSWORD: ${{ secrets.test_pypi_token }}
TWINE_PASSWORD: ${{ secrets.pypi_token }}
run: |
pip install -U twine
twine upload --skip-existing dist/*
82 changes: 82 additions & 0 deletions .github/workflows/scripts/update_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import argparse
import pathlib
import re


def is_canonical(version):
# from https://www.python.org/dev/peps/pep-0440/
return (
re.match(
r"^([1-9][0-9]*!)?(0|[1-9][0-9]*)(\.(0|[1-9][0-9]*))*((a|b|rc)(0|"
r"[1-9][0-9]*))?(\.post(0|[1-9][0-9]*))?(\.dev(0|[1-9][0-9]*))?$",
version,
)
is not None
)


def next_version(current_version: str):
major, minor, point = current_version.split(".")
if point.isdecimal():
# 1.2.3 -> 1.2.4
next = int(point) + 1
return f"{major}.{minor}.{next}"
if not point[-1].isdecimal():
# 0.1.2b -> 0.1.2b1
return f"{major}.{minor}.{point}1"
# 0.1.2b1 -> 0.1.2b2
match = re.match(r"(\d+\D*)(\d+)", point)
if not match:
raise RuntimeError(
f"Cannot decode the current version {current_version}"
)
base, current = match.groups()
print(base, current)
next = int(current) + 1
new_point = f"{base}{next}"
return f"{major}.{minor}.{new_point}"


def go(args):
if args.set_version and not is_canonical(args.set_version):
raise RuntimeError(f"Cannot use {args.set_version!r} as version")

setup = pathlib.Path("setup.cfg")
setup_text = setup.read_text()
new_setup_text = []
found = False
for line in setup_text.split("\n"):
if not line.startswith("version = "):
new_setup_text.append(line)
continue
if found:
raise RuntimeError(
"Multiple lines starting with 'version =' found"
)
if args.set_version:
new_setup_text.append(f"version = {args.set_version}")
version = args.set_version
else:
current_version = line.split(" ")[-1]
version = next_version(current_version)
new_setup_text.append(f"version = {version}")
found = True
if not found:
raise RuntimeError("No line found starting with 'version ='")

setup.write_text("\n".join(new_setup_text))
print("Updated version to", version)


if __name__ == "__main__":
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument("--set-version", help="Version to set")
group.add_argument(
"--update-version",
help="Take the current version and update it",
action="store_true",
)

args = parser.parse_args()
go(args)
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
.vscode/
dist/
*.egg-info

build/

sqla
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
[build-system]
requires = ['setuptools >= 44', 'wheel']
build-backend = 'setuptools.build_meta'

[tool.black]
line-length = 79
target-version = ['py36']

0 comments on commit 116dfdc

Please sign in to comment.