Day 29: Jenkins

Day 29: Jenkins

Important highlights about CI/ CD pipeline


Introduction to Jenkins:

  • Understanding Jenkins: Jenkins is an open-source automation tool used for continuous integration and continuous delivery (CI/CD) of software projects.

  • Benefits: Jenkins automates building, testing, and deployment processes, enhancing efficiency, reliability, and collaboration among development teams.

Installation and Setup:

  • System Requirements: Jenkins can be installed on various platforms, requiring Java Runtime Environment (JRE) and adequate system resources.

  • Installation Methods: Jenkins can be set up via standalone installation, Docker, or cloud-based services.

  • Initial Configuration: Post-installation, initial setup involves accessing the Jenkins web UI, setting an admin password, and installing suggested plugins.

Creating Jenkins Jobs:

  • Jenkins Jobs: Jobs are units of work, often representing build, test, or deployment tasks.

  • Job Creation: Jobs can be created in Jenkins UI by defining build steps, specifying source code repositories, and configuring triggers.

Building and Automation:

  • Source Control Integration: Jenkins integrates with version control systems like Git, enabling automatic builds triggered by code changes.

  • Build Automation: Jenkins automates tasks like compiling code, running tests, and generating artifacts, resulting in consistent and repeatable processes.

Jenkins Pipeline:

  • Pipeline Concepts: A Jenkins Pipeline is a script-based approach to defining and automating a CI/CD process.

  • Declarative and Scripted: Declarative pipelines offer a simpler, structured syntax, while scripted pipelines provide greater flexibility through Groovy scripting.

CI/CD Concepts:

  • Continuous Integration (CI): Developers frequently integrate code changes into a shared repository, followed by automated testing to catch issues early.

  • Continuous Delivery (CD): Automated deployment ensures that code is ready for release to production at any time.

Version Control Integration:

  • Version Control Systems: Jenkins integrates seamlessly with popular version control platforms like Git, SVN, and more.

  • Automated Builds: Jenkins monitors version control repositories for changes and automatically triggers build processes, maintaining code quality and consistency.

Plugins and Extensibility:

  • Plugin Usage: Jenkins offers a wide range of plugins to extend functionality, integrating with tools for notifications, testing, deployment, and more.

  • Plugin Management: Plugins can be installed, updated, and managed through the Jenkins Plugin Manager.

Security and Authentication:

  • User Access: Jenkins allows configuration of user accounts, permissions, and access control.

  • Security Measures: Secure Jenkins by enforcing authentication, authorization, and role-based access to ensure proper data protection.

Troubleshooting Basics:

  • Issue Identification: Jenkins provides logs and console outputs to help identify build failures and configuration problems.

  • Debugging Process: Debugging involves reviewing logs, adjusting configuration settings, and verifying external tool integrations.

These succinct explanations cover the key aspects of each topic, providing a concise overview of Jenkins and its essential functionalities.


Install Jenkins on Linux:

  1. Install Java: Jenkins requires Java to run. You can install OpenJDK using the following command:

     sudo apt install openjdk-11-jdk
    
  2. Add Jenkins Repository and Install: Add the Jenkins repository and install Jenkins using the following commands:

     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
    
  3. Start Jenkins: After installation, start the Jenkins service:

     sudo systemctl start jenkins
    
  4. Enable Jenkins to Start on Boot: To ensure Jenkins starts automatically on system boot:

     sudo systemctl enable jenkins
    
  5. Access Jenkins Web Interface: Jenkins runs on port 8080 by default. Open your web browser and navigate to http://your_server_ip:8080 to access the Jenkins setup wizard. Follow the instructions to complete the initial configuration.

Create a Jenkins job:

  1. Creating a Jenkins Job:

    In this example, we'll create a Freestyle project job named "MySampleJob" in Jenkins.

     # Create a new directory for Jenkins job configurations
     mkdir -p ~/jenkins-jobs/MySampleJob
    
     # Navigate to the job configuration directory
     cd ~/jenkins-jobs/MySampleJob
    
     # Create a basic Jenkins job configuration file (config.xml)
     echo '<?xml version="1.0" encoding="UTF-8"?>
     <project>
       <actions/>
       <description>My sample Jenkins job</description>
       <keepDependencies>false</keepDependencies>
       <properties/>
       <scm class="hudson.scm.NullSCM"/>
       <canRoam>true</canRoam>
       <disabled>false</disabled>
       <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
       <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
       <triggers class="vector"/>
       <concurrentBuild>false</concurrentBuild>
       <builders>
         <hudson.tasks.Shell>
           <command>echo "Hello, Jenkins!"</command>
         </hudson.tasks.Shell>
       </builders>
       <publishers/>
       <buildWrappers/>
     </project>' > config.xml
    
     # Create the job in Jenkins using the configuration file
     java -jar /usr/share/jenkins/jenkins.war import-job MySampleJob < config.xml
    
  2. Automating Builds and Running Jobs:

    Jenkins jobs can be automated by triggering them through various methods, including webhooks and scheduled builds. Here, we'll manually trigger the "MySampleJob" job we created.

    • To run the job using the Jenkins CLI:

        java -jar /usr/share/jenkins/jenkins.war build MySampleJob
      
    • To run the job using the Jenkins REST API (requires the "curl" command):

        curl -X POST http://localhost:8080/job/MySampleJob/build
      
  3. Integrating with Version Control (Git):

In this example, we'll integrate Jenkins with a Git repository.

# Install Git on your system if not already installed
sudo apt install git

# Create a sample Git repository
mkdir ~/sample-git-repo
cd ~/sample-git-repo
git init
echo "print('Hello, Git!')" > script.py
git add script.py
git commit -m "Initial commit"

# Configure the Jenkins job to use Git as the source code repository
# In the Jenkins job configuration, choose "Git" under "Source Code Management"
# Provide the repository URL (e.g., file:///home/user/sample-git-repo)

Conclusion :

In conclusion, Jenkins is a powerful open-source automation server that revolutionizes software development practices.

Hope you like my post. Don't forget to like, comment, and share.