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