2-how-to-set-up-cicd-pipelines-for-flask-applications-using-docker-and-github-actions.html

How to Set Up CI/CD Pipelines for Flask Applications Using Docker and GitHub Actions

In today's fast-paced software development landscape, Continuous Integration (CI) and Continuous Deployment (CD) have become essential practices for delivering high-quality applications efficiently. If you're developing a Flask application and want to streamline your deployment process, integrating Docker and GitHub Actions into your workflow is a powerful solution. This article will walk you through setting up a CI/CD pipeline for your Flask application, making the process as smooth and efficient as possible.

What is CI/CD?

Before diving into the implementation, let’s clarify what CI/CD means:

  • Continuous Integration (CI): This practice involves automatically testing and integrating code changes into a shared repository several times a day. The goal is to ensure that new code does not break existing functionality.

  • Continuous Deployment (CD): This extends CI by automatically deploying all code changes to production after passing tests. This helps in delivering features and bug fixes to users without manual intervention.

Why Use Docker?

Docker simplifies the deployment process by allowing you to package your application and its dependencies into a container. This container can run consistently across various environments, ensuring that "it works on my machine" is no longer a valid excuse.

Advantages of Using Docker:

  • Isolation: Each container is isolated from others, minimizing conflicts.
  • Consistency: Every environment runs the same container, ensuring consistent behavior.
  • Scalability: Containers can be easily scaled based on demand.

Why GitHub Actions?

GitHub Actions is a powerful tool for automating workflows directly from your GitHub repository. It allows you to set up workflows that can build, test, and deploy your application whenever code changes are made.

Benefits of GitHub Actions:

  • Integration: Seamlessly integrates with your GitHub repositories.
  • Flexibility: Supports a variety of triggers and actions, allowing for customized workflows.
  • Community: A rich marketplace of pre-built actions that can save time.

Step-by-Step Setup

Step 1: Create a Flask Application

First, let's create a simple Flask application. You can create a directory for your project and set up a basic Flask app.

mkdir flask-docker-ci-cd
cd flask-docker-ci-cd
python3 -m venv venv
source venv/bin/activate
pip install Flask

Create a file named app.py:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello():
    return "Hello, Flask with Docker and CI/CD!"

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

Step 2: Create a Dockerfile

Next, we need to create a Dockerfile that describes how to build our Flask application into a Docker image.

Create a file named Dockerfile in the project root:

# Use the official Python image from Docker Hub
FROM python:3.9-slim

# Set the working directory
WORKDIR /app

# Copy the requirements file and install dependencies
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt

# Copy the application code
COPY . .

# Expose the application port
EXPOSE 5000

# Command to run the application
CMD ["python", "app.py"]

Step 3: Create a Requirements File

We need to specify our application dependencies. Create a file named requirements.txt:

Flask==2.0.1

Step 4: Create a GitHub Actions Workflow

Now that our application is ready, we can create a GitHub Actions workflow. Create a directory named .github/workflows and add a file named ci-cd.yml:

name: CI/CD Pipeline

on:
  push:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest

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

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

    - name: Build Docker image
      run: |
        docker build . -t my-flask-app

    - name: Run tests
      run: |
        docker run --rm my-flask-app python -m unittest discover

    - name: Push to Docker Hub
      env:
        DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
        DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
      run: |
        echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin
        docker tag my-flask-app myusername/my-flask-app:latest
        docker push myusername/my-flask-app:latest

Step 5: Configure Secrets in GitHub

To enable Docker Hub login, you need to add your Docker Hub credentials to GitHub secrets: 1. Go to your GitHub repository. 2. Navigate to Settings > Secrets > Actions. 3. Add secrets named DOCKER_USERNAME and DOCKER_PASSWORD.

Step 6: Test Your Pipeline

Now that everything is set up, commit your changes and push them to the main branch:

git init
git add .
git commit -m "Initial commit"
git remote add origin <your-github-repo-url>
git push -u origin main

This push will trigger your GitHub Actions workflow, building the Docker image, running tests, and pushing the image to Docker Hub.

Troubleshooting Tips

  • Ensure that your Dockerfile is correctly configured to install all dependencies.
  • Check the logs in GitHub Actions for any errors during build or test steps.
  • Validate your Docker Hub credentials if the push fails.

Conclusion

Setting up a CI/CD pipeline for Flask applications using Docker and GitHub Actions not only automates your deployment process but also enhances the reliability and consistency of your application delivery. By following the steps outlined in this article, you can ensure that your Flask application is continuously integrated and deployed, allowing you to focus more on developing features and less on deployment headaches.

With the increasing importance of CI/CD in modern development, mastering these skills will significantly enhance your development workflow. Start implementing these practices today and watch your productivity soar!

SR
Syed
Rizwan

About the Author

Syed Rizwan is a Machine Learning Engineer with 5 years of experience in AI, IoT, and Industrial Automation.