TestPyPiにアップロードしますPyPiへのアップロードこれは、GitHubアクションCI/CDワークフローを使用して、PythonパッケージをTestPypiとPypiにテスト、ビルド、アップロードする実用的な例です。
これらのガイドを使用して、この例パッケージを作成しました。
残りのREADMEは、同じように新しいプロジェクトを設定することを説明しています。
セットアップ時;
pip install example-package-grumbitを使用してインストールできます高レベルでは、Pytestingを含むGitHub CI/CDプロジェクトパッケージをセットアップするプロセスは、このように見えます。
./.github/workflows/publish-to-test-pypi.ymlを省きます。./.github/workflows/publish-to-test-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トークンをセットアップして保存する必要があります。
パッケージをダウンロードして新しい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]セクションでバージョンを更新する必要があります。その後、再構築してアップロードする必要があります。 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トークンをリポジトリに追加します
gh browseを使用してGitHubでレポを開きます。ブラウザで、 Settings - > Secrets ] - > Actionsをクリックします。次に、上記のパッケージをアップロードした後にAPIトークンが作成された状態で、 PYPI_API_TOKENとTEST_PYPI_API_TOKENという2つの新しい秘密を追加します
.github/workflows/publish-to-test-pypi.ymlワークフロー定義を作成して構成します
publish-to-test-pypi.ymlには、自動テストに必要な部品が既にあります(以下を参照)TestPyPiにアップロードしますmasterブランチにコミットが行われるたびに、Github CI/CDが実行されます。TestPyPiにアップロードされますPyPiへのアップロードタグをコミットに置いてプッシュすると、GitHub CI/CDが実行され、PYPIリリースが作成されます。
以下を使用して、 ./pyproject.tomlで現在構成されているバージョンで、最新のコミット(つまりHEAD )にタグを付けます。
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/ディレクトリに追加する必要があることを意味しました。src/ツリーにtest/フォルダーを埋め込むよりも便利ではないと思います。 ./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