TestPyPi ผ่านการกระทำเพื่อมาสเตอร์PyPi ผ่านการติดแท็กนี่เป็นตัวอย่างการทำงานที่ใช้เวิร์กโฟลว์การกระทำของ GitHub CI/CD เพื่อทดสอบสร้างและอัปโหลดแพ็คเกจ Python ไปยัง TestPyPi และ PYPI
ฉันสร้างแพ็คเกจตัวอย่างนี้โดยทำงานผ่านคู่มือเหล่านี้
ส่วนที่เหลือของ readMe อธิบายเพื่อตั้งค่าโครงการใหม่ในลักษณะเดียวกัน
เมื่อตั้งค่า;
pip install example-package-grumbit ในระดับสูงกระบวนการสำหรับการตั้งค่าบรรจุภัณฑ์ของโครงการ GitHub CI/CD รวมถึง pytesting ดูเหมือนว่า
./.github/workflows/publish-to-test-pypi.yml workflows/publish-to-test-pyml.yml สำหรับตอนนี้./.github/workflows/publish-to-test-pypi.yml -pypi.yml และรับ gitHub 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 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
เพิ่มโทเค็น TestPyPi และ PYPI API ลงใน repo
เปิด Repo บน GitHub โดยใช้ gh browse ในเบราว์เซอร์คลิก Settings -> Secrets -> Actions จากนั้นเพิ่มความลับใหม่สองรายการที่เรียกว่า PYPI_API_TOKEN และ TEST_PYPI_API_TOKEN พร้อมโทเค็น API ที่สร้างขึ้นหลังจากอัปโหลดแพ็คเกจด้านบน
สร้างและกำหนดค่า .github/workflows/publish-to-test-pypi.yml นิยามเวิร์กโฟลว์
publish-to-test-pypi.yml มีชิ้นส่วนที่จำเป็นสำหรับการทดสอบอัตโนมัติแล้ว (ดูด้านล่าง)TestPyPi ผ่านการกระทำเพื่อมาสเตอร์master GitHub CI/CD จะทำงานTestPyPiPyPi ผ่านการติดแท็กการวางแท็กในการกระทำและการผลักมันจะทำให้ GitHub CI/CD ทำงานและสร้างการเปิดตัว PYPI
ใช้สิ่งต่อไปนี้เพื่อติดแท็ก commit ครั้งสุดท้าย (เช่น 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