0% found this document useful (0 votes)
39 views18 pages

Jenkins

Uploaded by

rrrfafafa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views18 pages

Jenkins

Uploaded by

rrrfafafa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

DevOps Shack

Jenkins Comprehensive Guide: .


. From Basics to Advanced
Click Here To Enrol To Batch-5 | DevOps & Cloud DevOps

Introduction
Jenkins is an open-source automation server that facilitates continuous integration
and continuous delivery (CI/CD) of software projects. This guide will cover Jenkins
from the basics to advanced topics with detailed examples and sample commands.

Table of Contents

1. Introduction to Jenkins
o What is Jenkins?
o Features and Benefits
o Installing Jenkins
o Configuring Jenkins
2. Getting Started with Jenkins
o Jenkins Dashboard Overview
o Creating Your First Job
o Jenkins Freestyle Projects
3. Advanced Jenkins Configuration
o Using Jenkins Pipelines
o Jenkinsfile Syntax
o Integrating with SCM (Git, GitHub)
4. Jenkins Plugins
o Managing Plugins
o Essential Plugins for CI/CD
o Installing and Configuring Plugins
5. Jenkins and DevOps
o Continuous Integration with Jenkins
o Continuous Deployment with Jenkins
o Automated Testing
6. Jenkins Security
o Security Best Practices
o User Management
o Securing Jenkins
7. Scaling Jenkins
o Master-Slave Architecture
o Setting Up Build Agents
o Distributed Builds
8. Jenkins for Real-World Projects
o Case Study: CI/CD Pipeline for a Web Application
o Case Study: Deployment Automation
9. Troubleshooting Jenkins
o Common Issues and Solutions
o Jenkins Logs and Debugging

1. Introduction to Jenkins

What is Jenkins?

Jenkins is an open-source automation server written in Java. It helps automate parts


of software development related to building, testing, and deploying, facilitating
continuous integration and continuous delivery.

Features and Benefits

• Easy Installation: Java-based and available as a WAR file, native packages, and
Docker image.
• Extensible: Supports a vast array of plugins.
• Distributed Builds: Scalable with master-slave architecture.
• Pipeline Support: Declarative and scripted pipeline support.
• Community Support: Large community providing support and plugins.

Installing Jenkins

1. Pre-requisites:
o Java 11 or newer
o Stable internet connection

2. Installation on Ubuntu:
3. sudo apt update
4. sudo apt install openjdk-11-jdk
5. wget -q -O - https://pkg.jenkins.io/debian/jenkins.io.key | sudo apt-
key add -
6. sudo sh -c 'echo deb http://pkg.jenkins.io/debian-stable binary/ >
/etc/apt/sources.list.d/jenkins.list'
7. sudo apt update
8. sudo apt install jenkins
9. sudo systemctl start jenkins
sudo systemctl status jenkins

10. Accessing Jenkins:

o Open your browser and navigate to http://localhost:8080


o Follow the on-screen instructions to complete the setup.

Configuring Jenkins

• Unlock Jenkins: Use the initial admin password located


at /var/lib/jenkins/secrets/initialAdminPassword.
• Customize Jenkins: Install suggested plugins during the setup wizard.
• Create Admin User: Set up your admin user for Jenkins.

2. Getting Started with Jenkins

Jenkins Dashboard Overview

The Jenkins dashboard provides a comprehensive overview of the jobs, build history,
and system configuration. Key components include:

• New Item: Create new jobs.


• People: View user profiles.
• Build History: View build results.
• Manage Jenkins: Configure system settings.

Creating Your First Job

1. Create a New Job:

o Click on New Item.


o Enter an item name, select Freestyle project, and click OK.
2. Configure the Job:

o Source Code Management: Link your Git repository.


o Build Triggers: Configure build triggers like Poll SCM or Build periodically.
o Build: Add build steps such as Execute shell.

3. Save and Build:

o Save the job configuration.


o Trigger a build manually or wait for the configured trigger.

3. Advanced Jenkins Configuration

Using Jenkins Pipelines

Pipelines as code allow you to define the entire build process, which typically
includes stages for building, testing, and deploying an application.

1. Creating a Pipeline Job:

o Click on New Item.


o Enter an item name, select Pipeline, and click OK.

2. Pipeline Script:

o In the Pipeline section, define your pipeline using either Declarative or


Scripted syntax.

Declarative Pipeline Example:


pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building...'
}
}
stage('Test') {
steps {
echo 'Testing...'
}
}
stage('Deploy') {
steps {
echo 'Deploying...'
}
}
}
}
Integrating with SCM (Git, GitHub)

1. SCM Configuration:

o In your job configuration, go to the Source Code Management section.


o Select Git and provide your repository URL.

2. Credentials Management:

