10.2 C
New York
Thursday, April 11, 2024

Implementing CI/CD with Jenkins for Flask Software Deployment


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. With the intention to fulfill these necessities, steady integration (CI) and steady deployment (CD), have change into crucial strategies. Whereas CD automates the deployment course of, CI automates the combination and testing of code modifications. This detailed handbook 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

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 may observe the earlier articles right here to find out about 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, crew member contributions, and code transparency.

"

Want of CI/CD

CI/CD is essential in fashionable 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.
  • Lowered Human Effort and Errors: CI/CD automates software program supply phases, decreasing human involvement, error danger, and growth time, permitting builders to concentrate on different initiatives and buyer wants.
  • Enhanced Collaboration and Communication: CI/CD instruments facilitate collaboration amongst growth, testing, and operations groups by offering visibility into the complete software program supply pipeline, enhancing communication, coordination, and alignment of efforts.
  • Scalability and Flexibility: CI/CD practices are adaptable to varied mission sizes and methodologies, providing flexibility by means of instruments that help programming languages, platforms, and cloud environments.
  • Steady Suggestions Loop: CI/CD facilitates a steady suggestions loop by amassing metrics and efficiency knowledge all through the software program supply course of, enabling groups to determine bottlenecks, optimize workflows, and make data-driven selections.

Total, CI/CD is important for contemporary software program growth practices because it allows organizations to ship high-quality software program sooner, extra reliably, and with larger effectivity, finally driving enterprise success and buyer satisfaction.

Challenges with out CI/CD

With out CI/CD, software program growth groups and course of could encounter a number of challenges, together with:

  • Guide Processes vulnerable to error: The time-consuming and human error-prone nature of handbook integration, testing, and deployment procedures causes delays and inconsistencies.
  • Ineffective launch cycles: Within the absence of automation, launch cycles are laborious and sluggish, making it tougher to offer 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 might 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 could 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 numerous forms of jobs, together with freestyle, pipeline, and multi-configuration initiatives, every tailor-made to totally different use circumstances within the CI/CD course of.

  • Jobs: Jenkins can deal with a variety of initiatives, together with Multibranch Pipeline, Freestyle, and Maven initiatives. Each form of job has distinctive capabilities and configuration choices.
  • Configuration: A job can have numerous configurations, together with parameters, construct triggers, supply code repositories, construct levels, and post-build actions.
  • Execution: Jenkins assigns assets and executes job configuration directions, monitoring progress and output by means of the net interface.
  • Integration: Jenkins jobs might be built-in with numerous construct instruments, testing frameworks, model management techniques, and deployment platforms like Docker and Kubernetes for environment friendly supply processes.

Jenkins makes use of initiatives (often known as “jobs”) to carry out its work. Tasks are outlined and run by Jenkins customers. Jenkins presents a number of forms of initiatives, together with:

  • Pipeline
  • Multibranch Pipeline
  • Group folders
  • Freestyle
  • Multi-configuration (matrix)
  • Maven
  • Exterior job

We will likely be working with the multi department pipeline for GitHub.

Implementing CI/CD with Jenkins for Flask Software Deployment

Jenkins is an open-source automation server that facilitates CI/CD processes. It supplies 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 techniques like Git, enabling builders to set off builds and deployments routinely based mostly on code adjustments.

Steps to Implement CI/CD with Jenkins 

  • Arrange Jenkins: Set up Jenkins on a server and configure it in line with your necessities. Jenkins might 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 enables Jenkins to tug code adjustments and set off builds routinely.
  • Configure Jenkins Pipeline: Create a Jenkins pipeline to outline the levels of the CI/CD course of. A typical pipeline consists of levels similar to constructing, testing, and deployment.
  • Implement Testing: Outline automated assessments to make sure the standard and performance of the Flask software. These assessments can embody unit assessments, integration assessments, and end-to-end assessments.
  • Construct and Take a look at Stage: Arrange Jenkins to routinely construct the Flask software each time there’s a brand new code commit. After constructing, execute the automated assessments to confirm the applying’s performance.
  • Deployment Stage: As soon as the assessments cross efficiently, deploy the Flask software utilizing Gunicorn and Nginx. Jenkins can execute deployment scripts or instructions to add the applying to the cloud server.
  • Monitor and Iterate: Monitor the CI/CD pipeline for any failures or points. Repeatedly 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 Person 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 

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 bundle. 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 key’s 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 option 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.listing.d/jenkins.listing > /dev/null” : This command provides the Jenkins repository to the system’s bundle sources listing. It specifies the situation of Jenkins packages and the important thing used for bundle 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.listing.d/jenkins.listing > /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

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

