Ok i can break this down for you more later today (we are actually working on tutorials to explain this type of thing so doing this is inline with what i’m working on now already)
i also suggest that you do NOT copy my build action. let’s fix yours instead!! if you open a pr i can work with you but let’s break this down here first.
Lets look at the tutorial first
breakdown - when the entire action is triggered
at the top of your action is tells github to only trigger the action when there is a PUSH to the main branch. This means that your action will only be triggered when that happens.
on:
push:
branches: [ 'main' ]
in the pypa tutorial is has this
name: Publish Python 🐍 distributions 📦 to PyPI and TestPyPI
on: push
this means that the action will be triggered in all pushes (with some caveats if you have logic below which you do in your action)
but this is the first different and it may be why your build isn’t working.
Breakdown - what is running and when using conditionals
let’s break down the second part
pypa has this:
- name: Publish distribution 📦 to Test PyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
password: ${{ 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@release/v1
with:
password: ${{ secrets.PYPI_API_TOKEN }}
and you have this
- name: Publish distribution 📦 to Test PyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
password: ${{ secrets.TEST_PYPI }}
repository-url: https://test.pypi.org/legacy/
- name: Publish distribution 📦 to PyPI
if: startsWith(github.ref, 'refs/tags')
uses: pypa/gh-action-pypi-publish@release/v1
with:
password: ${{ secrets.PYPI }}
in your chunk of code in the action - there is a condition here
if: startsWith(github.ref, 'refs/tags')
this tells github to ONLY run that part of the action which is pushing to pypi (rather than test pypi) IF the push “item” starts with a github reference to tags (github.ref, refs/tags
.) WHile a tag or release can be associated with a branch ( see image below when i create a release)
it does not entail a pull request. and again notice at the top of your action, you are telling github to only trigger that action on a PUSH to the MAIN branch (no other branches will be triggered).
OK so i hope that is clear! BUT i understand if it isn’t please ask more questions!
my action is a bit different but similar in how it behaves. let’s break down the pieces that would be relevant to you here.
on:
release:
types: [published]
push:
branches:
- master
here i’m telling GitHub to trigger this action when one of two conditions is true:
- on a release that is published OR
- on a push to master branch (i know we need to change our branch to main)…
you can ignore the middle part of my action for now - i’m using a different build tool than you are - you are using hatch and hatchling (i’m curious how you think it is to use!). I am using setuptools and setuptools scm for versioning. so i’d stick with what you have in the middle of your action.
Below i have some logic that tells the action to ONLY push to test pypi when the event is a push event. Because i have the action trigger on push to master only at the TOP it will then push to test pypi when ever we push to the master (in your case main) branch.
- name: Publish package on test PyPI on merge
# THIS LOGIC IS A TRIGGER TO ONLY RUN THE PUBLISH PACKAGE ON TEST PYPI SECTION OF THE BUILD ON A PUSH TO MASTER (WITH THE BRANCH NAME BEING SPECIFIED AT THE TOP OF THE ACTION).
if: github.event_name == 'push'
uses: pypa/gh-action-pypi-publish@release/v1
with:
password: ${{ secrets.TEST_PYPI_TOKEN }}
repository_url: https://test.pypi.org/legacy/
# THIS JUST TELLS IT TO NOT TRY TO OVERWRITE AN EXISTING TAGGED RELEASE ON TEST PYPI
skip_existing: true
- name: Publish package to PyPI
# THIS TELLS GITHUB TO ONLY RUN THIS SECTION OF THE ACTION WHEN THERE IS A RELEASE (A RELEASE AND TAG ARE NOT A PULL REQUEST BUT THEY CAN BE ASSOCIATED WITH A BRANCH AND HAVE A COMMIT REFERENCE THAT IS A TAG NUMBER)
if: github.event_name == 'release'
uses: pypa/gh-action-pypi-publish@release/v1
with:
password: ${{ secrets.PYPI_TOKEN }}
Summary!
this combination of logic just allows you to control
- when the action is triggered to begin with - in MY example it’s triggered on a release or on a push to master. You want the same but you want to change master to main (which is better for many reasons!).
- You can add conditionls to sections below in your action that tell github to only run that part of the action given particular situations. In this case you want to tell it to push to test pypi when you push to main and you want it to push to the real pypi when you create a new release.
Follow up
Please let me know if this generally makes sense to you. you can actually do the same thing using several different approaches.
Next steps -
why don’t you submit a pr to your repo with a revised action and i’ll have a look.