🚨 New Course Alert! 🚨 Complete MLOps Bootcamp With 10+ End To End ML Projects is now live! 🎉 Enroll Now

MLOPS Tutorial: Automating Workflow Of CI/CD for Dockerized Flask App Using Github Action

Hello all, my name is Krish Naik, and welcome to my YouTube channel. First of all, a very Happy Teachers Day to everyone out there. In this tutorial, we’re going to delve into GitHub Actions as we develop an end-to-end project with an automated workflow.

The project involves creating a simple Flask application, converting it into a Docker image, and then deploying or pushing it to Docker Hub. The entire process will be automated using CI/CD pipelines developed with GitHub Actions.

If you’d like to learn more from me, I have several affordable Udemy courses. You can find the links in the description of my YouTube video.

Introduction to the Workflow

In this tutorial, we’re focusing on implementing a complete end-to-end project featuring both CI and CD pipelines. We'll start with the basics and gradually move on to more advanced topics.

Project Breakdown

Here's a brief overview of what we’re going to do:

  • Create a Flask application
  • Write unit test cases for the Flask app using PyTest
  • Automate the testing, building, and deployment using GitHub Actions
  • Deploy the Docker image to Docker Hub

Setting up the Project

First, create a folder for your project. Open Visual Studio Code or any other code editor and set up the following files:

  • requirements.txt to list required dependencies like Flask and PyTest
  • app.py for the Flask application code
  • test_app.py for unit test cases
  • Dockerfile to create the Docker image
  • gitignore to omit unnecessary files like environment folders

Implementing the Workflow

Here's a step-by-step implementation of the desired workflow:

Creating the Flask Application


from flask import Flask
app = Flask(__name__)

@app.route('/')
def home():
    return "Hello, World!"

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=5000)
    

Writing Unit Tests


from app import app

def test_home():
    response = app.test_client().get('/')
    assert response.status_code == 200
    assert response.data == b"Hello, World!"
    

Creating the Dockerfile


FROM python:3.9-slim
WORKDIR /app
COPY . /app
RUN pip install -r requirements.txt
EXPOSE 5000
CMD ["python", "app.py"]
    

Setting up GitHub Actions

Create a new folder named .github/workflows and inside it, create a file named cicd.yaml with the following content:


name: CI/CD for Dockerized Flask App

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

jobs:
  build-and-test:
    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.9

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install flask pytest

      - name: Run tests
        run: pytest

  build-and-publish:
    needs: build-and-test
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v1

      - name: Log in to Docker Hub
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}

      - name: Build and Push Docker image
        uses: docker/build-push-action@v2
        with:
          context: .
          push: true
          tags: ${{ secrets.DOCKER_USERNAME }}/flask-test-app:latest
    

Setting Up Secrets

Create your Docker Hub account and generate a personal access token. Go to your GitHub repository's settings, navigate to Secrets, and add secrets for DOCKER_USERNAME and DOCKER_PASSWORD.

Executing the Workflow

Push your code to the repository, and GitHub Actions will automatically trigger the workflow. The stages include:

  • Checkout code
  • Setup Python and install dependencies
  • Run tests
  • Checkout code and set up Docker Buildx
  • Log in to Docker Hub
  • Build and push Docker image

Conclusion

This tutorial provides a comprehensive guide to automating your CI/CD pipeline using GitHub Actions for a Dockerized Flask application. This workflow ensures continuous integration and continuous deployment, streamlining your development process.

If you found this tutorial helpful, be sure to check out the video for a more detailed walkthrough: