JRubics / poetry-publish

An action to build and publish python package to pypi (https://pypi.org/) using poetry (https://github.com/sdispater/poetry)
BSD 3-Clause "New" or "Revised" License
137 stars 17 forks source link

Fail on mismatched version #29

Closed travis-cook-sfdc closed 1 year ago

travis-cook-sfdc commented 1 year ago

Poetry will publish to pypi using the version present in pyproject.toml.

If someone creates a github action using the following syntax:

on:
  release:
    types: [published]

Then it's possible that they'll create a github release that doesn't match the version present in pyproject.toml.

Github Actions support if conditions prior to running this github action, however if is tricky because poetry_version isn't in scope. Because poetry is installed in a Docker container, the github runner won't have access to run a command like poetry version -s to access the version.

I'd like to propose the addition of the VERIFY_GITHUB_TAG_VERSION bool argument to short-circuit if the github tag and the poetry version don't match.

JRubics commented 1 year ago

I am not sure if you are talking about poetry version or version of the package that will be published?

If talking about package version, I think this is out of scope of the action since it is not handling publishing to poetry directly but checking a version to be published in a file. Instead, I would recommend doing something like this:

    - name: Change version in pyproject.toml
      run: |
        REF=$(echo ${{ github.ref }} | sed "s#\(refs/tags/\)\?v\?##")
        sed -i "s/^version = \".\+\"/version = \"$REF\"/" pyproject.toml
    - name: Build and publish to pypi
      uses: JRubics/poetry-publish@v1.15
      with:
        pypi_token: ${{ secrets.PYPI_TOKEN }}

so you can always be sure you have the latest version in pyproject.toml. :slightly_smiling_face:

travis-cook-sfdc commented 1 year ago

Sorry I wasn't clear. I was talking about the package version.

I think this is out of scope of the action since it is not handling publishing to poetry directly

I'm not sure I would agree with this. This action does directly handle publishing to pypi / private repository.

However, the action doesn't expose setting the version. Instead, it pulls it directly from the pyproject.toml file. Because poetry publish ... does directly leverage the pyproject.toml file, that necessarily means there are two steps:

  1. Update pyproject.toml's version
  2. Create a release in github

However, if those two steps aren't done with the same release number, things start to break down.

Typing this all out begs the question for me: Is the preferred use of this action to use with:

on:
  push:
    branches:
      - main

And letting publish fail when the version hasn't changed?

EDIT: Adding some additional thoughts.

I see in the README, it lists the following example:

on:
  push:
    tags:
      - "v*.*.*"

In this example, doesn't this require that a developer releasing a new version remembering to tag a commit with the same version in pyproject.toml? If this action exposed a "package_version" arg, you wouldn't have to remember, you could just pass the tag in directly.

JRubics commented 1 year ago

I think this is out of scope of the action since it is not handling publishing to poetry directly

When I said this I meant your idea about adding version handling to the action, sorry if it was not clear. :slightly_smiling_face:

Yes, it requires from the developer to think about the versioning, but I have a few reasons why I think this action should not handle it:

What I would recommend you to do if you don't like the approach about creating another action to handle the version update, is to create a release script locally, and make it update the pyproject.toml, tag with the same version and push :slightly_smiling_face:

I hope you understand the reasons why I don't want to include this in the action, but still if you need it, feel free to fork this project and use your updated version :slightly_smiling_face:

tschm commented 1 year ago

I think the trick is to keep the version in pyproject.toml always at 0.0.0

In Gitlab I would use something like

.publish package:
    stage: deploy
    services: []
    needs: ["build", "tests"]
    script:
      - echo $CI_COMMIT_TAG
      - poetry version $CI_COMMIT_TAG
      - poetry build
      - poetry config repositories.qromatiq-registry https://qromatiq.codes/api/v4/projects/12/packages/pypi
      - poetry publish --repository qromatiq-registry --username ... --password ...
    rules:
      - if: $CI_COMMIT_TAG

So the pyproject.toml showing up in my registry will indeed have the correct version (given by the COMMIT_TAG). I am too new to Github actions but keen to learn more...

JRubics commented 1 year ago

Great idea @tschm, Thank you for sharing it :raised_hands: