Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Output the built package version #152

Merged
merged 4 commits into from
Dec 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion .github/workflows/ci-supported-pythons.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ jobs:
with:
repository: hynek/structlog
path: structlog
fetch-depth: 0
- uses: actions/checkout@v4
with:
path: action
Expand All @@ -29,12 +30,13 @@ jobs:
path: structlog

outputs:
package-version: ${{ steps.baipp.outputs.package_version }}
python-versions: ${{ steps.baipp.outputs.supported_python_classifiers_json_array }}
# If your matrix consists only of Python versions, you can use the
# following, too:
# python-versions: ${{ steps.baipp.outputs.supported_python_classifiers_json_job_matrix_value }}

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

test-package-version:
needs: build-package
runs-on: ubuntu-latest

steps:
- uses: actions/setup-python@v5
with:
python-version: "3.x"

- run: |
echo "Package version: ${{ needs.build-package.outputs.package-version }}"

...
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

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

### Added

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

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

Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,20 @@ While *build-and-inspect-python-package* will build a wheel for you by default,

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`.

- `package_version`: The version of the package as extracted from the package metadata.

This is useful, for example, for displaying the PyPI URL on the GitHub UI for the publishing job:

```yaml
jobs:
...
release:
runs-on: ubuntu-latest
needs: baipp
environment:
name: pypi
url: https://pypi.org/p/structlog/${{ needs.baipp.outputs.package-version }}
```

### Artifacts

Expand Down
27 changes: 18 additions & 9 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,17 @@ outputs:
versions. When loaded using the 'fromJson' function, this can be assigned
to a matrix strategy key (for example, `python-version`).

value: ${{ steps.supported-pythons-setter.outputs.supported_python_classifiers_json_array }}
value: ${{ steps.metadata-setter.outputs.supported_python_classifiers_json_array }}
supported_python_classifiers_json_job_matrix_value:
description: >
Same as 'supported_python_classifiers_json_array', except it's already a
JSON mapping from "python-version" to a list of all classifier-declared
supported Python versions. In other words, you can assign it directly to
the 'strategy.matrix' key.
value: ${{ steps.supported-pythons-setter.outputs.supported_python_classifiers_json_job_matrix_value }}
value: ${{ steps.metadata-setter.outputs.supported_python_classifiers_json_job_matrix_value }}
package_version:
description: The version of the package as declared in the metadata.
value: ${{ steps.metadata-setter.outputs.package_version }}

runs:
using: composite
Expand Down Expand Up @@ -233,18 +236,24 @@ runs:
path: /tmp/baipp/dist/out/sdist/PyPI-README.*

- name: Generate JSON objects of supported Python versions
id: supported-pythons-setter
id: metadata-setter
shell: bash
working-directory: /tmp/baipp/dist/out/sdist/
run: |
cat */PKG-INFO | python -c '
import email.parser
import json, re, sys
match_classifier = re.compile(
r"\s*Classifier: Programming Language :: Python :: (\d+\.\d+)$"
).match
version_tokens = [
m.group(1).strip() for l in sys.stdin.readlines() if (m := match_classifier(l))
]

pkg_info = email.parser.Parser().parsestr(sys.stdin.read())

version_tokens = []
for classifier in pkg_info.get_all("Classifier", []):
if match := re.match(r"Programming Language :: Python :: (\d+\.\d+)$", classifier):
version_tokens.append(match.group(1))

package_version = pkg_info.get("Version", "0.0.0")

print(f"package_version={package_version}")
print(f"supported_python_classifiers_json_array={json.dumps(version_tokens)}")
print(f"""supported_python_classifiers_json_job_matrix_value={json.dumps({"python-version": version_tokens})}""")
' >> $GITHUB_OUTPUT
Loading