From 29edf77d58e2ee9ce6478dce097c8ebbd6b61176 Mon Sep 17 00:00:00 2001 From: Nic Cope Date: Wed, 3 Jan 2024 17:58:33 -0800 Subject: [PATCH 1/2] Push to PyPI I _think_ this should push to PyPI only when the CI workflow is run with a version input, similar to how builds work for functions. Signed-off-by: Nic Cope --- .github/workflows/ci.yml | 37 +++++++++++++++++++++++++++++- crossplane/function/__version__.py | 16 +++++++++++++ pyproject.toml | 6 +++-- 3 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 crossplane/function/__version__.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c082715..3bc144a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -9,13 +9,16 @@ on: workflow_dispatch: inputs: version: - description: PyPI module version (e.g. v0.1.0) + description: PyPI project version (e.g. 0.1.0 - no 'v') required: false env: # Common versions PYTHON_VERSION: '3.11.5' + # The PyPi project version to push. The default is 0.0.0-gitdate-gitsha. + PYPI_VERSION: ${{ inputs.version }} + jobs: lint: runs-on: ubuntu-22.04 @@ -66,6 +69,17 @@ jobs: - name: Setup Hatch run: pipx install hatch==1.7.0 + # If a version wasn't explicitly passed as a workflow_dispatch input we + # default to version v0.0.0--, for example + # v0.0.0-20231101115142-1091066df799. This is a simple implementation of + # Go's pseudo-versions: https://go.dev/ref/mod#pseudo-versions. + - name: Set Default PyPI Project Version + if: env.PYPI_VERSION == '' + run: echo "PYPI_VERSION=0.0.0-$(date -d@$(git show -s --format=%ct) +%Y%m%d%H%M%S)-$(git rev-parse --short=12 HEAD)" >> $GITHUB_ENV + + - name: Set PyPI Project Version + run: hatch version ${{ env.PYPI_VERSION }} + - name: Build Sdist and Wheel run: hatch build @@ -76,3 +90,24 @@ jobs: path: "dist/*" if-no-files-found: error retention-days: 1 + + + publish: + # Don't publish unless we were run with an explicit version. + if: ${{ inputs.version != '' }} + runs-on: ubuntu-22.04 + steps: + - name: Download Sdist and Wheel from GitHub + uses: actions/download-artifact@v4 + with: + name: module + path: "dist" + + - name: Publish to PyPI + uses: pypa/gh-action-pypi-publish@v1.8.11 + with: + # Note that this is currently being pushed to the 'crossplane' PyPI + # user (not org). See @negz if you need access - PyPI requires 2FA to + # be enabled, which makes sharing the account hard. We're waiting for + # a crossplane org to be approved. + password: ${{ secrets.PYPI_API_TOKEN }} diff --git a/crossplane/function/__version__.py b/crossplane/function/__version__.py new file mode 100644 index 0000000..60c2919 --- /dev/null +++ b/crossplane/function/__version__.py @@ -0,0 +1,16 @@ +# Copyright 2023 The Crossplane Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# This is set at build time, using "hatch version" +__version__ = "0.0.0" \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index a73c853..12c3ae3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,5 +1,5 @@ [build-system] -requires = ["hatchling", "hatch-vcs"] +requires = ["hatchling", "hatch-semver"] build-backend = "hatchling.build" [project] @@ -30,7 +30,9 @@ Issues = "https://github.com/crossplane/function-sdk-python/issues" Source = "https://github.com/crossplane/function-sdk-python" [tool.hatch.version] -source = "vcs" +path = "crossplane/function/__version__.py" +scheme = "semver" +validate-bump = false # Allow going from 0.0.0-x to 0.0.0-y. [tool.hatch.envs.default] type = "virtual" From c1e5e20bef1c97baee5b2d4658ddf011461c33d3 Mon Sep 17 00:00:00 2001 From: Nic Cope Date: Wed, 3 Jan 2024 18:29:58 -0800 Subject: [PATCH 2/2] Use Python-compatible versions See https://packaging.python.org/en/latest/specifications/version-specifiers/#examples-of-compliant-version-schemes Signed-off-by: Nic Cope --- .github/workflows/ci.yml | 10 +++++----- crossplane/function/__version__.py | 4 +++- pyproject.toml | 7 +++---- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3bc144a..10c64d5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -9,14 +9,14 @@ on: workflow_dispatch: inputs: version: - description: PyPI project version (e.g. 0.1.0 - no 'v') + description: PyPI project version (e.g. v0.1.0) required: false env: # Common versions PYTHON_VERSION: '3.11.5' - # The PyPi project version to push. The default is 0.0.0-gitdate-gitsha. + # The PyPi project version to push. The default is v0.0.0+gitdate-gitsha. PYPI_VERSION: ${{ inputs.version }} jobs: @@ -70,12 +70,12 @@ jobs: run: pipx install hatch==1.7.0 # If a version wasn't explicitly passed as a workflow_dispatch input we - # default to version v0.0.0--, for example - # v0.0.0-20231101115142-1091066df799. This is a simple implementation of + # default to version v0.0.0+-, for example + # v0.0.0+20231101115142-1091066df799. This is a simple implementation of # Go's pseudo-versions: https://go.dev/ref/mod#pseudo-versions. - name: Set Default PyPI Project Version if: env.PYPI_VERSION == '' - run: echo "PYPI_VERSION=0.0.0-$(date -d@$(git show -s --format=%ct) +%Y%m%d%H%M%S)-$(git rev-parse --short=12 HEAD)" >> $GITHUB_ENV + run: echo "PYPI_VERSION=v0.0.0+$(date -d@$(git show -s --format=%ct) +%Y%m%d%H%M%S)-$(git rev-parse --short=12 HEAD)" >> $GITHUB_ENV - name: Set PyPI Project Version run: hatch version ${{ env.PYPI_VERSION }} diff --git a/crossplane/function/__version__.py b/crossplane/function/__version__.py index 60c2919..b0754f1 100644 --- a/crossplane/function/__version__.py +++ b/crossplane/function/__version__.py @@ -12,5 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +"""The version of function-sdk-python.""" + # This is set at build time, using "hatch version" -__version__ = "0.0.0" \ No newline at end of file +__version__ = "0.0.0" diff --git a/pyproject.toml b/pyproject.toml index 12c3ae3..e8ca1b8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,9 +1,9 @@ [build-system] -requires = ["hatchling", "hatch-semver"] +requires = ["hatchling"] build-backend = "hatchling.build" [project] -name = "function-sdk-python" +name = "crossplane-function-sdk-python" description = 'The Python SDK for Crossplane composition functions' readme = "README.md" requires-python = ">=3.11" @@ -31,8 +31,7 @@ Source = "https://github.com/crossplane/function-sdk-python" [tool.hatch.version] path = "crossplane/function/__version__.py" -scheme = "semver" -validate-bump = false # Allow going from 0.0.0-x to 0.0.0-y. +validate-bump = false # Allow going from 0.0.0.dev0+x to 0.0.0.dev0+y. [tool.hatch.envs.default] type = "virtual"