Skip to content

Publish to pypi #59

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

Merged
merged 2 commits into from
Nov 21, 2021
Merged

Conversation

wnienhaus
Copy link
Collaborator

@wnienhaus wnienhaus commented Nov 21, 2021

This PR adds a GitHub Actions workflow for publishing our upip package to PyPi. It resolves #58.

Overview

I was inspired to create this by this article: https://simonwillison.net/2021/Nov/4/publish-open-source-python-library/, even though I now saw, that GitHub offers a workflow template for this too, using an action from its Actions Marketplace.

Instead of using a hardcoded version number during the build, setup.py now uses setuptools-scm to derive a useful/relevant version from git metadata (tags/commits).

The below is documentation mainly for once the PR has been merged, and for how to then test it and later release 1.0.0.

Setup

The workflow in this PR will require setting up 2 repository secrets (Settings -> Secrets -> Actions secrets -> New repository secret), namely TEST_PYPI_API_TOKEN and PYPI_API_TOKEN (for TestPyPi and real PyPI respectively). These tokens can be generated in TestPyPI and real PyPI as follows:

  • Log in
  • Going to Account Settings -> API tokens
  • Selecting "Add API token"
  • Entering a token name, e.g. "publish-py-esp32-ulp"
  • Selecting the right project
  • And selecting "Add token"
  • Copy the resulting token, which should be a long string starting with pypi-

Once you have the tokens for both TestPyPI and real PyPI, add them as repository secret to the Github repo.

Testing

To test this PR, it sadly needs to be merged to master (or a "dummy" publish.yml must be committed to master), otherwise Github does make this workflow available in the user interface. However, merging to master should be safe, as this workflow is only triggered when creating a release (which is a deliberate step) or by manually triggering the workflow.

Once merged to master, it can be tested by either triggering the workflow manually (a good first step), or by creating a new Release with Github.

  • NOTE: Since PyPI does not allow reusing version numbers, it might be best to try the first release with a version number lower than 1.0.0, e.g. 0.0.1 and then deleting that version again once 1.0.0 is released (or just before).
  • It might also be good to not configure the live PYPI_API_TOKEN secret initially, to prevent accidentally publishing test releases to the live PyPI.
  • When manually triggering the workflow, and thus no tag is (yet) added to the repo, the version number of the package will be derived by setuptools-scm as 0.1-devX where X is the number of commits since the last tag (in our case, since the beginning of history, since we have no tags yet)

Test-before-publish

The article that inspired me to create this publishing workflow suggested to run the automated tests again as part of the publishing workflow to ensure no untested code is published. I like that idea.

However, I could not find a good way to do that without duplicating the test steps from the existing workflow into the new publish workflow. I tried many ways (see the ideas I tried) and eventually decided to leave this out. Github Actions sadly does not provide good enough ways to reuse workflows or to make workflows dependent on each other.

I ended up deciding that simple is better and that publishing is a deliberate act on our part anyway (creating a Release with Github) so we can choose to release only when we know all tests pass.

Happy to get feedback on this.

Disable appending local part to version because we're
testing uploads to TestPyPI, and pypi does not support
the local part in version numbers (see:
https://github.com/pypa/setuptools_scm/#version-number-construction)
This workflow is triggered when publishing a new release
using the GitHub Releases functionality. It can also be
triggered manually for testing.

The workflow will always publish to Test PyPI, but will
only publish to the real PyPI, when releasing a new
version using the GitHub Releases functionality.

The version for the package is generated by setuptools_scm,
based on the git tags/commits. For all non-tagged commits,
a "dev version" is generated with the format "0.1.2-dev3",
where 0.1.2 is the latest tag plus one more in the last part
of the version number (i.e. given the last tag was 0.1.1,
0.1.2 will be used for the dev version) and the 3 in "-dev3"
comes from the number of commits since the last tag.
(This all is standard setuptools_scm behaviour.)
@ThomasWaldmann
Copy link
Collaborator

Looks good, thanks for working on this. I set up the TEST secret (but no the production one) already, so let's try this...

@ThomasWaldmann ThomasWaldmann merged commit fd10ea7 into micropython:master Nov 21, 2021
@ThomasWaldmann
Copy link
Collaborator

ThomasWaldmann commented Nov 21, 2021

@wnienhaus wnienhaus deleted the publish-to-pypi branch November 28, 2021 10:37
@ThomasWaldmann
Copy link
Collaborator

ThomasWaldmann commented Dec 3, 2021

Hmm, it seems that I am not owner of the pypi.org project yet, so I can't generate the pypi api token.

Ehrm, that doesn't exist yet. And one can't create (specific) tokens for non-existing projects.

Update: using a temporary scary global token worked to create the pypi.org project, now replaced that again with a project-specific token.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Automate publish to PyPI
2 participants