From 8513bd5550e6bacac0a94dc161be42eb196b5c91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edgar=20Ram=C3=ADrez=20Mondrag=C3=B3n?= Date: Tue, 5 Nov 2024 16:11:39 -0600 Subject: [PATCH 1/2] Output the built package version --- .github/workflows/ci-supported-pythons.yml | 16 ++++++++++++- CHANGELOG.md | 4 ++++ README.md | 14 +++++++++++ action.yml | 27 ++++++++++++++-------- 4 files changed, 51 insertions(+), 10 deletions(-) diff --git a/.github/workflows/ci-supported-pythons.yml b/.github/workflows/ci-supported-pythons.yml index 8c353cc..a480d6c 100644 --- a/.github/workflows/ci-supported-pythons.yml +++ b/.github/workflows/ci-supported-pythons.yml @@ -20,6 +20,7 @@ jobs: with: repository: hynek/structlog path: structlog + fetch-depth: 0 - uses: actions/checkout@v4 with: path: action @@ -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: @@ -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 }}" + ... diff --git a/CHANGELOG.md b/CHANGELOG.md index e27b900..8de5c53 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. + [#TODO](TODO) ## [2.10.0](https://github.com/hynek/build-and-inspect-python-package/compare/v2.9.0...2.10.0) diff --git a/README.md b/README.md index 87e0768..77316d0 100644 --- a/README.md +++ b/README.md @@ -145,6 +145,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 diff --git a/action.yml b/action.yml index 64b712f..3810915 100644 --- a/action.yml +++ b/action.yml @@ -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 @@ -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 From 46c4be9b2c4cb35a02b5e7a09229b2ebc8f91eee Mon Sep 17 00:00:00 2001 From: Hynek Schlawack Date: Sun, 15 Dec 2024 13:55:29 +0100 Subject: [PATCH 2/2] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8de5c53..8491abb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ### Added - New output: `package_version` is the version of the package that was built. - [#TODO](TODO) + [#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)