Skip to content

Commit 821ccec

Browse files
committed
deployment: ensure proper version lands in tarballs
To ensure we have the proper version-strings in the tar-balls and other install forms, we now create two commits automatically. One which contestants the release-version, where the tag will also be places, and the one containing the dev-increment, as currently. closes #122
1 parent facdc47 commit 821ccec

File tree

1 file changed

+115
-57
lines changed

1 file changed

+115
-57
lines changed

.github/workflows/publish.yaml

+115-57
Original file line numberDiff line numberDiff line change
@@ -7,80 +7,138 @@ name: Upload Python Package
77

88
on:
99
release:
10-
types: [created]
10+
types: published
1111

1212

1313
jobs:
1414

15-
deploy:
15+
check-version: # --------------------------------------------------------------------
1616
runs-on: ubuntu-latest
17+
steps:
18+
- name: Checkout
19+
uses: actions/checkout@v4
20+
21+
- name: get versions
22+
id: get
23+
run: |
24+
echo "NEW_VERSION=$(echo '${{ github.ref_name }}' | cut -c2-)" | tee -a $GITHUB_OUTPUT
25+
echo "OLD_VERSION=$(curl -s https://pypi.org/pypi/readchar/json |jq -r .info.version)" | tee -a $GITHUB_OUTPUT
26+
27+
- name: validate version
28+
id: valid
29+
shell: python
30+
run: |
31+
from sys import exit
32+
from packaging import version
33+
new_version = version.parse("${{ steps.get.outputs.NEW_VERSION }}")
34+
old_version = version.parse("${{ steps.get.outputs.OLD_VERSION }}")
35+
if not new_version > old_version:
36+
print(f"::error::New version '{new_version}' not greatet than '{old_version}'")
37+
exit(1)
1738
39+
outputs:
40+
version: ${{ steps.get.outputs.NEW_VERSION }}
41+
42+
43+
tag: # ------------------------------------------------------------------------------
44+
runs-on: ubuntu-latest
45+
needs: check-version
46+
permissions:
47+
contents: write
48+
env:
49+
VERSION: ${{ needs.check-version.outputs.version }}
1850
steps:
1951
- name: Checkout
20-
uses: actions/checkout@v3
52+
uses: actions/checkout@v4
53+
54+
- name: update pyproject.toml
55+
run: sed -i -r "s/^(version = ).*$/\1\"$VERSION\"/" pyproject.toml
56+
57+
- name: commit version
58+
env:
59+
USER: github-actions[bot]
60+
EMAIL: github-actions[bot]@users.noreply.github.com
61+
run: git -c user.name="$USER" -c user.email="$EMAIL" commit --all -m "release v$VERSION"
62+
63+
- name: update tag
64+
run: git tag -f "v$VERSION"
65+
66+
- name: push updates
67+
run: git push --tags -f
68+
69+
70+
deploy: # ---------------------------------------------------------------------------
71+
runs-on: ubuntu-latest
72+
needs: tag
73+
steps:
74+
- name: Checkout
75+
uses: actions/checkout@v4
2176
with:
22-
fetch-depth: 0
23-
- name: Set env
24-
run: |
25-
echo "RELEASE_TAG=${GITHUB_REF#refs/*/}" | tee -a $GITHUB_ENV
26-
echo "BRANCH=$( \
27-
git branch -r --contains ${GITHUB_REF} \
28-
| grep -v HEAD \
29-
| sed -n 's/ *origin\/\(.*\)/\1/p' \
30-
)" | tee -a $GITHUB_ENV
77+
ref: ${{ github.ref }}
78+
3179
- name: Set up Python
32-
uses: actions/setup-python@v4
80+
uses: actions/setup-python@v5
3381
with:
3482
python-version: '3.x'
3583
cache: pip
84+
3685
- name: Install dependencies
37-
run: |
38-
pip install build twine
39-
- name: get infos from Tag
40-
run: |
41-
echo "RELEASE_TAG=${RELEASE_TAG}"
42-
if [[ $RELEASE_TAG =~ (([0-9]+)\.([0-9]+)\.([0-9]+))([-./]dev([0-9]+))?$ ]]
43-
then
44-
echo "VERSION=${BASH_REMATCH[0]}" | tee -a $GITHUB_ENV
45-
echo "VERSION_MAJOR=${BASH_REMATCH[2]}" | tee -a $GITHUB_ENV
46-
echo "VERSION_MINOR=${BASH_REMATCH[3]}" | tee -a $GITHUB_ENV
47-
echo "VERSION_PATCH=${BASH_REMATCH[4]}" | tee -a $GITHUB_ENV
48-
echo "VERSION_DEV=${BASH_REMATCH[6]}" | tee -a $GITHUB_ENV
49-
else
50-
echo "INVALID_TAG=True" | tee -a $GITHUB_ENV
51-
fi
52-
- name: Fail on invalid Tag
53-
if: ${{ env.INVALID_TAG }}
54-
uses: actions/github-script@v6
55-
with:
56-
script: core.setFailed('Invalid Tag name used with this release!')
86+
run: pip install build twine
5787

