DEV Community

Adetokun Halimat
Adetokun Halimat

Posted on

2

Building a CI/CD Pipeline for a Flask API Using Jenkins and Docker

CI/CD Architecture

Introduction

Continuous Integration and Continuous Deployment (CI/CD) is a crucial part of modern software development. In this article, I will walk you through how I built a CI/CD pipeline for a Flask-based Random Quote API using Jenkins and Docker. This project automates the process of testing and deploying the API, ensuring that any updates to the repository are immediately built, tested, and deployed without manual intervention.

Project Overview

The Random Quote API is a simple RESTful API that returns quotes. It includes endpoints to:

  • Fetch all quotes

  • Fetch a random quote

  • Fetch a specific quote by ID

The entire project follows DevOps best practices by implementing CI/CD automation.

Tech Stack

  • Flask: Web framework for Python

  • Docker: Containerization tool

  • Jenkins: CI/CD automation server

  • GitHub: Version control

Setting Up the CI/CD Pipeline

1. Setting Up the GitHub Repository

The first step is to host the project on GitHub. The repository contains:

  • app.py: The main Flask application

  • Dockerfile: Instructions to containerize the application

  • Jenkinsfile: The pipeline configuration

  • requirements.txt: Dependencies for the application

  • tests/: Unit tests for the API

2. Writing the Jenkinsfile

Jenkinsfile is a script that defines the automation process in Jenkins. Below is the Jenkinsfile used for this project:

pipeline {
    agent any

    stages {
        stage('Checkout Code') {
            steps {
                git branch: 'main', url: 'https://github.com/your-username/random-quote-api.git'
            }
        }

        stage('Build Docker Image') {
            steps {
                sh 'DOCKER_BUILDKIT=1 docker build -t random-quote-api .'
            }
        }

        stage('Run Unit Tests') {
            steps {
                sh 'docker run --rm random-quote-api pytest'
            }
        }

        stage('Deploy Application') {
            steps {
                sh 'docker run -d -p 5000:5000 random-quote-api'
            }
        }
    }

    post {
        success {
            echo 'Pipeline executed successfully!'
        }
        failure {
            echo 'Pipeline failed. Check logs for details.'
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

3. Installing and Configuring Jenkins

To install Jenkins, run:

sudo apt update && sudo apt install jenkins -y
Enter fullscreen mode Exit fullscreen mode

to start Jenkins:

sudo service jenkins start
Enter fullscreen mode Exit fullscreen mode

Then, access Jenkins in the browser using:

http://localhost:8080
Enter fullscreen mode Exit fullscreen mode

4. Setting Up the Jenkins Pipeline

  • Create a new Pipeline project in Jenkins.

  • Link the repository to Jenkins.

  • Ensure Jenkins has Git and Docker installed.

  • Add necessary credentials (GitHub, Docker permissions).

5. Running the CI/CD Pipeline

Every time code is pushed to the repository:

  • Jenkins pulls the latest changes.

  • Docker builds a new container.

  • Unit tests are executed.

  • If tests pass, the application is deployed automatically.

Demonstrating the API

To test the API after deployment:

  1. Check running containers:
docker ps
Enter fullscreen mode Exit fullscreen mode
  1. Fetch all quotes:
curl http://127.0.0.1:5000/api/quotes
Enter fullscreen mode Exit fullscreen mode
  1. Fetch a random quote:
curl http://127.0.0.1:5000/api/quotes/random
Enter fullscreen mode Exit fullscreen mode
  1. Fetch a specific quote by ID:
curl http://127.0.0.1:5000/api/quotes/1
Enter fullscreen mode Exit fullscreen mode

Challenges and Learnings

Challenges:

  • Setting up Jenkins on WSL required extra configuration.

  • Jenkins initially lacked access to Docker, requiring user permission fixes.

  • Troubleshooting firewall and port issues.

Learnings:

  • Automating deployment saves time and reduces errors.

  • CI/CD ensures code quality by running tests before deployment.

  • Docker makes application deployment more scalable and portable.

Conclusion

This project highlights how Jenkins and Docker can be leveraged to automate the testing and deployment of a Python Flask API. Setting up a CI/CD pipeline ensures seamless integration of code updates, leading to a more efficient and reliable development workflow.

By following these steps, you can integrate CI/CD into your projects and ensure a smoother, more automated development process.

Future Plans?

  • Deploy the API to a cloud provider (AWS, GCP, or Heroku).

  • Implement database integration to store and manage quotes, as the current implementation temporarily uses JSON for storage.

  • Extend CI/CD to include automated security checks.

Would love to hear your thoughts! Let me know if you have implemented CI/CD in your projects and the challenges you faced.

Here is a link to the repository on Github

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs