DEV Community

Cover image for CI/CD Pipeline with GitHub Actions: From Code to Deployment
Amol Mali
Amol Mali

Posted on

CI/CD Pipeline with GitHub Actions: From Code to Deployment

Modern software development demands speed, consistency, and automation. CI/CD (Continuous Integration and Continuous Deployment) is a crucial practice that automates the steps between writing code and deploying it to production.

In this article, we’ll walk through building a complete CI/CD pipeline using GitHub Actions — from committing code to deploying a containerized Python app.

Whether you’re a developer, DevOps engineer, or curious learner, this guide will help you go from code to deployment with confidence.


🧭 What is GitHub Actions?

GitHub Actions is a built-in CI/CD and automation platform in GitHub. It lets you trigger workflows based on events — like pushing code, creating pull requests, or scheduling jobs — and run scripts to build, test, or deploy applications.

✅ No external CI/CD server needed
✅ Fully integrated with your repository
✅ Free for public repos and generous free tier for private ones


🧰 Prerequisites

Before diving in, make sure you have the following:


📦 What We’ll Build

We’ll set up a pipeline that:

  1. Triggers on every push to the main branch
  2. Installs dependencies
  3. Builds a Docker image
  4. Pushes the image to Docker Hub

This entire process will be automated via GitHub Actions.


🔗 GitHub Repo

👉 github.com/amolxops/python-cicd-github-actions.git


🐍 The Python App

We’ll use a simple Flask application that returns “Hello, Welcome to the World of Github Actions CI/CD workflow!” when accessed. You can adapt this to any app stack you prefer.

app.py

from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello, Welcome to the World of Github Actions CI/CD workflow!"

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=3000)
Enter fullscreen mode Exit fullscreen mode

requirements.txt

flask==2.3.3
Enter fullscreen mode Exit fullscreen mode

Dockerfile

FROM python:3.12-slim

WORKDIR /app

COPY requirements.txt .
RUN pip install -r requirements.txt

COPY . .

EXPOSE 3000

CMD ["python", "app.py"]
FROM python:3.12-slim

WORKDIR /app

COPY requirements.txt .
RUN pip install -r requirements.txt

COPY . .

EXPOSE 3000

CMD ["python", "app.py"]
Enter fullscreen mode Exit fullscreen mode

🔧 Setting Up GitHub Actions Workflow

Create a workflow file at .github/workflows/ci-cd.yml:

name: CI/CD Pipeline

on:
  push:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: '3.11'

      - name: Install dependencies
        run: |
          pip install -r requirements.txt

  docker:
    needs: build
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Log in to Docker Hub
        run: echo "${{ secrets.DOCKER_PASSWORD }}" | docker login -u "${{ secrets.DOCKER_USERNAME }}" --password-stdin

      - name: Build and Push Docker Image
        run: |
          docker build -t ${{ secrets.DOCKER_USERNAME }}/python-cicd:${{ github.sha }} .
          docker push ${{ secrets.DOCKER_USERNAME }}/python-cicd:${{ github.sha }}
Enter fullscreen mode Exit fullscreen mode

🔐 Adding GitHub Secrets

Keep credentials safe by adding secrets:

  • Go to Settings > Secrets and variables > Actions
  • Add: DOCKER_USERNAME and DOCKER_PASSWORD These allow the workflow to authenticate securely with Docker Hub.

✅ What Happens Now?

With this setup, every time you push to main branch:

  1. GitHub checks out the latest code
  2. Installs dependencies
  3. Builds a Docker image
  4. Pushes it to your Docker Hub (tagged with the commit SHA)

✨ No more manual Docker builds or pushes!


📚 Bonus: Running the Container

After the image is pushed, run it anywhere Docker is installed:

docker run -p 3000:3000 your-username/python-cicd:<commit-sha>
Enter fullscreen mode Exit fullscreen mode

Want to deploy it to a server automatically? Add a new job using appleboy/ssh-action to deploy via SSH. Let me know if you'd like help extending the workflow!


💡 Wrap-Up

CI/CD pipelines make development faster and more reliable. Using GitHub Actions, you’ve automated everything from building a Docker image to pushing it to the registry.

This guide covered:

  1. A simple Python + Flask app
  2. Dockerizing the app
  3. Automating with GitHub Actions

Whether you’re building APIs, websites, or internal tools — this foundation sets you up for scalable and efficient deployments.


🙌 Let’s Connect

If you found this guide helpful or have questions, drop a comment or connect with me here on Medium. Follow for more content on DevOps, automation, and Linux tips.

Top comments (0)