Automatically Build Python Package using Github actions

Fredy Somy · 4 mins read ·
python pypi

Are you tired of manually building and deploying Python packages to PyPi? In this post, we are going to talk about the solution in detail. By the end of the post, you will not have to build your package again.

Prerequisites - What you need.

  • A github account
  • A PyPi account.
  • A Packagable Python project.
If you dont know how to package a project refer here.

How - Lets get started

  • Go to GitHub repository containing your python project.
  • Click on the Actions tab. Alt Text
  • Select the Publish Python package action. Alt Text
  • Notice that the new file which we are committing is in the .github directory and inside the workflows directory.
.github/workflows/python-publish.yml
  • Let’s see the code which is .yml file containing some commands to be executed.
    name: Upload Python Package
    
    on:
      release:
        types: [created]
    
    jobs:
      deploy:
    
        runs-on: ubuntu-latest
    
        steps:
        - uses: actions/[email protected]
        - name: Set up Python
          uses: actions/[email protected]
          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: $
            TWINE_PASSWORD: $
          run: |
            python setup.py sdist bdist_wheel
            twine upload dist/*
  • Let’s go over this line by line
    name: Upload Python Package
    
    on:
      release:
        types: [created]
  • The name: " " is the name of the workflow which you are creating.

  • The "on" depicts an event which here is the releases event.

  • If we want to run the workflow each time we push code .👇

    on:
      push:
        branches : [ master ]
  • Here the event is "Push" so each time you push code the workflow runs.

    jobs:
     .....
  • jobs tell the workflow what all jobs have to be done when the workflow is triggered.
    deploy:
        runs-on: ubuntu-latest
        steps:
        - uses: actions/[email protected]
        - name: Set up Python
          uses: actions/[email protected]
          with:
            python-version: '3.x'
  • runs-on specifies which OS it should run on.
  • Then the steps is a long list of works to be done.
  • Here the first job is to set up python and the - name means the name of the job running.
  • Here is a set of jobs running in my package.
Alt Text

  • Then after setting up python we have to install the dependencies.
    - name: Install dependencies
          run: |
            python -m pip install --upgrade pip
            python -m pip install -r requirements.txt
            pip install setuptools wheel twine
  • Here the run: | is the set of commands to be executed.
    • We are Upgrading pip.
    • Installing the dependencies in requirements.txt
    • And we are installing setuptools, wheel and twine.
  • setuptools and wheel are for packaging.
  • Twine is for uploading the package to Pypi.

  • Here is the final part to Build and Publish a package.
    - name: Build and publish
          env:
            TWINE_USERNAME: $
            TWINE_PASSWORD: $
          run: |
            python setup.py sdist bdist_wheel
            twine upload dist/*
  • The env: is some environment variables which you need ( Here your PyPi username and password.)

    • You can go to the repo settings and add secrets , which can be accessed by secrets.envname
  • In the "run :" you are seeing two commands.

    • Here python setup.py sdist bdist_wheel builds the package and add it into a dist/ folder.
  • And finally twine upload dist/* uploads the distributions to PyPi.

  • You can also set up tests if you want.

About Author

Fredy Somy
Full Stack Dev , Open Source Dev ,Javascript and Python,Love creating Desktop apps ,Bots and CLI's.

Please share your Feedback:

Did you enjoy reading or think it can be improved? Don’t forget to leave your thoughts in the comments section below! If you liked this article, please share it with your friends, and read a few more!