TestPyPi a través de Compromed to MasterPyPi a través del etiquetadoEste es un ejemplo de trabajo que utiliza un flujo de trabajo de GitHub Actions CI/CD para probar, construir y cargar un paquete Python para TestPypi y Pypi.
Creé este paquete de ejemplo trabajando a través de estas guías;
El resto del ReadMe describe establecer un nuevo proyecto de la misma manera.
Cuando se configura;
pip install example-package-grumbit En un alto nivel, el proceso para configurar el embalaje del proyecto GitHub CI/CD, incluido Pytesting, se ve así;
./.github/workflows/publish-to-test-pypi.yml por ahora../.github/workflows/publish-to-test-pypi.yml y obtenga github ci/cd funcione cd < pacakges directory >
python3 -m venv .venv # Create the venv if it doesn't exist yet
source .venv/bin/activate
python3 -m pip install --upgrade pip setuptools wheel pip-tools pytest # Install the tools needed for the build tool
python3 -m pip install --upgrade build # Install the build tool itself
python3 -m build # build the packagepython3 -m pip install --upgrade twine # Install the twine upload tool
python3 -m twine upload --repository testpypi dist/ * # Upload to TestPyPi
# When prompted, the username is __token__ and the password is the TestPyPi global scope API tokenHabiendo subido el paquete, se debe configurar y guardar un token API específico de paquete en TestPypi
Verifique que el paquete se pueda descargar y usar en un nuevo venv ;
cd < some new tmp directory >
python3 -m venv .venv
source .venv/bin/activate
package_name= " example-package-grumBit "
python3 -m pip install --index-url https://test.pypi.org/simple/ --pre ${package_name} # Check the package can be installed
python3 -c " from example_package_grumbit import example; print(example.add_one(1)) " # Check package functionspython3 -m twine upload dist/ * # Upload to PyPi
# When prompted, the username is __token__ and the password is the PyPi global scope API token[project] de ./pyproject.toml, entonces debe volver a construir y cargar; vs ./pyproject.toml
python3 -m build # build the package
python3 -m twine check dist/ * # check the package can be uploaded
python3 -m twine upload --repository testpypi dist/ * # test uploading using TestPyPi
python3 -m twine upload dist/ * # Upload to PyPi
cd " <the project's directory> "
repo_name= " <the new repo's name> "
gh repo create " ${repo_name} " --private
git init
git add --all
git commit -m " init "
git branch -M master
git remote add origin [email protected]:grumBit/ ${repo_name} .git
git push -u origin master Si la rama predeterminada no es master , cámbiela en GitHub o cambie .github/workflows/publish-to-test-pypi.yml .
Agregue los tokens Testpypi y Pypi API al repositorio
Abra el repositorio en GitHub usando gh browse . En el navegador, haga clic en Settings -> Secrets -> Actions . Luego agregue dos nuevos secretos llamados PYPI_API_TOKEN y TEST_PYPI_API_TOKEN , con los tokens API creados después de cargar los paquetes anteriores
Crear y configurar .Github/Workflows/Publish-to-test-pypi.yml Workflow Definición
publish-to-test-pypi.yml de este paquete de ejemplo ya tiene las piezas necesarias para la prueba automática incluida (ver más abajo)TestPyPi a través de Compromed to Mastermaster , se ejecutará el GitHub CI/CD.TestPyPiPyPi a través del etiquetadoPoner una etiqueta en una confirmación y presionarla hará que se ejecute Github CI/CD y cree una versión PYPI.
Use lo siguiente para etiquetar el último compromiso (IE HEAD ) con la versión actualmente configurada en ./pyproject.toml ;
version_tag=v $( cat ./pyproject.toml | egrep " ^version " | cut -d ' " ' -f2 )
version_tag_info= " Some release info "
git tag -a " ${version_tag} " -m " ${version_tag_info} "
git push --tagversion_tag= " vX.X.X "
version_tag_info= " Some release info "
commit_sha= " 16fb0fd "
git tag -a " ${version_tag} " " ${commit_sha} " -m " ${version_tag_info} "
git push --tag - name : Install requirements
run : >-
python -m
pip install
--requirement requirements.txt
- name : Run tests
run : >-
python -m
pytest__init__.py necesario para agregar al src/ y tests/ directorio como este;test/ carpetas de incrustación dentro del src/ árbol. ./pyproject.toml se puede configurar para que se excluyan test/ carpetas integradas, pero he ido con el "estándar" por ahora. packaging_tutorial/
├── src/
│ ├── __init__.py
│ └── example_package_grumbit/
│ ├── __init__.py
│ └── example.py
└── tests/
├── __init__.py
└── example_package_grumbit/
├── __init__.py
└── test_example.py