Skip to content

Publish Package📦 To PyPI #7

Publish Package📦 To PyPI

Publish Package📦 To PyPI #7

# Github actions:
# 自动Python打包推送到PyPI
# (1)通过pypi的api-token发布的方式:
# - name: Publish Distribution 📦 To PyPI
# uses: pypa/gh-action-pypi-publish@release/v1
# with:
# password: ${{ secrets.PYPI_TOKEN }}
# Actions secrets配置:
# Repo -> Settings -> Actions secrets and variables -> Repository secrets
# (2)通过PyPI with a Trusted Publisher的发布方式:
# 详细参见教程:https://docs.pypi.org/trusted-publishers/
# 只要在pypi里对包授权publish就行了。
# # Specifying a GitHub environment is optional, but strongly encouraged
# environment: release
# permissions:
# # IMPORTANT: this permission is mandatory for trusted publishing
# id-token: write
#
# 下面方式使用 Trusted Publisher 发布:
name: Publish Package📦 To PyPI
on:
# 允许手动触发
workflow_dispatch:
# 在仓库push时触发
#push:
# #branches:
# # - main
# # 推送 tag 是 v*.*.*时触发
# tags:
# - "v*.*.*"
# 在release时触发
release:
types: [published]
# 设置工作流访问仓库的权限:只读。
permissions:
contents: read
jobs:
publish_python_package_to_pypi:
name: Publish Package To PyPI
environment: pypi
permissions:
# IMPORTANT: this permission is mandatory for trusted publishing
id-token: write
# 只在推送tag时,才发布到 PyPI
#if: startsWith(github.ref, 'refs/tags/')
runs-on: ubuntu-latest
steps:
# clone 仓库
- name: Checkout Code
uses: actions/checkout@v4
# 设置 Python环境和版本
- name: Setup Python 🐍
uses: actions/setup-python@v5
with:
python-version: '3.x'
- name: Install Dependencies
run: |
python -m pip install --upgrade pip
pip install poetry
- name: Build
run: |
#poetry self add "poetry-dynamic-versioning[plugin]"
[ -f dist ] && rm -rvf dist
poetry build
- name: Publish Distribution 📦 To PyPI
uses: pypa/gh-action-pypi-publish@release/v1
#with:
# password: ${{ secrets.PYPI_TOKEN }}