# Use GitHub workflows to automatically publish to PyPI when new tags are created

**URL:** <https://discourse.jupyter.org/t/use-github-workflows-to-automatically-publish-to-pypi-when-new-tags-are-created/14941>\
**Category:** Show and Tell\
**Created:** [July 15, 2022, 12:54pm UTC](https://discourse.jupyter.org/t/use-github-workflows-to-automatically-publish-to-pypi-when-new-tags-are-created/14941 "2022-07-15T12:54:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![choldgraf](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/choldgraf/32/1264_2.png) [@choldgraf](https://discourse.jupyter.org/u/choldgraf)\
**Post date:** [July 15, 2022, 12:54pm UTC](https://discourse.jupyter.org/t/use-github-workflows-to-automatically-publish-to-pypi-when-new-tags-are-created/14941/1 "2022-07-15T12:54:11Z")

</div>

I recently came across a pretty nice pattern for using GitHub Workflows to automatically publish packages to PyPI when new tags are created. This lets you generate a new release via the GitHub `Draft a New Release` UI. (e.g. [here’s the page for the pydata sphinx theme](https://github.com/pydata/pydata-sphinx-theme/releases)). Writing it down here in case it’s useful for others and for me to re-discover in the future!

> ⚠ This will allow anybody who can create a repository tag to also create a release! That could either be a good or bad thing depending on your aversion to risk!

Here are the quick steps:

1. Generate a release secret via PyPI for your account.

2. Create a new secret for your repository via `settings` → `secrets` → `actions` → `New repository secret`:

3. Paste in the PyPI token that you just created, e.g.  

4. You can now use this secret to authenticate PyPI in your GitHub Workflows

5. In your tests workflow, add a `workflow_call` trigger, which will allow other workflows to call it on their own. E.g. you might have something like this in a `tests.yml` workflow:

6. Create a `publish.yml` workflow to publish your package. Make it **call** , and **need success for** your tests workflow. Then make it publish your package upon success. Use a template like below (note the `${{ secrets.PYPI_KEY }}`, which re-uses the secret you stored above).

And that should be it! Now to create a release you should simply do so via the GitHub repository UI, and when this happens it will trigger the `publish.yml` workflow, which will call `tests.yml` and wait for it to pass, and if so then it will publish to PyPI.

I hope this is useful!

---

<div class="post-metadata">

**Author:** ![vsoch](https://avatars.discourse-cdn.com/v4/letter/v/5fc32e/32.png) [@vsoch](https://discourse.jupyter.org/u/vsoch)\
**Post date:** [July 21, 2022, 4:38pm UTC](https://discourse.jupyter.org/t/use-github-workflows-to-automatically-publish-to-pypi-when-new-tags-are-created/14941/2 "2022-07-21T16:38:25Z")

</div>

Huge +1! I have a workflow to do similar for my main Python libs - here is the recipe that makes the pypi release on a GitHub release. Of course the only thing you need to be sure to do is that the version number is incremented.

```nohighlight
name: Release Python Package

on:
  release:
    types: [created]

jobs:
  deploy:
    runs-on: ubuntu-20.04

    steps:
    - uses: actions/checkout@v2

    - name: Install
      run: conda create --quiet --name shpc twine

    - name: Install dependencies
      run: |
        export PATH="/usr/share/miniconda/bin:$PATH"
        source activate shpc
        pip install -e .
        pip install setuptools wheel twine
    - name: Build and publish
      env:
        TWINE_USERNAME: ${{ secrets.PYPI_USER }} 
        TWINE_PASSWORD: ${{ secrets.PYPI_PASS }}
      run: |
        export PATH="/usr/share/miniconda/bin:$PATH"
        source activate shpc
        python setup.py sdist bdist_wheel
        twine upload dist/*

```

And direct link to it: [singularity-hpc/release.yaml at fb9a99a95d596f9ffbe1f2155ad91bfadf145bac · singularityhub/singularity-hpc · GitHub](https://github.com/singularityhub/singularity-hpc/blob/fb9a99a95d596f9ffbe1f2155ad91bfadf145bac/.github/workflows/release.yaml)

---

<div class="post-metadata">

**Author:** ![jtp](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/jtp/32/19100_2.png) [@jtp](https://discourse.jupyter.org/u/jtp)\
**Post date:** [July 25, 2022, 7:30am UTC](https://discourse.jupyter.org/t/use-github-workflows-to-automatically-publish-to-pypi-when-new-tags-are-created/14941/3 "2022-07-25T07:30:41Z")

</div>

For reference there is also the Jupyter Releaser which is a set of GitHub Actions to make the release process consistent and easier to manage across repositories: [Jupyter Releaser — Jupyter Releaser 0.22.5 documentation](https://jupyter-releaser.readthedocs.io/en/latest/)

With some improvements and simplifications on the roadmap: [Simplified Workflow · Issue #272 · jupyter-server/jupyter\_releaser · GitHub](https://github.com/jupyter-server/jupyter_releaser/issues/272)
