TestPyPi melalui komit untuk masterPyPi melalui penandaanIni adalah contoh kerja yang menggunakan alur kerja GitHub Action CI/CD untuk menguji, membangun, dan mengunggah paket Python ke TestPyPi dan PYPI.
Saya membuat paket contoh ini dengan mengerjakan panduan ini;
Sisa Readme menjelaskan untuk membuat proyek baru dengan cara yang sama.
Saat diatur;
pip install example-package-grumbit Pada tingkat tinggi, proses untuk menyiapkan kemasan proyek GitHub CI/CD, termasuk Pytesting, terlihat seperti ini;
./.github/workflows/publish-to-test-pypi.yml untuk saat ini../.github/workflows/publish-to-test-pypi.yml dan dapatkan github ci/cd berfungsi 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 tokenSetelah mengunggah paket, token API spesifik paket harus diatur dan disimpan di testpypi
Periksa paket dapat diunduh dan digunakan dalam venv baru;
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, maka perlu dibangun kembali dan diunggah; 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 Jika cabang default bukan master , ubah di github, atau ubah .github/workflows/publish-to-test-pypi.yml .
Tambahkan token API testpypi dan pypi ke repo
Buka repo di GitHub menggunakan gh browse . Di browser, klik Settings -> Secrets -> Actions . Kemudian tambahkan dua rahasia baru yang disebut PYPI_API_TOKEN dan TEST_PYPI_API_TOKEN , dengan token API dibuat setelah mengunggah paket di atas
Buat dan konfigurasikan .github/alur kerja/publish-to-test-pypi.yml Definisi alur kerja
publish-to-test-pypi.yml sudah memiliki bagian yang diperlukan untuk pengujian otomatis (lihat di bawah)TestPyPi melalui komit untuk mastermaster , GitHub CI/CD akan berjalan.TestPyPiPyPi melalui penandaanMenempatkan tag pada komit dan mendorongnya akan menyebabkan GitHub CI/CD berjalan dan membuat rilis PYPI.
Gunakan yang berikut untuk menandai komit terakhir (yaitu HEAD ) dengan versi yang saat ini dikonfigurasi di ./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 perlu ditambahkan ke src/ dan tests/ direktori seperti ini;test/ folder di dalam src/ Tree. ./pyproject.toml dapat dikonfigurasi sehingga test/ folder tertanam dikecualikan, tetapi saya sudah menggunakan "standar" untuk saat ini. packaging_tutorial/
├── src/
│ ├── __init__.py
│ └── example_package_grumbit/
│ ├── __init__.py
│ └── example.py
└── tests/
├── __init__.py
└── example_package_grumbit/
├── __init__.py
└── test_example.py