@@demo::page_name@@
In this tutorial we will create a Python package and we will
automatically upload the resulting .tar.gz and .whl files to
the PyPI repository.
The steps were copied from
https://packaging.python.org/tutorials/packaging-projects/.
We assume you already created a repository on rocketgit.com, named
prj1.
If you do not have an account on PyPI, go
there
and create one.
To be able to push your packages to PyPI, you need to create an API token.
Go there
and press Add API token;
fill in any name you want, and, for Scope, select
Entire account (all projects) and press Add token.
Copy the API token to clipboard because you will need it later.
Clone your repository:
git clone @@base_url@@@@login_ui::homepage@@/prj1
cd prj1
Create a file named my_build_script.sh with the following content:
#!/bin/bash
# Build the package
python3 setup.py sdist bdist_wheel
# Upload package
python3 -m twine upload dist/*
It is the script which will get executed when you will run git push.
Create another file, setup.py. It is the build script for setuptools:
import setuptools
setuptools.setup(
name="example-pkg-YOUR-USERNAME-HERE", # Replace with your own username
version="0.0.1",
author="Example Author",
author_email="author@example.com",
description="A small example package",
long_description="bla bla bla",
long_description_content_type="text/markdown",
url="@@base_url@@@@login_ui::homepage@@/prj1",
packages=setuptools.find_packages(),
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
python_requires='>=3.6',
)
Create the LICENSE file:
Put here a nice license text.
Create the directory which will contain the code for your package:
mkdir prj1
cd prj1
Create the package main file, prj1.py:
def hello():
print("Hello from prj1!")
return
And the initialization file, __init__.py:
from .prj1 import hello
We will add all the files to git:
cd ..
git add .
git commit -m "First commit"
It is time to define a webhook which, when you do a git push, will
generate a job which will be sent to one of the workers.
The worker will start a build environment (a VM, for example)
and will run my_build_script.sh inside it.
Click here
(opens in a new tab)
or go to Settings main menu,
click on Webhooks submenu and then press Add.
From the list, go to type build line and click "Generic".
For Repository input field
set prj1 or leave it empty to allow the webhook to execute for all
your projects. Add a description to easily identify the webhook.
Because we need to authenticate ourselves to PyPI, we need to set the API token:
go to Secrets area, set the Name field to TWINE_USERNAME
and the Value field to __token__.
Go to the next secret line and set the name to TWINE_PASSWORD and, for the value,
paste the PyPI token you have in your clipboard. It must start with pypi-.
Select Push trigger
event, select debian-10-amd64 environment, add python-setuptools
python-setuptools-wheel python3-setuptools python3-wheel twine to the
Packages to install section (we add all the possible names to cover
all distributions - invalid names are ignored), and, for the first command,
input bash my_build_script.sh. Finally, click Add button.
We can go back to terminal, and run:
git push
If everything went OK, after few minutes, you can check the Webhooks area:
go to Settings in the main menu and go to Webhooks submenu.
Scroll to your webhook, and expand Last output area: here you will
find the output of the package installation process and the output of
your my_build_script.sh script. From this log, you can figure out
why something went wrong or you can find that your upload was a success.
Go to
PyPI
and you should see your package uploaded there.
To install your newly uploaded package, on any machine, use:
python3 -m pip install example-pkg-YOUR-USERNAME-HERE
To use it:
python3
>>> import prj1
>>> prj1.hello();