DEV Community

Cover image for Automate Build and deploy of Python Package using Github actions.
Fredy Somy
Fredy Somy

Posted on

Automate Build and deploy of Python Package using Github actions.

Are you tired of manually building and deploying Python packages to PyPi. Well here is the solution.

What you need.

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

Lets get started

  • Go to the repo containing your python project.
  • Click on Actions tab. A window appears. Alt Text
  • Select the Publish Python package action. Alt Text
  • Notice that the new file which we are commiting is in the .github folder and inside the workflows folder.
.github/workflows/python-publish.yml

Enter fullscreen mode Exit fullscreen mode
  • Lets see the code which is .yml file containing some commands to be excecuted .
name: Upload Python Package

on:
  release:
    types: [created]

jobs:
  deploy:

    runs-on: ubuntu-latest

    steps:
    - 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: ${{ secrets.PYPI_USERNAME }}
        TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
      run: |
        python setup.py sdist bdist_wheel
        twine upload dist/*
Enter fullscreen mode Exit fullscreen mode
  • Let me explain Line by line.
name: Upload Python Package

on:
  release:
    types: [created]
Enter fullscreen mode Exit fullscreen mode
  • The name : " " is the name of the workflow which you are creating.

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

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

on:
  push:
    branches : [ master ]

Enter fullscreen mode Exit fullscreen mode
  • Here the event is "Push" so each time you push code the workflow runs.
jobs:
 .....
Enter fullscreen mode Exit fullscreen mode
  • Jobs tells the workflow what all jobs have to be done when the workflow is triggered.
deploy:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - name: Set up Python
      uses: actions/setup-python@v2
      with:
        python-version: '3.x'
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode
  • 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: ${{ secrets.PYPI_USERNAME }}
        TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
      run: |
        python setup.py sdist bdist_wheel
        twine upload dist/*
Enter fullscreen mode Exit fullscreen mode
  • 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.

Contact

  • Feel free to email me for Bugs Found or queries.

  • I will be happy to help.

  • Cover image taken from here

Discussion (0)