How to publish package to pypi in github ci

To publish a package to PyPI using GitHub CI, you can follow these steps:

  1. 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.

  2. 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.

  3. 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.

  4. 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 using twine.

  5. 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.

  6. Commit and push changes: Commit the workflow file and push it to your GitHub repository.

  7. 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.

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.

  • 26
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值