python_packaging_example
1.0.0
TestPyPiPyPi這是一個使用GitHub操作CI/CD工作流程來測試,構建和上傳Python軟件包中的工作示例。
我通過通過這些指南來創建此示例包;
其餘的REDME描述的是以相同的方式建立一個新項目。
設置時;
pip install example-package-grumbit軟件包在高水平上,設置GitHub CI/CD項目包裝(包括pytesting)的過程看起來像這樣;
./.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令牌並保存在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]部分中更新其版本,然後需要重新構建並上傳; 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 。然後添加兩個新秘密,稱為PYPI_API_TOKEN和TEST_PYPI_API_TOKEN ,並在上面上傳包裝後創建了API令牌
創建和配置.github/workflows/publish-to-Test-pypi.yml工作流程定義
publish-to-test-pypi.yml已經具有自動測試所需的零件(請參見下文)TestPyPimaster分支提交提交時,github ci/cd都會運行。TestPyPiPyPi在提交上放一個標籤並將其推動會導致GitHub CI/CD運行並創建PYPI釋放。
使用以下內容將最新提交(IE 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/目錄中;src/ TREE中的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