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)