这两天了解了一点python打包发布到PyPi上,同时最好是通过CI来进行自动化配置,了解过only和except操作,搞了半天也没了解配置规则一堆Error报错,最后发现通过on来进行指定就行了。
包目录情况
|-- .github
| |-- workflows
| |-- publish.yml : actions文件
|-- config : 配置文件
|-- XmlRpc : 需要发布的包
|-- setup.py : 发布需要用到的setup.py配置文件
注册Pypi
注册
https://test.pypi.org/
https://pypi.org/
获取token
-
验证邮箱后直接去
Account Settings
生成即可,注意保存,这东西只会出现一次。 -
其次注意权限的控制。
-
获取到TOKEN后注意去项目设置中的secret配置PYPI_API_TOKEN,脚本中通过环境变量来设定保护密钥安全性。
发布
- 第一种是直接去搜索市场就能找到相关的,这个没啥说的。改一下就行了,主要是配置一下paths和账户秘密。
# This workflow will upload a Python Package using Twine when a release is created
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python#publishing-to-package-registries
# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.
name: Upload Python Package
on:
push:
branches: [ "master" ]
paths:
- 'XmlRpc/__init__.py'
pull_request:
branches: [ "master" ]
paths:
- 'XmlRpc/__init__.py'
permissions:
contents: read
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: '3.x'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install build
- name: Build package
run: python -m build
- name: Publish package
uses: pypa/gh-action-pypi-publish@27b31702a0e7fc50959f5ad993c78deac1bdfc29
with:
user: __token__
password: ${{ secrets.PYPI_API_TOKEN }}
- 自定义脚本
name: Upload Python Package
on:
push:
branches: [ "master" ]
paths:
- 'XmlRpc/__init__.py'
pull_request:
branches: [ "master" ]
paths:
- 'XmlRpc/__init__.py'
permissions:
contents: read
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: '3.x'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install setuptools wheel twine
- name: Build package
run: python setup.py sdist bdist_wheel
- name: Publish package
env:
TWINE_USERNAME: __token__
TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }}
run: |
twine check dist/*
twine upload dist/*