Skip to content

Commit 116dfdc

Browse files
authored
Add Release workflow (#80)
* 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
1 parent 07570c5 commit 116dfdc

File tree

4 files changed

+190
-1
lines changed

4 files changed

+190
-1
lines changed

.github/workflows/release.yaml

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
name: Release to pypi
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
version:
7+
description: 'Version to use. Leave default to use the current version'
8+
required: true
9+
default: '~~version~~'
10+
11+
12+
env:
13+
# comment TWINE_REPOSITORY_URL to use the real pypi. NOTE: change also the secret used in TWINE_PASSWORD
14+
# TWINE_REPOSITORY_URL: https://test.pypi.org/legacy/
15+
16+
jobs:
17+
release:
18+
name: Release
19+
if: github.actor == 'CaselIT' || github.actor == 'zzzeek'
20+
runs-on: "ubuntu-latest"
21+
22+
steps:
23+
- name: Checkout repo
24+
uses: actions/checkout@v2
25+
26+
- name: Set up python
27+
uses: actions/setup-python@v2
28+
with:
29+
python-version: "3.9"
30+
31+
- name: Set version
32+
# A specific version was set in the action trigger.
33+
# Change the version in setup.cfg to that version
34+
if: ${{ github.event.inputs.version != '~~version~~' }}
35+
run: |
36+
python .github/workflows/scripts/update_version.py --set-version ${{ github.event.inputs.version }}
37+
38+
- name: Commit version change
39+
# If the setup.cfg version was changed commit it.
40+
if: ${{ github.event.inputs.version != '~~version~~' }}
41+
uses: stefanzweifel/git-auto-commit-action@v4
42+
with:
43+
commit_message: Version ${{ github.event.inputs.version }}
44+
file_pattern: setup.cfg
45+
46+
- name: Get version
47+
# Get current version
48+
id: get-version
49+
run: |
50+
version=`grep 'version =' setup.cfg | awk '{print $NF}'`
51+
echo $version
52+
echo "::set-output name=version::$version"
53+
54+
- name: Create distribution
55+
# Create wheel and sdist
56+
run: |
57+
python -m pip install --upgrade pip
58+
pip --version
59+
pip install build
60+
pip list
61+
python -m build --sdist --wheel .
62+
63+
- name: Create release
64+
# Create github tag and release and upload the distribution wheels and sdist
65+
uses: softprops/action-gh-release@v1
66+
with:
67+
body: Release ${{ steps.get-version.outputs.version }}
68+
files: dist/*
69+
name: 'v${{ steps.get-version.outputs.version }}'
70+
tag_name: v${{ steps.get-version.outputs.version }}
71+
env:
72+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
73+
74+
- name: Next version
75+
# Update the version to the next version
76+
run: |
77+
python .github/workflows/scripts/update_version.py --update-version
78+
79+
- name: Get version
80+
# Get new current version
81+
id: get-new-version
82+
run: |
83+
version=`grep 'version =' setup.cfg | awk '{print $NF}'`
84+
echo $version
85+
echo "::set-output name=version::$version"
86+
87+
- name: Commit next version update
88+
# Commit new current version
89+
uses: stefanzweifel/git-auto-commit-action@v4
90+
with:
91+
commit_message: Start work on ${{ steps.get-new-version.outputs.version }}
92+
file_pattern: setup.cfg
93+
94+
- name: Publish distribution
95+
# Publish to pypi
96+
env:
97+
TWINE_USERNAME: __token__
98+
# replace TWINE_PASSWORD with token for real pypi
99+
# TWINE_PASSWORD: ${{ secrets.test_pypi_token }}
100+
TWINE_PASSWORD: ${{ secrets.pypi_token }}
101+
run: |
102+
pip install -U twine
103+
twine upload --skip-existing dist/*
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import argparse
2+
import pathlib
3+
import re
4+
5+
6+
def is_canonical(version):
7+
# from https://www.python.org/dev/peps/pep-0440/
8+
return (
9+
re.match(
10+
r"^([1-9][0-9]*!)?(0|[1-9][0-9]*)(\.(0|[1-9][0-9]*))*((a|b|rc)(0|"
11+
r"[1-9][0-9]*))?(\.post(0|[1-9][0-9]*))?(\.dev(0|[1-9][0-9]*))?$",
12+
version,
13+
)
14+
is not None
15+
)
16+
17+
18+
def next_version(current_version: str):
19+
major, minor, point = current_version.split(".")
20+
if point.isdecimal():
21+
# 1.2.3 -> 1.2.4
22+
next = int(point) + 1
23+
return f"{major}.{minor}.{next}"
24+
if not point[-1].isdecimal():
25+
# 0.1.2b -> 0.1.2b1
26+
return f"{major}.{minor}.{point}1"
27+
# 0.1.2b1 -> 0.1.2b2
28+
match = re.match(r"(\d+\D*)(\d+)", point)
29+
if not match:
30+
raise RuntimeError(
31+
f"Cannot decode the current version {current_version}"
32+
)
33+
base, current = match.groups()
34+
print(base, current)
35+
next = int(current) + 1
36+
new_point = f"{base}{next}"
37+
return f"{major}.{minor}.{new_point}"
38+
39+
40+
def go(args):
41+
if args.set_version and not is_canonical(args.set_version):
42+
raise RuntimeError(f"Cannot use {args.set_version!r} as version")
43+
44+
setup = pathlib.Path("setup.cfg")
45+
setup_text = setup.read_text()
46+
new_setup_text = []
47+
found = False
48+
for line in setup_text.split("\n"):
49+
if not line.startswith("version = "):
50+
new_setup_text.append(line)
51+
continue
52+
if found:
53+
raise RuntimeError(
54+
"Multiple lines starting with 'version =' found"
55+
)
56+
if args.set_version:
57+
new_setup_text.append(f"version = {args.set_version}")
58+
version = args.set_version
59+
else:
60+
current_version = line.split(" ")[-1]
61+
version = next_version(current_version)
62+
new_setup_text.append(f"version = {version}")
63+
found = True
64+
if not found:
65+
raise RuntimeError("No line found starting with 'version ='")
66+
67+
setup.write_text("\n".join(new_setup_text))
68+
print("Updated version to", version)
69+
70+
71+
if __name__ == "__main__":
72+
parser = argparse.ArgumentParser()
73+
group = parser.add_mutually_exclusive_group(required=True)
74+
group.add_argument("--set-version", help="Version to set")
75+
group.add_argument(
76+
"--update-version",
77+
help="Take the current version and update it",
78+
action="store_true",
79+
)
80+
81+
args = parser.parse_args()
82+
go(args)

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
.vscode/
66
dist/
77
*.egg-info
8-
8+
build/
99

1010
sqla

pyproject.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
[build-system]
2+
requires = ['setuptools >= 44', 'wheel']
3+
build-backend = 'setuptools.build_meta'
4+
15
[tool.black]
26
line-length = 79
37
target-version = ['py36']

0 commit comments

Comments
 (0)