58-
- name: Write Version to pyproject.toml
59-
run: |
60-
sed -i "s/version = \".*\"$/version = \"$VERSION\"/" pyproject.toml
6188
- name: Build sdist and bdist_wheel
89+
run: python -m build
90+
91+
- uses: actions/upload-artifact@v3
92+
with:
93+
name: dist
94+
path: dist/
95+
96+
# - name: publish to PyPi
97+
# env:
98+
# TWINE_USERNAME: __token__
99+
# TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }}
100+
# run: twine upload dist/*
101+
102+
103+
increment: # ------------------------------------------------------------------------
104+
runs-on: ubuntu-latest
105+
needs: deploy
106+
steps:
107+
- name: Checkout
108+
uses: actions/checkout@v4
109+
with:
110+
ref: ${{ github.ref }}
111+
fetch-depth: 3
112+
113+
- name: get versions
114+
id: get
115+
shell: python
62116
run: |
63-
python -m build
64-
- name: publish to PyPi
117+
from sys import exit
118+
from os import environ
119+
from packaging import version
120+
ver = version.parse("${{ github.ref_name }}")
121+
if ver.dev is not None:
122+
new_ver = f"{ver.base_version}-dev{ver.dev +1}"
123+
else:
124+
new_ver = f"{ver.major}.{ver.minor}.{ver.micro +1}-dev0"
125+
with open(environ.get("GITHUB_OUTPUT"), "a") as fp:
126+
towrite = f"NEW_VERSION={new_ver}"
127+
print(towrite)
128+
fp.write(towrite + "\n")
129+
130+
- name: update pyproject.toml
65131
env:
66-
TWINE_USERNAME: __token__
67-
TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }}
68-
run: |
69-
twine upload dist/*
132+
VERSION: ${{ steps.get.outputs.NEW_VERSION }}
133+
run: sed -i -r "s/^(version = ).*$/\1\"$VERSION\"/" pyproject.toml
70134

71-
- name: increment development version
72-
if: ${{ env.VERSION_DEV }}
73-
run: |
74-
v=$VERSION_MAJOR.$VERSION_MINOR.$VERSION_PATCH-dev$((VERSION_DEV+1))
75-
sed -i "s/version = \".*\"$/version = \"$v\"/" pyproject.toml
76-
- name: increment patch version
77-
if: ${{ !env.VERSION_DEV }}
135+
- name: commit version
136+
env:
137+
USER: github-actions[bot]
138+
EMAIL: github-actions[bot]@users.noreply.github.com
139+
run: git -c user.name="$USER" -c user.email="$EMAIL" commit --all -m "increment version after release"
140+
141+
- name: push updates
78142
run: |
79-
v=$VERSION_MAJOR.$VERSION_MINOR.$((VERSION_PATCH+1))-dev0
80-
sed -i "s/version = \".*\"$/version = \"$v\"/" pyproject.toml
81-
- name: commit new version-number
82-
uses: stefanzweifel/git-auto-commit-action@v4
83-
with:
84-
branch: ${{ env.BRANCH }}
85-
create_branch: true
86-
commit_message: "increment version after release"
143+
git fetch origin 'refs/heads/*:refs/remotes/origin/*'
144+
git push origin "HEAD:$(git log --pretty='%D' | grep -oPm1 '(?<=origin/).*')"

0 commit comments

Comments
 (0)