preface

Still manually packaging and uploading PyPI? GitHub Actions automation is delicious!

In One-click Flowchart of Python Code, I described PyFlowchart of my open source project. Over the years, several people have made suggestions or reported bugs on the project. With the help of these friends, the project has gone through several iterations. Before, I had to do a lot of code accident work with each version update of this project:

  1. Publish a release on GitHub,
  2. Manually package and upload PyPI.

This process is so anti-human that it takes two jobs to release one version. To make things worse, the job of packing and uploading PyPI is very stereotyped (my article how to Install Your Own Packages with PIP describes this process). I don’t think that as a developer, you should waste your time on this kind of routine.

I wrote a post called “Still manually blogging? GitHub Actions Automation is awesome” that outlined how I use GitHub Actions to automatically update my blog site. So, for now, I’m trying to use GitHub Actions to set up a workflow for automatically packaging and uploading new releases to PyPI 📦.

Now, when you Release a new version, you simply create a new Release on GitHub. GitHub Actions automatically helps me build, package, and upload PyPI.

This article describes how to use GitHub Actions to automatically publish Python packages to PyPI.

Note: The implementation I used for PyFlowchart is a little different from that of the diagram below. I made changes and workflow diagram changes according to requirements. If you are interested, see my flowchart implementation: github.com/cdfmlr/pyfl…


Publishing Package Distribution Releases Using GitHub Actions CI/CD Workflows Publishing Package Distribution Releases Using GitHub Actions CI/CD Workflows Publishing Package Distribution Releases Using GitHub Actions CI/CD Workflows

The original link: packaging.python.org/guides/publ…

GitHub Actions CI/CD allows a series of commands to be automatically run when certain events occur on the GitHub platform. This allows you to set up a workflow that responds to push events. This article shows how to release a new Python package distribution (to PyPI) when git push is available. We will use pypa/gh-action-pypi- Publish GitHub action.

Note: This tutorial assumes that you already have a Python project on GitHub and that you know how to build the package and publish it to PyPI.

Save the token on GitHub

In this article, we will upload the project to PyPI and TestPyPI. So you need to generate two separate tokens and save them to GitHub’s repository Settings.

Let’s get started! 🚀

  1. accessPypi.org/manage/acco…To create a newAPI token. If you have already published your project in PyPI, you should limit the token scope to that project only. You can name the new token asMaking Actions CI/CD - project - org/project - repoSomething like that. It’s easy to identify. Token is generated afterDo not close the browser page — the token will only be displayed once.
  2. In another browser TAB or window, open your project page on GitHub and clickSettingsTAB, and click on theSecrets 。
  3. Create a new sercret with the commandPYPI_API_TOKENThen copy and paste the token generated in the first step.
  4. accessTest.pypi.org/manage/acco…Repeat the previous steps to save the TestPyPI token asTEST_PYPI_API_TOKEN.

Note: If you don’t already have a TestPyPI account, you should sign up for one. TestPyPI and PyPI do not share the same account.

Create the workflow

GitHub CI/CD Workflows are stored as yamL-formatted files in the.github/workflows/ directory of the repository.

. We create a lot/workflows/publish – to – test – pypi. Yml file.

We’ll start with a meaningful name and then define the events that trigger GitHub to run this workflow:

name: Publish Python 🐍 distributions 📦 to PyPI and TestPyPI

on: push
Copy the code

Define the working environment for the workflow

Now, let’s add the initial Settings for the job. This procedure will execute the commands we define later. Here, we’ll use Ubuntu 18.04:

jobs:
  build-n-publish:
    name: Build and publish Python 🐍 distributions 📦 to PyPI and TestPyPI
    runs-on: Ubuntu 18.04
Copy the code

Check out the project and build the distribution

Then, under the build-n-publish section, add the following:

    steps:
    - uses: actions/checkout@master
    - name: Set up Python 3.7
      uses: actions/setup-python@v1
      with:
        python-version: 3.7
Copy the code

These operations will download our project source code into the CI run container, and then install and activate the Python 3.7 environment.

Now we can build dist from source code. In this example, we will be using the Build package, so assume that PyProject.toml is set up correctly in the project (see PEP 517 / PEP 518).

(Note: emmm, it’s ok not to write pyproject.toml here)

Tip: You can use any other method to build your distribution, as long as you save the packages ready to upload to the dist/ folder.

Add the following code to the steps:

    - name: Install pypa/build
      run: >- python -m pip install build --user    - name: Build a binary wheel and a source tarball
      run: >- python -m build --sdist --wheel --outdir dist/ .Copy the code

Publish to PyPI and TestPyPI

Finally, add the following code:

    - name: Publish distribution 📦 to Test PyPI
      uses: pypa/gh-action-pypi-publish@master
      with:
        password: The ${{ secrets.TEST_PYPI_API_TOKEN }}
        repository_url: https://test.pypi.org/legacy/
    - name: Publish distribution 📦 to PyPI
      if: startsWith(github.ref, 'refs/tags')
      uses: pypa/gh-action-pypi-publish@master
      with:
        password: The ${{ secrets.PYPI_API_TOKEN }}
Copy the code

Pypa /gh-action-pypi- Publish GitHub action

The first step is to unconditionally upload the contents of the dist/ folder to TestPyPI. The second step is to upload its contents to PyPI, which is only done for git tagged commits.

Done, done!

Now, every time you push a tagged submission to GitHub, this workflow will publish it to PyPI. Also, for any push, it packages it and releases it to TestPyPI, which is great for providing alpha versions and ensuring that the distribution channel stays healthy!


by("CDFMLR"."2021-01-17")
# Ah, it's snowing in Kunming today, and it's very cold at home...
# See you ❄ ️
Copy the code