To publish a package to PyPI using GitHub CI, you can follow these steps:
-
Create a PyPI account: Before publishing a package, you need to create an account on PyPI (https://pypi.org/) if you don’t have one already.
-
Generate PyPI API token: Once you have a PyPI account, generate an API token. Go to your PyPI account settings and create a new API token. Make sure to save the token securely as you will need it later.
-
Prepare your package: Ensure that your package is properly structured and contains all the necessary files. Make sure you have a
setup.py
file that defines your package metadata and dependencies. -
Configure GitHub CI workflow: In your GitHub repository, create a workflow file (e.g.,
.github/workflows/publish.yml
) to define the CI workflow. Here’s an example workflow file:name: Publish to PyPI on: push: branches: - main jobs: publish: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v2 - name: Set up Python uses: actions/setup-python@v2 with: python-version: 3.x - name: Install dependencies run: | python -m pip install --upgrade pip pip install setuptools wheel twine - name: Build and publish env: TWINE_USERNAME: __token__ TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }} run: | python setup.py sdist bdist_wheel twine upload dist/*
This workflow will trigger on every push to the
main
branch. It sets up Python, installs dependencies, builds the package, and publishes it to PyPI usingtwine
. -
Add PyPI API token as a secret: In your GitHub repository, go to “Settings” > “Secrets” and create a new secret named
PYPI_API_TOKEN
. Paste the PyPI API token generated in step 2 as the value for this secret. -
Commit and push changes: Commit the workflow file and push it to your GitHub repository.
-
Verify the workflow: Go to the “Actions” tab in your GitHub repository to see the workflow running. It should build and publish your package to PyPI.文章来源:https://www.toymoban.com/news/detail-819609.html
That’s it! Your package will now be published to PyPI automatically whenever you push changes to the main
branch. Make sure to update the workflow file and adjust it according to your package’s specific requirements.文章来源地址https://www.toymoban.com/news/detail-819609.html
到了这里,关于How to publish package to pypi in github ci的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!