Monday, November 24, 2025

thumbnail

Setting Up GitHub Actions for CI/CD in Python Projects

✅ Setting Up GitHub Actions for CI/CD in Python Projects


GitHub Actions lets you automatically test, build, and deploy your Python app whenever you push code or open a pull request. Here’s how to set it up.


๐Ÿ—‚️ 1. Create Your Workflow Directory


In your repository, create:


.github/workflows/



Inside, add a workflow file, for example:


.github/workflows/ci.yml


๐Ÿงช 2. Basic CI Workflow for Python


Here is a common example workflow that:


runs on pushes & pull requests


sets up Python


installs dependencies


runs tests using pytest


name: Python CI


on:

  push:

    branches: [ "main" ]

  pull_request:

    branches: [ "main" ]


jobs:

  build:

    runs-on: ubuntu-latest


    strategy:

      matrix:

        python-version: ["3.10", "3.11", "3.12"]


    steps:

      - name: Check out repository

        uses: actions/checkout@v4


        # Set up Python

      - name: Set up Python ${{ matrix.python-version }}

        uses: actions/setup-python@v5

        with:

          python-version: ${{ matrix.python-version }}


      - name: Install dependencies

        run: |

          python -m pip install --upgrade pip

          pip install -r requirements.txt


      - name: Run tests

        run: pytest


๐Ÿ” 3. Add Linting (flake8, black, ruff)


You can add a linting stage before running tests:


      - name: Lint with flake8

        run: |

          pip install flake8

          flake8 .



Or using ruff:


      - name: Lint with Ruff

        run: |

          pip install ruff

          ruff check .


๐Ÿ“ฆ 4. Build Python Package (optional)


If your project is a package, add:


      - name: Build package

        run: |

          pip install build

          python -m build



This generates:


dist/*.tar.gz


dist/*.whl


๐Ÿš€ 5. Publish to PyPI (optional deployment)


Create a separate workflow such as:


.github/workflows/publish.yml



Then add:


name: Publish to PyPI


on:

  push:

    tags:

      - "v*.*.*"   # deploy only on version tags


jobs:

  deploy:

    runs-on: ubuntu-latest


    steps:

      - uses: actions/checkout@v4


      - uses: actions/setup-python@v5

        with:

          python-version: 3.11


      - name: Install dependencies

        run: |

          pip install build twine


      - name: Build package

        run: python -m build


      - name: Publish package to PyPI

        run: |

          twine upload dist/*

        env:

          TWINE_USERNAME: "__token__"

          TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }}



You must set the PyPI token under:


GitHub repo → Settings → Secrets → Actions → PYPI_API_TOKEN


๐ŸŸฆ 6. Deploy a FastAPI / Flask app to a server or cloud (optional CD)


You can add deployment steps for:


Docker image build + push


GitHub Pages for static documentation


SSH deploy to a server


Example: Build and push Docker image:


      - name: Build and push Docker image

        uses: docker/build-push-action@v6

        with:

          push: true

          tags: username/myapp:latest


๐ŸŽ‰ Finished!


With this setup you get:


Feature Supported?

Automatic testing ✔️

Linting ✔️

Multi-version Python support ✔️

Packaging ✔️

Auto publishing to PyPI ✔️

Optional deployment ✔️ 

Learn Fullstack Python Training in Hyderabad

Read More

How to Use Git for Version Control in Full Stack Projects

Introduction to Git and GitHub for Full Stack Python Developers

Version Control and Deployment

How to Perform End-to-End Testing in Full Stack Python Applications

At Our Quality Thought Training Institute in Hyderabad

Get Directions

Subscribe by Email

Follow Updates Articles from This Blog via Email

No Comments

About

Search This Blog

Powered by Blogger.

Blog Archive