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 applicationDockerfile
: Instructions to containerize the applicationJenkinsfile
: The pipeline configurationrequirements.txt
: Dependencies for the applicationtests/
: 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.'
}
}
}
3. Installing and Configuring Jenkins
To install Jenkins, run:
sudo apt update && sudo apt install jenkins -y
to start Jenkins:
sudo service jenkins start
Then, access Jenkins in the browser using:
http://localhost:8080
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:
- Check running containers:
docker ps
- Fetch all quotes:
curl http://127.0.0.1:5000/api/quotes
- Fetch a random quote:
curl http://127.0.0.1:5000/api/quotes/random
- Fetch a specific quote by ID:
curl http://127.0.0.1:5000/api/quotes/1
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
Top comments (0)