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:
- A GitHub account
- A Docker Hub account
- A Docker personal access token for authentication → How to create one
- Docker installed locally (for optional manual testing) Basic knowledge of Python and Git
📦 What We’ll Build
We’ll set up a pipeline that:
- Triggers on every push to the main branch
- Installs dependencies
- Builds a Docker image
- 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)
requirements.txt
flask==2.3.3
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"]
🔧 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 }}
🔐 Adding GitHub Secrets
Keep credentials safe by adding secrets:
- Go to Settings > Secrets and variables > Actions
- Add:
DOCKER_USERNAME
andDOCKER_PASSWORD
These allow the workflow to authenticate securely with Docker Hub.
✅ What Happens Now?
With this setup, every time you push to main branch:
- GitHub checks out the latest code
- Installs dependencies
- Builds a Docker image
- 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>
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:
- A simple Python + Flask app
- Dockerizing the app
- 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)