Table of Contents


1. Prerequisites

Before starting, ensure you have the following installed on your system:

  • Ubuntu Linux (VM or local machine)
  • Python 3
  • Git
  • Docker
  • Jenkins
  • ngrok (for exposing Jenkins to the public)

2. Creating the Flask Application

Create a directory for your Flask app and the following files:

app.py

Copied!
from flask import Flask app = Flask(__name__) @app.route('/') def home():     return "Hello, World! This is our CI/CD Pipeline demo." if __name__ == "__main__":     app.run(host='0.0.0.0', port=5000)

requirements.txt

Copied!
flask

3. Writing the Dockerfile

Create a Dockerfile to containerize the Flask application.

Dockerfile

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

4. Installing and Configuring Jenkins

Step 1: Install Jenkins

  1. Add the Jenkins repository and install Jenkins:

bash

Copied!
wget -q -O - https://pkg.jenkins.io/debian/jenkins.io.key | sudo apt-key add - sudo sh -c 'echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list' sudo apt update sudo apt install jenkins -y
  1. Start and enable Jenkins:
Copied!
sudo systemctl start jenkins sudo systemctl enable jenkins
  1. Access Jenkins:
    Open http://localhost:8080 and follow the instructions to unlock Jenkins.

5. Creating a Jenkins Pipeline

Step 1: Create a Jenkinsfile

Create a Jenkinsfile in your project root directory:

Copied!
pipeline {     agent any     environment {         DOCKER_IMAGE = "my-flask-app"         DOCKER_TAG = "${env.BUILD_NUMBER}"     }     stages {         stage('Checkout Code') {             steps {                 git branch: 'main', url: 'https://github.com/yourusername/my-flask-app.git'             }         }         stage('Build Docker Image') {             steps {                 sh "docker build -t ${DOCKER_IMAGE}:${DOCKER_TAG} ."             }         }         stage('Remove Existing Container') {             steps {                 sh '''                 docker ps -a | grep flask-app && docker stop flask-app && docker rm flask-app || true                 '''             }         }         stage('Run New Container') {             steps {                 sh "docker run -d -p 5000:5000 --name flask-app ${DOCKER_IMAGE}:${DOCKER_TAG}"             }         }         stage('Clean Up Old Images') {             steps {                 sh "docker image prune -f"             }         }     }     post {         success {             echo 'Deployment succeeded!'         }         failure {             echo 'Deployment failed. Please check the logs.'         }     } }

Step 2: Create a Pipeline Job in Jenkins

  1. Go to Jenkins Dashboard > New Item.
  2. Enter a job name (e.g., my-flask-app-pipeline).
  3. Select Pipeline and click OK.
  4. In the Pipeline section, set the Definition to Pipeline script from SCM.
  5. Add your Git repository URL and branch.
  6. Click Save.

6. Setting Up GitHub Webhooks

  1. Go to Your GitHub Repository > Settings > Webhooks.
  2. Click Add webhook.
  3. Payload URL:

vbnet

  1. Content type: application/json
  2. Select Just the push event.
  3. Click Add webhook.

7. Exposing Jenkins to the Internet Using ngrok

  1. Install ngrok:

bash

Copied!
wget https://bin.equinox.io/c/bNyj1mQVY4c/ngrok-stable-linux-amd64.zip unzip ngrok-stable-linux-amd64.zip sudo mv ngrok /usr/local/bin
  1. Authenticate ngrok:

bash

Copied!
ngrok config add-authtoken <YOUR_AUTH_TOKEN>
  1. Start ngrok:

bash

Copied!
ngrok http 8080
  1. Copy the Public URL provided by ngrok and use it for the GitHub webhook.

8. Configuring Firewall Rules

For Ubuntu (ufw)

  1. Enable the firewall:

bash

Copied!
sudo ufw enable
  1. Allow port 5000 (for Flask app):

bash

Copied!
sudo ufw allow 5000
  1. Allow port 8080 (for Jenkins):

bash

Copied!
sudo ufw allow 8080

9. Testing the Pipeline

  1. Make a Change in Your Flask App:

bash

Copied!
git add app.py git commit -m "Test CI/CD pipeline" git push origin main
  1. Check Jenkins:
    The pipeline should trigger automatically, build the Docker image, and deploy the Flask app.
  2. Access the Flask App:
    Open your browser and go to:

arduino


10. Conclusion

Congratulations! 🎉 You have successfully set up a CI/CD pipeline for your Flask app using Jenkins, Docker, and GitHub. This setup ensures that every time you push changes to your GitHub repository, Jenkins automatically builds a new Docker image and deploys it. Using ngrok allows you to expose your local Jenkins instance to the public Internet for webhook integration.

This CI/CD pipeline can be extended to include automated testing, linting, and more advanced deployment strategies.

Happy coding! 🚀


Leave a Reply

Your email address will not be published. Required fields are marked *