Copy this token and paste it on the block given on the URL for Jenkins.

Then after login ans organising the choice as “Set up advised plugins”. 

Ouput:

CI/CD with Jenkins

Allow us to start by constructing our first pipeline for the mission which we used earlier to deploy on cloud and now to place underneath steady watch/integration. 

Step2: Constructing Pipeline

  • Click on on new merchandise 
  • Enter a reputation and choose Pipeline – MultiBranch right here 
Building Pipeline
  • 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

You may 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 routinely will get up to date within the pipeline. 

Additionally, for the implementation, there have to be a Jenkins file in your repo to ensure that Jenkins to acknowledge it and browse the pipeline saved in it. 

Create a file identify as “jenkinsfile” in the identical listing. You may create whereas doing it in your native after which put the entire recordsdata and folders in a listing and on Github. 

CI/CD with Jenkins

The Jenkins file we used: 

pipeline {
    agent any

    atmosphere {
        VENV_PATH = 'myprojectenv'
        FLASK_APP = 'myproject.py'
    }

    levels {
        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 {
                    // Test for the digital atmosphere, 
                    // create it if it would 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 assessments right here. That is only a placeholder.
                // For instance, when you had assessments, you would possibly run: pytest
                echo "Assuming assessments are run right here. Please exchange 
                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 relies 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 particular CLI device for that platform
                    echo 'Deploying software...'
                    // Instance: sh 'scp -r . person@your_server:/path/to/deploy'
                }
            }
        }
    }

    publish {
        at all times {
            // Clear up after the pipeline runs
            echo 'Cleansing up...'
            sh 'rm -rf ${VIRTUAL_ENV_DIR}'
        }
    }
}

Easy methods to Write Jenkins File ? 

This Jenkins pipeline script defines a steady integration and deployment (CI/CD) course of for a Flask internet software hosted on GitHub. Lets perceive necessary elements and ideas:

Pipeline Declaration

  • pipeline { … }: This defines the complete Jenkins pipeline.

Agent

  • agent any: This tells Jenkins to execute the pipeline on any obtainable 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 identify of the Flask software file.

Levels

  • levels { … }: This block comprises a number of levels 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 working assessments. Right here, it solely echoes a message.
    • Deploy: Placeholder for deploying the Flask software. Presently, it solely echoes a message. we are going to full this within the upcoming article with docker and CICD

Steps

  • steps { … }: This block comprises particular person steps inside every stage.
    • sh ‘…’: Executes shell instructions. Used for numerous duties similar to making a digital atmosphere, activating it, putting in dependencies, and so forth.
    • script { … }: Permits for the execution of Groovy script code.

Put up-build Actions

  • publish { … }: This block comprises actions to be executed after the pipeline completes, no matter success or failure.
  • at all times { … }: 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 : 

Now we are able to put a set off in order that it routinely 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 verify for the updates. You may verify the web site for syntax of timing set off https://crontab.guru/

To verify this, we added a brand new wsgi.py and it will routinely set off jenkins to determine the adjustments.

CI/CD with Jenkins

Right here the repo is my created repo, You are able to do the identical with your individual created repo, keep in mind it should have a jenkins file to determine. you possibly can add or edit the prevailing 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 might need to click on on construct now everytime you need jenkins to determine the adjustments. 

For steady deployment, observe the following article on Dockerisation together with CICD pipeline.

Conclusion

Deploying Flask purposes 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 we have now examined the worth of steady integration and steady growth, its benefits and difficulties. We’ve additionally lined the sensible aspect of configuring Jenkins CI/CD pipelines, overlaying all the pieces from establishing pipelines and comprehending Jenkins jobs to managing the complete deployment course of. 



Supply hyperlink

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles