Skip to content

Commit 7d15c7f

Browse files
Output the built package version (#152)
* Output the built package version * Update CHANGELOG.md --------- Co-authored-by: Hynek Schlawack <[email protected]>
1 parent f11e84c commit 7d15c7f

File tree

4 files changed

+51
-10
lines changed

4 files changed

+51
-10
lines changed

.github/workflows/ci-supported-pythons.yml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ jobs:
2020
with:
2121
repository: hynek/structlog
2222
path: structlog
23+
fetch-depth: 0
2324
- uses: actions/checkout@v4
2425
with:
2526
path: action
@@ -29,12 +30,13 @@ jobs:
2930
path: structlog
3031

3132
outputs:
33+
package-version: ${{ steps.baipp.outputs.package_version }}
3234
python-versions: ${{ steps.baipp.outputs.supported_python_classifiers_json_array }}
3335
# If your matrix consists only of Python versions, you can use the
3436
# following, too:
3537
# python-versions: ${{ steps.baipp.outputs.supported_python_classifiers_json_job_matrix_value }}
3638

37-
test-package:
39+
test-supported-pythons:
3840
needs: build-package
3941
runs-on: ubuntu-latest
4042
strategy:
@@ -76,4 +78,16 @@ jobs:
7678
- name: Run tox environments for ${{ matrix.python-version }}
7779
run: echo python -Im tox run --installpkg dist/*.whl -f py$(echo ${{ matrix.python-version }} | tr -d .)
7880

81+
test-package-version:
82+
needs: build-package
83+
runs-on: ubuntu-latest
84+
85+
steps:
86+
- uses: actions/setup-python@v5
87+
with:
88+
python-version: "3.x"
89+
90+
- run: |
91+
echo "Package version: ${{ needs.build-package.outputs.package-version }}"
92+
7993
...

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
77

88
## [Unreleased](https://github.com/hynek/build-and-inspect-python-package/compare/v2.10.0...main)
99

10+
### Added
11+
12+
- New output: `package_version` is the version of the package that was built.
13+
[#152](https://github.com/hynek/build-and-inspect-python-package/pull/152)
1014

1115
## [2.10.0](https://github.com/hynek/build-and-inspect-python-package/compare/v2.9.0...2.10.0)
1216

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,20 @@ While *build-and-inspect-python-package* will build a wheel for you by default,
156156

157157
This is useful if you only want to define a matrix based on Python versions, because then you can just assign this to `strategy.matrix`.
158158

159+
- `package_version`: The version of the package as extracted from the package metadata.
160+
161+
This is useful, for example, for displaying the PyPI URL on the GitHub UI for the publishing job:
162+
163+
```yaml
164+
jobs:
165+
...
166+
release:
167+
runs-on: ubuntu-latest
168+
needs: baipp
169+
environment:
170+
name: pypi
171+
url: https://pypi.org/p/structlog/${{ needs.baipp.outputs.package-version }}
172+
```
159173

160174
### Artifacts
161175

action.yml

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,17 @@ outputs:
3636
versions. When loaded using the 'fromJson' function, this can be assigned
3737
to a matrix strategy key (for example, `python-version`).
3838
39-
value: ${{ steps.supported-pythons-setter.outputs.supported_python_classifiers_json_array }}
39+
value: ${{ steps.metadata-setter.outputs.supported_python_classifiers_json_array }}
4040
supported_python_classifiers_json_job_matrix_value:
4141
description: >
4242
Same as 'supported_python_classifiers_json_array', except it's already a
4343
JSON mapping from "python-version" to a list of all classifier-declared
4444
supported Python versions. In other words, you can assign it directly to
4545
the 'strategy.matrix' key.
46-
value: ${{ steps.supported-pythons-setter.outputs.supported_python_classifiers_json_job_matrix_value }}
46+
value: ${{ steps.metadata-setter.outputs.supported_python_classifiers_json_job_matrix_value }}
47+
package_version:
48+
description: The version of the package as declared in the metadata.
49+
value: ${{ steps.metadata-setter.outputs.package_version }}
4750

4851
runs:
4952
using: composite
@@ -233,18 +236,24 @@ runs:
233236
path: /tmp/baipp/dist/out/sdist/PyPI-README.*
234237

235238
- name: Generate JSON objects of supported Python versions
236-
id: supported-pythons-setter
239+
id: metadata-setter
237240
shell: bash
238241
working-directory: /tmp/baipp/dist/out/sdist/
239242
run: |
240243
cat */PKG-INFO | python -c '
244+
import email.parser
241245
import json, re, sys
242-
match_classifier = re.compile(
243-
r"\s*Classifier: Programming Language :: Python :: (\d+\.\d+)$"
244-
).match
245-
version_tokens = [
246-
m.group(1).strip() for l in sys.stdin.readlines() if (m := match_classifier(l))
247-
]
246+
247+
pkg_info = email.parser.Parser().parsestr(sys.stdin.read())
248+
249+
version_tokens = []
250+
for classifier in pkg_info.get_all("Classifier", []):
251+
if match := re.match(r"Programming Language :: Python :: (\d+\.\d+)$", classifier):
252+
version_tokens.append(match.group(1))
253+
254+
package_version = pkg_info.get("Version", "0.0.0")
255+
256+
print(f"package_version={package_version}")
248257
print(f"supported_python_classifiers_json_array={json.dumps(version_tokens)}")
249258
print(f"""supported_python_classifiers_json_job_matrix_value={json.dumps({"python-version": version_tokens})}""")
250259
' >> $GITHUB_OUTPUT

0 commit comments

Comments
 (0)