From 90015dc5992fa93ac749d784744b1666d4c1061c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Paduszyn=CC=81ski?= Date: Fri, 3 Nov 2023 14:40:51 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=91=B7=20Add=20workflow=20publishing=20th?= =?UTF-8?q?e=20package=20to=20PyPI?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/publish-package.yml | 80 +++++++++++++++++++++++++++ scripts/package_version.py | 15 +++++ 2 files changed, 95 insertions(+) create mode 100644 .github/workflows/publish-package.yml create mode 100755 scripts/package_version.py diff --git a/.github/workflows/publish-package.yml b/.github/workflows/publish-package.yml new file mode 100644 index 0000000..c8bddf4 --- /dev/null +++ b/.github/workflows/publish-package.yml @@ -0,0 +1,80 @@ +name: Publish package + +on: + workflow_dispatch: + +jobs: + get-release-tag: + name: Get the package version + runs-on: ubuntu-latest + outputs: + package-version: ${{ steps.package-version.outputs.package-version }} + steps: + - name: Check out 🎉 + uses: actions/checkout@v4.1.1 + - name: Set up Python 🐍 + uses: actions/setup-python@v4.7.1 + with: + cache: pip + - name: Get the package version 🔖 + id: package-version + run: + echo "package-version=$(python ./scripts/package_version.py)" >> "$GITHUB_OUTPUT" + pypi-publish: + needs: get-release-tag + name: Upload to PyPI + runs-on: ubuntu-latest + environment: + name: PyPI + url: https://pypi.org/p/python-gitmojis + permissions: + id-token: write + steps: + - name: Check out 🎉 + uses: actions/checkout@v4.1.1 + - name: Set up Python 🐍 + uses: actions/setup-python@v4.7.1 + with: + cache: pip + - name: Install PyPA `build` package 📦 + run: | + python -m pip install --upgrade pip + python -m pip install build + - name: Build the package 👷 + run: + python -m build + - name: Publish to PyPI 🚀 + uses: pypa/gh-action-pypi-publish@v1.8.10 + create-release-tag: + needs: + - get-release-tag + - pypi-publish + name: Create the release tag + runs-on: ubuntu-latest + permissions: + contents: write + steps: + - name: Check out 🎉 + uses: actions/checkout@v4.1.1 + - name: Create tag 🔖 + run: | + git config user.name "${{ github.actor }}" + git config user.email "${{ github.actor_id }}+${{ github.actor }}@users.noreply.github.com" + git tag "${{ needs.get-release-tag.outputs.package-version }}" + git push --tags + publish-release: + needs: + - get-release-tag + - create-release-tag + name: Publish GitHub release + runs-on: ubuntu-latest + permissions: + contents: write + steps: + - name: Check out 🎉 + uses: actions/checkout@v4.1.1 + - name: Publish GitHub Release 📝 + uses: softprops/action-gh-release@v0.1.15 + with: + tag_name: ${{ needs.get-release-tag.outputs.package-version }} + generate_release_notes: true diff --git a/scripts/package_version.py b/scripts/package_version.py new file mode 100755 index 0000000..f896531 --- /dev/null +++ b/scripts/package_version.py @@ -0,0 +1,15 @@ +import sys +from pathlib import Path + +import tomllib + + +def get_package_version() -> str: + """Return the package version from the `pyproject.toml` metadata.""" + with (Path.cwd() / "pyproject.toml").open("rb") as pyproject_toml: + metadata = tomllib.load(pyproject_toml) + return metadata["project"]["version"] + + +if __name__ == "__main__": + sys.stdout.write(get_package_version())