[ad_1]
Introduction
As AI applied sciences proceed to develop and the software program growth panorama continually adjustments, it’s crucial that software program be developed rapidly, reliably, and of top quality. In an effort to fulfill these necessities, steady integration (CI) and steady deployment (CD), have turn into crucial strategies. Whereas CD automates the deployment course of, CI automates the combination and testing of code modifications. This detailed guide will go over the CI/CD with Jenkins, significance of CI/CD, its benefits, and the issues it solves.
![Implementing CI/CD with Jenkins for Flask Application Deployment](https://cdn.analyticsvidhya.com/wp-content/uploads/2024/04/jenkins-1.png)
Utilizing Jenkins, we are going to then develop CI/CD for Flask software deployment, increasing on the work we did in our earlier article. You’ll be able to comply with the earlier articles right here to study it. The mission we’re utilizing is on Github.
Understanding CI/CD
Automating the software program supply pipeline from code decide to deployment is the target of CI/CD. By guaranteeing that code updates are merged and examined, steady integration, or CI, lowers integration points and ensures code high quality. Steady Integration (CI) is enhanced by CD by automating the deployment course of and making it simpler to distribute updates, changes, and new options in a dependable and well timed method.
How CI/CD Works ?
CI/CD reduces human error, automates repetitive operations, and provides engineers fast suggestions to optimize the software program growth lifecycle. Updates and new options are launched extra rapidly, enabling groups to react swiftly to market and buyer calls for. Furthermore, CI/CD improves code high quality general, workforce member contributions, and code transparency.
!["](https://av-eks-lekhak.s3.amazonaws.com/media/__sized__/article_images/jenkins_FHEYnh3-thumbnail_webp-600x300.webp)
Want of CI/CD
CI/CD is essential in trendy software program growth for a number of causes:
Sooner Time to Market: These options automate software program supply, enabling builders to deploy updates extra reliably, offering a aggressive benefit and swift market response.
Improved Software program High quality: CI ensures common testing and integration of code updates right into a shared repository, figuring out bugs early in growth and decreasing the chance of breaking adjustments or introducing bugs.
Diminished Human Effort and Errors: CI/CD automates software program supply phases, decreasing human involvement, error danger, and growth time, permitting builders to deal with different tasks and buyer wants.
Enhanced Collaboration and Communication: CI/CD instruments facilitate collaboration amongst growth, testing, and operations groups by offering visibility into your complete software program supply pipeline, enhancing communication, coordination, and alignment of efforts.
Scalability and Flexibility: CI/CD practices are adaptable to numerous mission sizes and methodologies, providing flexibility by instruments that help programming languages, platforms, and cloud environments.
Steady Suggestions Loop: CI/CD facilitates a steady suggestions loop by gathering metrics and efficiency information all through the software program supply course of, enabling groups to determine bottlenecks, optimize workflows, and make data-driven choices.
Total, CI/CD is crucial for contemporary software program growth practices because it allows organizations to ship high-quality software program sooner, extra reliably, and with larger effectivity, in the end driving enterprise success and buyer satisfaction.
Challenges with out CI/CD
With out CI/CD, software program growth groups and course of might encounter a number of challenges, together with:
Handbook Processes susceptible to error: The time-consuming and human error-prone nature of guide integration, testing, and deployment procedures causes delays and inconsistencies.
Ineffective launch cycles: Within the absence of automation, launch cycles are exhausting and gradual, making it harder to supply updates and new options on time.
Lack of visibility and suggestions: Builders are unable to obtain quick suggestions on the performance and high quality of their code within the absence of automated testing and deployment, which may end in issues in the actual world.
Understanding Jenkins Jobs and Pipeline
Jenkins Pipeline is a group of plugins designed to facilitate the setup and implementation of CI/CD processes. With the assistance of a domain-specific language (DSL) based mostly on the programming language, customers might outline and automate Jenkins operations. This Pipeline permits groups to model handle construct, take a look at, and deployment pipelines, in addition to software code, selling collaboration, reliability, and velocity in software program supply. Jenkins presents varied varieties of jobs, together with freestyle, pipeline, and multi-configuration tasks, every tailor-made to totally different use instances within the CI/CD course of.
Jobs: Jenkins can deal with a variety of tasks, together with Multibranch Pipeline, Freestyle, and Maven tasks. Each type of job has distinctive capabilities and configuration choices.
Configuration: A job can have varied configurations, together with parameters, construct triggers, supply code repositories, construct phases, and post-build actions.
Execution: Jenkins assigns sources and executes job configuration directions, monitoring progress and output by the net interface.
Integration: Jenkins jobs could be built-in with varied construct instruments, testing frameworks, model management programs, and deployment platforms like Docker and Kubernetes for environment friendly supply processes.
Jenkins makes use of tasks (also referred to as “jobs”) to carry out its work. Initiatives are outlined and run by Jenkins customers. Jenkins presents a number of varieties of tasks, together with:
Pipeline
Multibranch Pipeline
Group folders
Freestyle
Multi-configuration (matrix)
Maven
Exterior job
We can be working with the multi department pipeline for GitHub.
Implementing CI/CD with Jenkins for Flask Utility Deployment
Jenkins is an open-source automation server that facilitates CI/CD processes. It offers a platform for automating constructing, testing, and deployment duties, making it an important device for contemporary software program growth pipelines. Jenkins integrates seamlessly with model management programs like Git, enabling builders to set off builds and deployments mechanically based mostly on code adjustments.
Steps to Implement CI/CD with Jenkins
Arrange Jenkins: Set up Jenkins on a server and configure it based on your necessities. Jenkins could be put in on-premises or on a cloud server.
Join Jenkins to GitHub: Configure Jenkins to connect with your GitHub repository the place the Flask software code is hosted. This permits Jenkins to drag code adjustments and set off builds mechanically.
Configure Jenkins Pipeline: Create a Jenkins pipeline to outline the phases of the CI/CD course of. A typical pipeline consists of phases equivalent to constructing, testing, and deployment.
Implement Testing: Outline automated exams to make sure the standard and performance of the Flask software. These exams can embody unit exams, integration exams, and end-to-end exams.
Construct and Take a look at Stage: Arrange Jenkins to mechanically construct the Flask software each time there’s a brand new code commit. After constructing, execute the automated exams to confirm the appliance’s performance.
Deployment Stage: As soon as the exams cross efficiently, deploy the Flask software utilizing Gunicorn and Nginx. Jenkins can execute deployment scripts or instructions to add the appliance to the cloud server.
Monitor and Iterate: Monitor the CI/CD pipeline for any failures or points. Constantly enhance the pipeline by incorporating suggestions and making optimizations.
Right here on this article we’re performing CI, within the continuation to this text, we are going to carry out CICD with Docker.
Step1 : Set-up ec2 Consumer and Set up Dependencies
Create an Ec2 occasion and select OS ubuntu. Additionally, don’t overlook so as to add safety teams with rule to permit port 8080, as port 8080 is for serving Jenkins.
![Set-up ec2 User and Install Dependencies](https://cdn.analyticsvidhya.com/wp-content/uploads/2024/04/inboundrule.png)
Then SSH login into your ec2 and distant use it and set up the dependencies.
“sudo apt set up openjdk-17-jdk -y“: This command installs the OpenJDK 17 JDK package deal. Jenkins requires Java to run, and OpenJDK is a well-liked selection for Java growth environments.
“wget -q -O – https://pkg.jenkins.io/debian-stable/jenkins.io.key |sudo gpg –dearmor -o/ubuntu/share/keyrings/jenkins.gpg“: This command downloads the Jenkins GPG key and shops it in /ubuntu/share/keyrings/jenkins.gpg. This secret is used to confirm the authenticity of Jenkins packages throughout set up.
“curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo tee /usr/share/keyrings/jenkins-keyring.asc > /dev/null” : This command downloads the Jenkins GPG key and shops it in /usr/share/keyrings/jenkins-keyring.asc. That is another method to obtain the important thing.
“echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] https://pkg.jenkins.io/debian-stable binary/ | sudo tee /and so forth/apt/sources.record.d/jenkins.record > /dev/null” : This command provides the Jenkins repository to the system’s package deal sources record. It specifies the situation of Jenkins packages and the important thing used for package deal signing.
sudo apt replace
sudo apt improve
sudo apt set up 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 /and so forth/apt/sources.record.d/jenkins.record > /dev/null
sudo apt set up python3-pip
Now Put within the jenkins instructions :
sudo apt set up jenkins -y
sudo systemctl standing jenkins
sudo systemctl allow –now jenkins
Output:
![CI/CD with Jenkins](https://cdn.analyticsvidhya.com/wp-content/uploads/2024/04/001.png)
Now Open your public IP and add “:8080” to it in http after which within the ssh terminal put the command
sudo cat /var/lib/jenkins/secrets and techniques/initialAdminPassword
Output:
![Output](https://cdn.analyticsvidhya.com/wp-content/uploads/2024/04/token.png)
Copy this token and paste it on the block given on the URL for Jenkins.
![](https://cdn.analyticsvidhya.com/wp-content/uploads/2024/04/loginPage.png)
![](https://cdn.analyticsvidhya.com/wp-content/uploads/2024/04/01-1.png)
Then after login ans establishing the choice as “Set up instructed plugins”.
Ouput:
![CI/CD with Jenkins](https://cdn.analyticsvidhya.com/wp-content/uploads/2024/04/04-2.png)
Allow us to start by constructing our first pipeline for the mission which we used earlier to deploy on cloud and now to place below steady watch/integration.
Step2: Constructing Pipeline
Click on on new merchandise
Enter a reputation and choose Pipeline – MultiBranch right here
![Building Pipeline](https://cdn.analyticsvidhya.com/wp-content/uploads/2024/04/05-2.png)
Then configure the primary pipeline after including an outline and including the URL of your repo after which choose the department as grasp and put it aside.
The repo we’re utilizing is right here:
![CI/CD with Jenkins](https://cdn.analyticsvidhya.com/wp-content/uploads/2024/04/06-2.png)
You’ll be able to merely attempt on the flask mannequin which we used beforehand. Nonetheless, if you wish to see the adjustments, then you must use your repo, in order that subsequent time you add one thing, it mechanically will get up to date within the pipeline.
Additionally, for the implementation, there should be a Jenkins file in your repo to ensure that Jenkins to acknowledge it and browse the pipeline saved in it.
Create a file title as “jenkinsfile” in the identical listing. You’ll be able to create whereas doing it in your native after which put all the information and folders in a listing and on Github.
![CI/CD with Jenkins](https://cdn.analyticsvidhya.com/wp-content/uploads/2024/04/07-3.png)
The Jenkins file we used:
pipeline {
agent any
atmosphere {
VENV_PATH = ‘myprojectenv’
FLASK_APP = ‘myproject.py’
}
phases {
stage(‘Checkout’) {
steps {
// Checkout code from a supply management
//administration system (e.g., Git)
git url: ‘https://github.com/Geek-shikha
/Flask_model_sentiment_analysis.git’, department: ‘grasp’
}
}
stage(‘Setup Digital Atmosphere’) {
steps {
script {
// Verify for the digital atmosphere,
// create it if it does not exist
sh ‘bash -c “python3 -m venv $VENV_PATH”‘
// Activate the digital atmosphere
sh ‘bash -c “supply $VENV_PATH/bin/activate”‘
}
}
}
stage(‘Set up dependencies’) {
steps {
// Set up any dependencies listed in necessities.txt
sh ‘bash -c “supply $VENV_PATH/bin/activate &&
pip set up -r necessities.txt”‘
}
}
stage(‘Take a look at’) {
steps {
// Run your exams right here. That is only a placeholder.
// For instance, when you had exams, you would possibly run: pytest
echo “Assuming exams are run right here. Please substitute
this with precise take a look at instructions.”
// sh “supply $VENV_PATH/bin/activate && pytest”
}
}
stage(‘Deploy’) {
steps {
script {
// Deploy your Flask app
// This step significantly will depend on the place and
// the way you’re deploying your app
// For instance, when you’re deploying to a server you management,
// you would possibly use scp, rsync, or SSH instructions
// In the event you’re utilizing a PaaS (Platform as a Service),
//you would possibly use a selected CLI device for that platform
echo ‘Deploying software…’
// Instance: sh ‘scp -r . consumer@your_server:/path/to/deploy’
}
}
}
}
publish {
all the time {
// Clear up after the pipeline runs
echo ‘Cleansing up…’
sh ‘rm -rf ${VIRTUAL_ENV_DIR}’
}
}
}
Methods to Write Jenkins File ?
This Jenkins pipeline script defines a steady integration and deployment (CI/CD) course of for a Flask net software hosted on GitHub. Lets perceive necessary parts and ideas:
Pipeline Declaration
pipeline { … }: This defines your complete Jenkins pipeline.
Agent
agent any: This tells Jenkins to execute the pipeline on any out there agent (Jenkins employee node).
Atmosphere
atmosphere { … }: This block defines atmosphere variables used all through the pipeline.
VENV_PATH: Specifies the trail for the digital atmosphere.
FLASK_APP: Specifies the title of the Flask software file.
Phases
phases { … }: This block incorporates a number of phases representing totally different steps of the pipeline.
Checkout: Clones the supply code repository from GitHub.
Setup Digital Atmosphere: Creates and prompts a digital atmosphere for the mission.
Set up Dependencies: Installs Python dependencies listed in necessities.txt.
Take a look at: Placeholder for operating exams. Right here, it solely echoes a message.
Deploy: Placeholder for deploying the Flask software. At the moment, it solely echoes a message. we are going to full this within the upcoming article with docker and CICD
Steps
steps { … }: This block incorporates particular person steps inside every stage.
sh ‘…’: Executes shell instructions. Used for varied duties equivalent to making a digital atmosphere, activating it, putting in dependencies, and so forth.
script { … }: Permits for the execution of Groovy script code.
Publish-build Actions
publish { … }: This block incorporates actions to be executed after the pipeline completes, no matter success or failure.
all the time { … }: This block executes its contents whatever the pipeline standing. On this case, it echoes a message indicating cleanup.
Begin constructing by clicking on Construct Now :
![](https://cdn.analyticsvidhya.com/wp-content/uploads/2024/04/10-2.png)
Now we will put a set off in order that it mechanically checks for any github updates and combine it within the pipeline, we’re setting the set off for two minutes.
Triggers
Go to configure within the Jenkins UI and construct Triggers.
Choose ballot SCM
Now add H/2**** and that is will set off and test for the updates. You’ll be able to test the web site for syntax of timing set off https://crontab.guru/
![](https://cdn.analyticsvidhya.com/wp-content/uploads/2024/04/09-1.png)
To test this, we added a brand new wsgi.py and it will mechanically set off jenkins to determine the adjustments.
![CI/CD with Jenkins](https://cdn.analyticsvidhya.com/wp-content/uploads/2024/04/10-3.png)
Right here the repo is my created repo, You are able to do the identical with your personal created repo, keep in mind it should have a jenkins file to determine. you’ll be able to add or edit the present file in your repo after which if it’s important to set the set off it’s going to determine and auto replace it. in any other case you may need to click on on construct now everytime you need jenkins to determine the adjustments.
For steady deployment, comply with the subsequent article on Dockerisation together with CICD pipeline.
Conclusion
Deploying Flask functions utilizing CI/CD with Jenkins is a crucial step in enhancing the effectiveness, dependability, and adaptability of software program growth processes. These processes assist groups to rapidly produce high-quality software program in response to buyer enter and market calls for by automating the combination, testing, and deployment of code adjustments. On this complete information now we have examined the worth of steady integration and steady growth, its benefits and difficulties. We’ve additionally coated the sensible facet of configuring Jenkins CI/CD pipelines, protecting all the things from setting up pipelines and comprehending Jenkins jobs to managing the total deployment course of.
[ad_2]
Source link