一、pypi中创建token
1. 浏览器中打开这个网址Log in · PyPI
输入Token name,选择一个Scope,然后点击Create token
创建成功后,进入到如下页面:
点进Copy token,然后在新的标签页打开github
二、配置token到GitHub
1. 登录Github后,进入要配置的代码仓,点击Settings
进入新页面后,点击左侧的Secrets and variables,点击子菜单中的Actions
在跳转后的新页面里,点击右下角的New repository secrets
进入新页面后,Secret下方粘贴刚才从pypi中复制的token,Name下方输入PYPI_API_TOKEN
点击下方的绿色按钮Add secret,添加成功后Repository secrets栏目下方就会列出这个token了
三、创建挂载Github Action的自动化发布脚本
在代码仓的.github/workflows/目录下(如果没该目录,就手动创建一下),添加publish.yml文件,内容如下:
name: Publish
on:
push:
tags:
- '*'
jobs:
publish:
runs-on: ubuntu-latest
strategy:
matrix:
package:
- fast-dev-cli
- fastdevcli-slim
permissions:
id-token: write
steps:
- name: Dump GitHub context
env:
GITHUB_CONTEXT: ${{ toJson(github) }}
run: echo "$GITHUB_CONTEXT"
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.10'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install build
- name: Build package
env:
BUILD_PACKAGE: ${{ matrix.package }}
run: python -m build
- name: Publish package
uses: pypa/gh-action-pypi-publish@27b31702a0e7fc50959f5ad993c78deac1bdfc29
with:
user: __token__
password: ${{ secrets.PYPI_API_TOKEN }}
- name: Dump GitHub context
env:
GITHUB_CONTEXT: ${{ toJson(github) }}
run: echo "$GITHUB_CONTEXT"
文件主要参考了https://github.com/tiangolo/fastapi/blob/master/.github/workflows/publish.yml
把其中的package改成了我的,触发构建的钩子也由release改成tag
注:该自动化发布脚本会一次性发布两个Python包,如果只想发布一个包,可以参考这个:tortoise-orm/.github/workflows/pypi.yml at main · tortoise/tortoise-orm · GitHub