As AI technologies continue to grow and the software development landscape constantly changes, it is critical that software be developed quickly, reliably, and of high quality. In order to satisfy these requirements, continuous integration (CI) and continuous deployment (CD), have become critical methods. While CD automates the deployment process, CI automates the integration and testing of code modifications. This detailed manual will go over the CI/CD with Jenkins, significance of CI/CD, its advantages, and the problems it solves.
Using Jenkins, we will then develop CI/CD for Flask application deployment, expanding on the work we did in our previous article. You can follow the previous articles here to learn about it. The project we are using is on Github.
Automating the software delivery pipeline from code commit to deployment is the objective of CI/CD. By guaranteeing that code updates are merged and tested, continuous integration, or CI, lowers integration issues and ensures code quality. Continuous Integration (CI) is enhanced by CD by automating the deployment process and making it easier to distribute updates, adjustments, and new features in a reliable and timely manner.
CI/CD reduces human error, automates repetitive operations, and gives engineers quick feedback to optimize the software development lifecycle. Updates and new features are released more quickly, enabling teams to react swiftly to market and customer demands. Moreover, CI/CD improves code quality overall, team member contributions, and code transparency.
CI/CD is crucial in modern software development for several reasons:
Overall, CI/CD is essential for modern software development practices as it enables organizations to deliver high-quality software faster, more reliably, and with greater efficiency, ultimately driving business success and customer satisfaction.
Without CI/CD, software development teams and process may encounter several challenges, including:
Jenkins Pipeline is a collection of plugins designed to facilitate the setup and implementation of CI/CD processes. With the help of a domain-specific language (DSL) based on the programming language, users may define and automate Jenkins operations. This Pipeline allows teams to version manage build, test, and deployment pipelines, as well as application code, promoting collaboration, reliability, and speed in software delivery. Jenkins offers various types of jobs, including freestyle, pipeline, and multi-configuration projects, each tailored to different use cases in the CI/CD process.
Jenkins uses projects (also known as “jobs”) to perform its work. Projects are defined and run by Jenkins users. Jenkins offers several types of projects, including:
We will be working with the multi branch pipeline for GitHub.
Jenkins is an open-source automation server that facilitates CI/CD processes. It provides a platform for automating building, testing, and deployment tasks, making it an essential tool for modern software development pipelines. Jenkins integrates seamlessly with version control systems like Git, enabling developers to trigger builds and deployments automatically based on code changes.
Here in this article we are performing CI, in the continuation to this article, we will perform CICD with Docker.
Create an Ec2 instance and choose OS ubuntu. Also, don’t forget to add security groups with rule to allow port 8080, as port 8080 is for serving Jenkins.
sudo apt update
sudo apt upgrade
sudo apt install openjdk-17-jdk -y
wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key |sudo
gpg --dearmor -o /ubuntu/share/keyrings/jenkins.gpg
curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io.key |
sudo tee /usr/share/keyrings/jenkins-keyring.asc > /dev/null
echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc]
https://pkg.jenkins.io/debian-stable binary/ |
sudo tee /etc/apt/sources.list.d/jenkins.list > /dev/null
sudo apt install python3-pip
Now Put in the jenkins commands :
sudo apt install jenkins -y
sudo systemctl status jenkins
sudo systemctl enable --now jenkins
Output:
Now Open your public IP and add “:8080” to it in http and then in the ssh terminal put the command
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
Output:
Copy this token and paste it on the block given on the URL for Jenkins.
Then after login ans setting up the option as “Install suggested plugins”.
Ouput:
Let us begin by building our first pipeline for the project which we used earlier to deploy on cloud and now to put under continuous watch/integration.
The repo we are using is here:
You can simply try on the flask model which we used previously. However, if you want to see the changes, then you should use your repo, so that next time you add something, it automatically gets updated in the pipeline.
Also, for the implementation, there must be a Jenkins file in your repo in order for Jenkins to recognize it and read the pipeline saved in it.
Create a file name as “jenkinsfile” in the same directory. You can create while doing it in your local and then put all of the files and folders in a directory and on Github.
The Jenkins file we used:
pipeline {
agent any
environment {
VENV_PATH = 'myprojectenv'
FLASK_APP = 'myproject.py'
}
stages {
stage('Checkout') {
steps {
// Checkout code from a source control
//management system (e.g., Git)
git url: 'https://github.com/Geek-shikha
/Flask_model_sentiment_analysis.git', branch: 'master'
}
}
stage('Setup Virtual Environment') {
steps {
script {
// Check for the virtual environment,
// create it if it doesn't exist
sh 'bash -c "python3 -m venv $VENV_PATH"'
// Activate the virtual environment
sh 'bash -c "source $VENV_PATH/bin/activate"'
}
}
}
stage('Install dependencies') {
steps {
// Install any dependencies listed in requirements.txt
sh 'bash -c "source $VENV_PATH/bin/activate &&
pip install -r requirements.txt"'
}
}
stage('Test') {
steps {
// Run your tests here. This is just a placeholder.
// For example, if you had tests, you might run: pytest
echo "Assuming tests are run here. Please replace
this with actual test commands."
// sh "source $VENV_PATH/bin/activate && pytest"
}
}
stage('Deploy') {
steps {
script {
// Deploy your Flask app
// This step greatly depends on where and
// how you're deploying your app
// For example, if you're deploying to a server you control,
// you might use scp, rsync, or SSH commands
// If you're using a PaaS (Platform as a Service),
//you might use a specific CLI tool for that platform
echo 'Deploying application...'
// Example: sh 'scp -r . user@your_server:/path/to/deploy'
}
}
}
}
post {
always {
// Clean up after the pipeline runs
echo 'Cleaning up...'
sh 'rm -rf ${VIRTUAL_ENV_DIR}'
}
}
}
This Jenkins pipeline script defines a continuous integration and deployment (CI/CD) process for a Flask web application hosted on GitHub. Lets understand important components and concepts:
Start building by clicking on Build Now :
Now we can put a trigger so that it automatically checks for any github updates and integrate it in the pipeline, we are setting the trigger for 2 minutes.
To check this, we added a new wsgi.py and this will automatically trigger jenkins to identify the changes.
Here the repo is my created repo, You can do the same with your own created repo, remember it must have a jenkins file to identify. you can add or edit the existing file in your repo and then if you have to set the trigger it will identify and auto update it. otherwise you might have to click on build now everytime you want jenkins to identify the changes.
For continuous deployment, follow the next article on Dockerisation along with CICD pipeline.
Deploying Flask applications using CI/CD with Jenkins is a critical step in improving the effectiveness, dependability, and flexibility of software development processes. These processes help teams to quickly produce high-quality software in response to customer input and market demands by automating the integration, testing, and deployment of code changes. In this comprehensive guide we have examined the value of continuous integration and continuous development, its advantages and difficulties. We’ve also covered the practical side of configuring Jenkins CI/CD pipelines, covering everything from constructing pipelines and comprehending Jenkins jobs to managing the full deployment process.