JENKINS SHARED LIBRARIES TUTORIAL | WHAT IS SHARED LIBRARY IN JENKINS| DEMO INCLUDED | #devops #cicd
Introduction to Jenkins Shared Libraries
In this section, Abhishek introduces the concept of shared libraries and explains why they are important in Jenkins.
What are Shared Libraries?
- Shared libraries are a way to share code across multiple pipelines in Jenkins.
- They allow you to define common functions and classes that can be reused across different pipelines.
- This helps reduce duplication of code and makes it easier to maintain your pipelines.
Advantages of Shared Libraries
- Using shared libraries can help simplify your pipeline code and make it more modular.
- It allows you to centralize your pipeline logic, making it easier to manage and update.
- You can also enforce best practices across all your pipelines by defining them in the shared library.
Scenario-Based Interview Questions on Shared Libraries
- Abhishek provides some scenario-based interview questions related to shared libraries.
Live Demo: Configuring, Writing, and Using a Shared Library
- Abhishek demonstrates how to configure, write, and use a shared library in Jenkins through a live demo.
- He explains step-by-step how to create a shared library for an existing pipeline.
Simplifying Jenkins Pipelines with Shared Libraries
In this section, the speaker explains how shared libraries can simplify Jenkins pipelines and reduce the amount of repetitive code that needs to be written.
What are Shared Libraries?
- Shared libraries are a common, reusable code that can be used across multiple pipelines.
- They can include repetitive code such as Maven targets or build and test stages.
- The shared library is stored in a Git repository and referenced in the pipeline stage using a simple syntax.
How do Shared Libraries Work?
- When the pipeline references the shared library, Jenkins controller searches for the file in the Git repository and executes it.
- The shared library contains the same piece of code that would otherwise need to be written in each individual pipeline.
- To import the library, add a line at the beginning of your pipeline with syntax "Library" followed by name of your library.
Advantages of Using Shared Libraries
- Standardizes pipelines across an organization.
- Reduces amount of code needed to write new pipelines.
- Allows for easy modification and maintenance of pipelines.
Advantages of Shared Libraries
In this section, the speaker explains the advantages of using shared libraries in Jenkins pipelines.
Benefits of Shared Libraries
- Standardization of pipeline reduces duplication of code.
- Easy onboarding for new team members and projects due to standardized code.
- Less code maintenance in future, as changes can be made in one place and reflected across all pipelines.
- Reduced risk of errors when moving towards low code development.
Definition of Shared Libraries
- Shared libraries are reusable code that can be used across multiple pipelines.
- They function similarly to functions in Java or Python applications, where repetitive code is put into a function and referenced wherever needed.
Writing a Shared Library
In this section, the speaker explains how to write a shared library for use in Jenkins pipelines.
Creating a Folder for Shared Libraries
- Create a folder called "vars" within your GitHub repository or source code folder.
- This is where you will add all your shared libraries.
Syntax for Writing Functions in Groovy
- The first line you write should be "def" followed by the name of the function. This is similar to writing functions in groovy.
Example: Hello World Groovy File
- An example file called "hello world.groovy" is provided as an example shared library file.
Learning Jenkins from Scratch
In this section, the speaker introduces three videos on their channel that cover everything about Jenkins. They also introduce two projects on Jenkins and explain how to build an entire ecosystem using Jenkins.
Introduction to Jenkins Pipeline
- The speaker shows a plain Jenkins instance and demonstrates how to write a simple pipeline.
- They replace the code with shared libraries and explain how to create a shared library in the vars folder of your GitHub repository.
- The speaker explains how to configure global pipeline libraries in Manage Jenkins > Configure System.
Creating a Shared Library
- The speaker creates a file called "hello world.gruby" in the vars folder of their GitHub repository.
- They use camel casing for naming conventions and write the function def call inside it.
- The speaker imports the shared library into their test pipeline and configures global pipeline libraries in Manage Jenkins > Configure System.
Building Your Own CI/CD Pipeline
- The speaker explains how to build your own CI/CD pipeline using Jenkins.
- They demonstrate how to install plugins, create stages, and run multi-stage engines pipelines.
- The speaker introduces two projects on building an ultimate CI/CD pipeline using Jenkins.
Shared Library Configuration
In this section, the speaker explains how to configure a shared library in Jenkins and import it into a pipeline.
Configuring Shared Library
- To configure a shared library, you need to provide the name of the folder and click on the save button.
- Once configured, you can import the library by using
@Libraryfollowed by the name of the library inside double quotes.
- Use
space underscoreto import everything that is present in this shared Library.
Importing Shared Library
- To import a specific file from the shared library, provide only its name without specifying the function inside it.
- The syntax for importing everything from a particular module in groovy scripting is hyphen underscore.
Using Shared Library
- After configuring and importing, you can execute your pipeline using code picked up from GitHub.
- This configuration needs to be done only once for any number of pipelines.
Standardizing Pipelines with Shared Libraries
In this section, the speaker explains how to standardize pipelines using shared libraries and provides an example of doing so with Maven targets.
Standardizing Pipelines with Maven Targets
- You can standardize pipelines by creating different stages as shared libraries using different file names in your GitHub repository.
- This helps in avoiding repetitive code and makes it easier to modify or remove code across multiple pipelines.
Modifying Code Across Multiple Pipelines
In this section, the speaker explains how modifying code across multiple pipelines becomes easier when using shared libraries.
Modifying Code Across Multiple Pipelines
- When you want to modify something across multiple pipelines, go to your GitHub repository and make changes there.
- Once you commit these changes, all 200 Jenkins pipelines will reflect these changes.
Interview Questions for Jenkins Shared Libraries
In this section, the speaker provides information about interview questions related to Jenkins shared libraries. The questions are available in a GitHub repository under the shared libraries folder.
Interview Questions
- The speaker suggests being ready with interview questions related to Jenkins shared libraries.
- The interview questions can be found in the same GitHub repository under the shared libraries folder.
Turn any video into a summary like this
YouTube links, meetings, lectures. With transcripts, search, and chat.