TestPyPi عبر الالتزام بالسيدPyPi عبر وضع العلاماتهذا مثال عاملة يستخدم سير عمل CI/CD لاختبار وإنشاء وتحميل حزمة Python إلى TestPypi و Pypi.
لقد قمت بإنشاء حزمة المثال من خلال العمل من خلال هذه الأدلة ؛
يصف بقية The Readme إعداد مشروع جديد بنفس الطريقة.
عند الإعداد ؛
pip install example-package-grumbit على مستوى عالٍ ، تبدو عملية إعداد مشروع GitHub CI/CD ، بما في ذلك pytesting ، هكذا ؛
./.github/workflows/publish-to-test-pypi.yml لحزمة المثال ، ولكن اتركها../.github/workflows/publish-to-test-pypi.yml 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 tokenبعد تحميل الحزمة ، يجب إعداد رمز API محدد الحزمة وحفظه في TestPypi
تحقق من تنزيل الحزمة واستخدامها في 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] من ./pyproject.toml ، ثم يجب إعادة تصميمه وتحميله ؛ 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 إذا لم يكن الفرع الافتراضي master ، فإما تغييره على github ، أو تغيير .github/workflows/publish-to-test-pypi.yml .
أضف رموز api و pypi api إلى الريبو
افتح ريبو على جيثب باستخدام gh browse . في المتصفح ، انقر فوق Settings -> Secrets -> Actions . ثم أضف اثنين من الأسرارين الجديدين يسمى PYPI_API_TOKEN و TEST_PYPI_API_TOKEN ، مع أنشأت رموز API بعد تحميل الحزم أعلاه
قم بإنشاء وتكوين .github/سير العمل/تعريف سير العمل إلى الاختبار
publish-to-test-pypi.yml يحتوي بالفعل على الأجزاء اللازمة للاختبار التلقائي (انظر أدناه)TestPyPi عبر الالتزام بالسيدmaster ، سيتم تشغيل Github CI/CD.TestPyPiPyPi عبر وضع العلاماتإن وضع علامة على التزام ودفعها سيؤدي إلى تشغيل Github CI/CD وإنشاء إصدار PYPI.
استخدم ما يلي لوضع علامة على أحدث الالتزام (أي HEAD ) مع الإصدار الذي تم تكوينه حاليًا في ./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 اللازم لإضافته إلى src/ tests/ دليل مثل هذا ؛test/ المجلدات داخل src/ TREE. ./pyproject.toml test/ packaging_tutorial/
├── src/
│ ├── __init__.py
│ └── example_package_grumbit/
│ ├── __init__.py
│ └── example.py
└── tests/
├── __init__.py
└── example_package_grumbit/
├── __init__.py
└── test_example.py