This post contains my notes for how I release a Python project from GitLab to PyPI. I love hosting projects on GitLab since I can use the built-in runners to release automatically.
The very first release is necessarily a very manual process. Once the project is on PyPI, subsequent releases can be automated.
First, you want to make sure that your source code is correct. There are a lot of little regretable mistakes to be made.
python setup.py sdist bdist_wheeldist\<package>.tar.gzrelease [version](link-to-changelog)If you already have gitlab-ci setup to release on tag, it will try and surely fail. That's OK for this initail release. You can cancel it, if you think to.
Now we actually release to PyPI.
rm -rf distpython setup.py sdist bdist_wheeltwine upload dist/*Horray! Don't forget to log into PyPI and make other project maintainers into "owners".
In this section we will set up gitlab-ci to automatically release everytime a new tag is made.
In .gitlab-ci.yml:
twine:
stage: deploy
script:
- python setup.py sdist bdist_wheel
- twine upload dist/*
artifacts:
paths:
- dist/*
only:
- tags
Within the GitLab repository, we must mark tags that we intend to release as protected. This is intended as a safety feature so that only maintainers are able to release. On GitLab:
v*PyPI allows uploads via tokens. This is how we will authenticate and let PyPI know that we are who we say we are. First, we need to generate the token. On PyPI:
gitlab-<project>, scope only for projectOn GitLab:
TWINE_PASSWORD value pypi-<TOKEN>TWINE_USERNAME value __token__On subsequent releases gitlab should release for you on tag.
built 2025-11-17 08:00:39