o Add necessary credentials (SSH keys, user/password) for accessing the


repository.

4. Jenkins Plugins

Managing Plugins

• Installing Plugins:

o Go to Manage Jenkins > Manage Plugins.


o Search for the desired plugin and click Install.

• Essential Plugins:

o Git Plugin
o Pipeline Plugin
o Blue Ocean
o Credentials Binding Plugin

5. Jenkins and DevOps

Continuous Integration with Jenkins

• Automating Builds:
o Set up jobs to trigger builds on code commits.
o Configure build steps to compile code, run tests, and generate reports.

Continuous Deployment with Jenkins

• Deploying Applications:
o Use plugins like Deploy to Container or SSH to automate the deployment
process.
o Integrate with tools like Ansible or Kubernetes for orchestration.

6. Jenkins Security
Security Best Practices

• Enable Security:

o Go to Manage Jenkins > Configure Global Security.


o Enable security, configure authentication (e.g., LDAP, Active Directory).

• Role-Based Access Control:

o Use Role-based Authorization Strategy plugin to manage user permissions.

7. Scaling Jenkins

Master-Slave Architecture

• Setting Up Build Agents:


o Go to Manage Jenkins > Manage Nodes and Clouds.
o Add a new node and configure its launch method (e.g., SSH, JNLP).

8. Jenkins for Real-World Projects

Case Study: CI/CD Pipeline for a Web Application

• Pipeline Stages:
o Build: Compile code.
o Test: Run unit tests.
o Deploy: Deploy to staging/production environments.

9. Troubleshooting Jenkins

Common Issues and Solutions

• Build Failures:

o Check build logs for errors.


o Ensure all dependencies are correctly configured.

• Performance Issues:

o Monitor system resources.


o Optimize job configurations and build steps.

Conclusion
Jenkins is a powerful tool for automating the software development lifecycle. By
following this comprehensive guide, you can effectively utilize Jenkins for your CI/CD
needs, from basic configurations to advanced deployments.

1. Introduction to Jenkins (Detailed)

What is Jenkins?

Jenkins is an open-source automation server used to automate tasks related to


building, testing, and deploying software. It is particularly popular for implementing
continuous integration (CI) and continuous delivery (CD) workflows.

Key Concepts:

• Continuous Integration (CI): Integrating code changes frequently, usually multiple


times a day.
• Continuous Delivery (CD): Ensuring that the codebase is always in a deployable
state.

Features and Benefits

• Extensible: Jenkins supports a wide array of plugins that extend its capabilities.
• Distributed Builds: Supports running jobs across multiple nodes for better resource
utilization.
• Pipeline as Code: Define your build process through code using Jenkins Pipelines.
• Rich Ecosystem: Large community and extensive documentation.

Installing Jenkins

Pre-requisites:

• Java 11 or newer
• A stable internet connection

Installation on Ubuntu:

1. Update the system packages:


sudo apt update

2. Install Java:
sudo apt install openjdk-11-jdk
3. Add Jenkins Debian repository and key:
4. 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'

5. Install Jenkins:
6. sudo apt update
sudo apt install jenkins

7. Start Jenkins:
sudo systemctl start jenkins

8. Check Jenkins status:


sudo systemctl status jenkins
Accessing Jenkins:

• Open a web browser and go to http://localhost:8080


• Jenkins setup wizard will start.

Unlock Jenkins:

• Use the initial admin password located


at /var/lib/jenkins/secrets/initialAdminPassword.

Customize Jenkins:

• Install suggested plugins during the setup wizard

• Create your first admin user.

2. Getting Started with Jenkins (Detailed)

Jenkins Dashboard Overview

The Jenkins dashboard is the control center for Jenkins, providing access to all its
features and configurations.

Key Components:

• New Item: Create new jobs or projects.


• People: View and manage user profiles.
• Build History: See the build history for all jobs.
• Manage Jenkins: Access to Jenkins system settings and configurations.

Creating Your First Job

1. Create a New Job:

o Click on New Item.


o Enter an item name, select Freestyle project, and click OK.

2. Configure the Job:

o General: Provide a description and configure the job's settings.


o Source Code Management: Link to your Git repository.
o Build Triggers: Configure triggers like Poll SCM or Build periodically.
o Build: Add build steps such as Execute shell.

echo "Building the project"

3. Save and Build:

o Save the job configuration.


o Trigger a build manually or wait for the configured trigger.

3. Advanced Jenkins Configuration (Detailed)

Using Jenkins Pipelines

Jenkins Pipelines allow you to define your entire build process through code,
providing a more robust and maintainable way to handle CI/CD workflows.

Creating a Pipeline Job:

1. Create a Pipeline Job:

o Click on New Item.


o Enter an item name, select Pipeline, and click OK.

2. Pipeline Script:

