TestPyPi via s'engager à MasterPyPi via le marquageIl s'agit d'un exemple de travail qui utilise un flux de travail CI / CD GitHub Actions pour tester, créer et télécharger un package Python sur TestPyPi et PYPI.
J'ai créé cet exemple de package en travaillant via ces guides;
Le reste de la lecture décrit pour mettre en place un nouveau projet de la même manière.
Lors de la configuration;
pip install example-package-grumbit À un niveau élevé, le processus de configuration de l'emballage du projet GitHub CI / CD, y compris Pytesting, ressemble à ceci;
./.github/workflows/publish-to-test-pypi.yml pour l'instant../.github/workflows/publish-to-test-pypi.yml et obtenez un gitub ci / cd 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 tokenAyant téléchargé le package, un jeton API spécifique à un package doit être configuré et enregistré dans TestPy
Vérifier que le package peut être téléchargé et utilisé dans un nouveau 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, il doit être reconstruit et téléchargé; 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 branche par défaut n'est pas master , modifiez-la sur github ou modifiez .github/workflows/publish-to-test-pypi.yml .
Ajoutez les jetons API TestPyPi et PYPI au repo
Ouvrez le dépôt sur GitHub à l'aide de gh browse . Dans le navigateur, cliquez sur Settings -> Secrets -> Actions . Ensuite, ajoutez deux nouveaux secrets appelés PYPI_API_TOKEN et TEST_PYPI_API_TOKEN
Créer et configurer .github / workflows / Publish-to-test-PyPi.yml Workflow Définition
publish-to-test-pypi.yml a déjà les pièces nécessaires pour les tests automobiles inclus (voir ci-dessous)TestPyPi via s'engager à Mastermaster , le GitHub CI / CD s'exécutera.TestPyPiPyPi via le marquageMettre une balise sur un engagement et le pousser provoquera l'exécution de GitHub CI / CD et créera une version PYPI.
Utilisez ce qui suit pour marquer le dernier engagement (IE HEAD ) avec la version actuellement configurée dans ./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 devait ajouter au src/ et tests/ répertoire comme celui-ci;test/ dossiers dans le src/ Tree. ./pyproject.toml peut être configuré pour que test/ dossiers intégrés soient exclus, mais je suis allé avec la "norme" pour l'instant. packaging_tutorial/
├── src/
│ ├── __init__.py
│ └── example_package_grumbit/
│ ├── __init__.py
│ └── example.py
└── tests/
├── __init__.py
└── example_package_grumbit/
├── __init__.py
└── test_example.py