天天看點

Python實戰:GitHub Actions自動建構pypa/gh-action-pypi-publish自動釋出新版本到pypi

每次release新包的時候,我們可以利用GitHub Actions自動建構,釋出到pypi

使用pypa/gh-action-pypi-publish釋出

.github/workflows/python-publish.yml

# https://github.com/actions/starter-workflows/blob/main/ci/python-publish.yml
name: Upload Python Package

on:
  release:
    types: [published]

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 build
      - name: Build package
        run: python -m build

      - name: Publish package
        # https://github.com/pypa/gh-action-pypi-publish
        uses: pypa/gh-action-pypi-publish@27b31702a0e7fc50959f5ad993c78deac1bdfc29
        with:
          user: mouday
          password: ${{ secrets.PYPI_API_TOKEN }}
          skip_existing: true      

如果發現釋出失敗,嘗試使用第二種方式

# https://github.com/actions/starter-workflows/blob/main/ci/python-publish.yml
name: Upload Python Package

on:
  release:
    types: [published]

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: ${{ secrets.PYPI_USERNAME }}
          TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
        run: |
            twine check dist/*
            twine upload dist/*