o In the Pipeline section, define your pipeline using either Declarative or


Scripted syntax.
Declarative Pipeline Example:
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building...'
}
}
stage('Test') {
steps {
echo 'Testing...'
}
}
stage('Deploy') {
steps {
echo 'Deploying...'
}
}
}
}
Scripted Pipeline Example:
node {
stage('Build') {
echo 'Building...'
}
stage('Test') {
echo 'Testing...'
}
stage('Deploy') {
echo 'Deploying...'
}
}

Integrating with SCM (Git, GitHub)

Integrating Jenkins with Source Control Management (SCM) like Git or GitHub allows
you to automate the build process triggered by code changes.

1. SCM Configuration:

o In your job configuration, go to the Source Code Management section.


o Select Git and provide your repository URL.
o Example:

https://github.com/username/repository.git

2. Credentials Management:

o Add necessary credentials (SSH keys, user/password) for accessing the


repository.
o In Credentials dropdown, add new credentials and select the appropriate
type.
3. Build Triggers:

o Set up build triggers like Poll SCM to periodically check for changes.

H/5 * * * *

4. Jenkins Plugins (Detailed)

Managing Plugins

Plugins extend Jenkins capabilities and integrate with other tools in your CI/CD
pipeline.

Installing Plugins:

• Go to Manage Jenkins > Manage Plugins.


• In the Available tab, search for the desired plugin.
• Click Install without restart or Download now and install after restart.

Essential Plugins:

1. Git Plugin: Integrates Git with Jenkins.


2. Pipeline Plugin: Enables Pipeline support.
3. Blue Ocean: Provides a modern user interface for Jenkins.
4. Credentials Binding Plugin: Manages credentials securely.
5. JUnit Plugin: Processes JUnit test results.

Example: Installing Git Plugin

• Search for Git Plugin in Manage Plugins.


• Check the box next to Git Plugin.
• Click Install without restart.

Configuring Plugins

• After installation, configure the plugins under Manage Jenkins > Configure
System or within specific jobs.

5. Jenkins and DevOps (Detailed)


Continuous Integration with Jenkins

CI involves automating the process of integrating code changes from multiple


contributors into a shared repository several times a day.

Automating Builds:

1. Source Code Management:


o Link your repository in the job configuration.
2. Build Triggers:
o Configure triggers to automate builds.
o Example: Poll SCM with a cron schedule.
3. Build Steps:
o Add steps to compile code, run tests, and generate reports.
o Example shell script for a Java project:

mvn clean install

Continuous Deployment with Jenkins

CD automates the deployment process, ensuring code changes are automatically


deployed to production environments.

Deploying Applications:

1. Deploy to Container Plugin:

o Install and configure to deploy WAR/EAR files to servlet containers like


Tomcat.
o Example:

Deploy to Container

2. SSH Plugin:

o Use for executing commands on remote servers via SSH.


o Example:
o pipeline {
o agent any
o stages {
o stage('Deploy') {
o steps {
o sshPublisher(publishers:
[sshPublisherDesc(configName: 'MyServer', transfers:
[sshTransfer(sourceFiles: 'target/*.war', removePrefix:
'target', remoteDirectory: '/opt/tomcat/webapps', execCommand:
'sudo systemctl restart tomcat')])])
o }
o }
o }
}

3. Integration with Orchestration Tools:

o Use tools like Ansible or Kubernetes for deployment orchestration.

6. Jenkins Security (Detailed)

Security Best Practices

Security is crucial in Jenkins to protect your CI/CD pipelines and sensitive data.

Enable Security:

1. Global Security Configuration:

o Go to Manage Jenkins > Configure Global Security.


o Enable Jenkins' own user database and Matrix-based security.

2. User Management:

o Add users and assign roles with appropriate permissions.

Role-Based Access Control:

• Use Role-based Authorization Strategy plugin to manage user permissions.


• Define roles and assign them to users/groups.

Securing Jenkins:

1. Secure Communication:
o Enable HTTPS for Jenkins.
o Generate SSL certificates and configure Jenkins to use them.
2. Access Control:
o Limit access to Jenkins based on IP addresses.
o Use firewalls and network segmentation.

Example: Enabling HTTPS

1. Generate SSL Certificates:


sudo openssl req -newkey rsa:2048 -nodes -keyout jenkins.key -x509 -
days 365 -out jenkins.crt

2. Configure Jenkins to Use HTTPS:


o Edit Jenkins configuration file
(/etc/default/jenkins or /etc/sysconfig/jenkins).
o Add:

--httpsPort=8443 --httpsCertificate=/path/to/jenkins.crt --
httpsPrivateKey=/path/to/jenkins.key

7. Scaling Jenkins (Detailed)

Master-Slave Architecture

Jenkins can distribute build jobs across multiple nodes (slaves) to manage load and
improve performance.

Setting Up Build Agents:

1. Add a New Node:

o Go to Manage Jenkins > Manage Nodes and Clouds.


o Click New Node and provide a name.
o Select Permanent Agent and configure the node settings.

2. Launch Method:

o Choose a launch method, such as Launch agents via SSH or Launch


agent via Java Web Start.
o Provide the necessary credentials and remote root directory.

3. Agent Configuration:

o Configure agent settings like number of executors, remote work directory,


and labels.
o Labels help assign specific jobs to particular agents.

4. Verify Connection:

o Save the configuration and ensure the agent is connected.

Example: Setting Up SSH Agent

1. Install SSH Server on Agent:


sudo apt install openssh-server

2. Generate SSH Key on Jenkins Master:


ssh-keygen -t rsa -b 2048

3. Copy SSH Key to Agent:


ssh-copy-id user@agent-ip

4. Configure SSH Agent in Jenkins:

o Go to Manage Jenkins > Manage Nodes and Clouds.


o Add a new node and configure the SSH credentials.

Distributed Builds:

• Distribute builds across multiple agents to manage load.


• Use agent labels to direct specific jobs to certain agents.

8. Jenkins for Real-World Projects (Detailed)

Case Study: CI/CD Pipeline for a Web Application

Creating a CI/CD pipeline involves defining stages for building, testing, and
deploying the application.

Pipeline Stages:

1. Build:

o Compile the code.


o Example:
o stage('Build') {
o steps {
o script {
o sh 'mvn clean install'
o }
o }
}

2. Test:

o Run unit tests.


o Example:
o stage('Test') {
o steps {
o script {
o sh 'mvn test'
o }
o }
}
3. Deploy:
Deploy the application to staging/production environments.

• Example:
• stage('Deploy') {
• steps {
• script {
• sshPublisher(publishers: [sshPublisherDesc(configName:
'ProdServer', transfers: [sshTransfer(sourceFiles: 'target/*.war',
removePrefix: 'target', remoteDirectory: '/opt/tomcat/webapps',
execCommand: 'sudo systemctl restart tomcat')])])
• }
• }
}

Case Study: Deployment Automation

Automating the deployment process ensures consistency and reduces manual errors.

Deployment Steps:

1. Build Artifact:

o Compile the code and generate an artifact (e.g., WAR file).


o Example:
o stage('Build Artifact') {
o steps {
o sh 'mvn clean package'
o }
}

2. Upload Artifact:

o Upload the artifact to a remote server or artifact repository.


o Example:
o stage('Upload Artifact') {
o steps {
o sshPublisher(publishers: [sshPublisherDesc(configName:
'Artifactory', transfers: [sshTransfer(sourceFiles:
'target/*.war', removePrefix: 'target', remoteDirectory:
'/opt/artifacts')])])
o }
}

3. Deploy Artifact:

o Deploy the artifact to the target environment.


o Example:
o stage('Deploy Artifact') {
o steps {
o sshPublisher(publishers: [sshPublisherDesc(configName:
'ProdServer', transfers: [sshTransfer(sourceFiles:
'/opt/artifacts/*.war', remoteDirectory: '/opt/tomcat/webapps',
execCommand: 'sudo systemctl restart tomcat')])])
o }
}

9. Troubleshooting Jenkins (Detailed)

Common Issues and Solutions

Build Failures:

1. Check Build Logs:

o Analyze logs for errors.


o Example:
o stage('Build') {
o steps {
o script {
o sh 'mvn clean install'
o }
o }
}

2. Dependency Issues:

o Ensure all dependencies are correctly configured in your build tool (e.g.,
Maven, Gradle).

Performance Issues:

1. Monitor System Resources:

o Use monitoring tools to check CPU, memory, and disk usage.


o Example tools: Prometheus, Grafana.

2. Optimize Job Configurations:

o Limit the number of concurrent builds.


o Use agent labels to distribute load.

Jenkins Logs and Debugging:

1. Accessing Jenkins Logs:

o Jenkins logs are located in /var/log/jenkins/jenkins.log (Linux)


or C:\Program Files (x86)\Jenkins\jenkins.out.log (Windows).
2. Increasing Log Level:

o Go to Manage Jenkins > System Log > Add new log recorder.
o Configure the log level for specific components.

3. Debugging:

o Use Manage Jenkins > Script Console to run Groovy scripts for
debugging.
o Example script to list all jobs:

Jenkins.instance.getAllItems(Job.class).each { println it.name


}

Conclusion
Jenkins is a powerful tool for automating the software development lifecycle. By
following this comprehensive guide, you can effectively utilize Jenkins for your CI/CD
needs, from basic configurations to advanced deployments.

You might also like