For DevOps experts and newcomers alike, Jenkins CI/CD has long been the go-to solution. With more than 1500 plugins available, Jenkins, one of the more established players in the CI/CD market, enjoys strong community support and enables professionals to ship more quickly through their Jenkins pipeline. You've come to the perfect place if you're just beginning your CI/CD adventure or want a quick refresher on Jenkins Pipeline. This Jenkins pipeline article will provide you with all the knowledge you need to set one up, along with a thorough understanding of the underlying ideas of the Jenkins course.
What is Jenkins Pipeline?
Jenkins Pipeline is a mixture of plugins that helps the combination and implementation of non-stop transport pipelines. It has an extensible automation server to create easy and complicated transport pipelines as code through pipeline DSL. A Pipeline is a collection of occasions interlinked with every difference in a sequence.
Jenkins Features
The following are the key features offered by Jenkins:
- CI and CD: Jenkins can function as a straightforward CI server or a central center for continuous delivery for every project.
- Plugins: Hundreds of plugins allow Jenkins to be linked with virtually any tool in the continuous integration and delivery toolchain.
- Simple installation: Jenkins can be set up using native system packages, Jenkins docker, or even operate independently on any computer that has Java Runtime Environment (JRE) installed.
- Simple configuration: Jenkins' web interface allows for simple setup and configuration.
- Extensible: Jenkins is extensible; by adding plugins, it may be made to perform an endless number of additional tasks.
- Distributed: Jenkins can quickly divide work among several machines when it is distributed.
How do Continuous Delivery Pipelines work?
Each task or event in a Jenkins pipeline depends in some way on at least one or more other jobs or events.
A continuous delivery pipeline in Jenkins is shown in the image below. It has four states: build, deploy, test, and release. These things are related to one another. Each state has its events, which follow a continuous supply pipeline.
A continuous delivery pipeline is a machine-readable phrase that shows how you get software for version control. As a result, before it is made public, every modification to your software must pass through several difficult steps. It also entails progressing the programme through various testing and deployment stages while developing it in a dependable and repeatable manner. If you want to enroll in DevOps training , you are at the right place.
Why Pipelines?
Jenkins is a free, open-source continuous integration server with the capacity to help software development process automation. With the aid of use cases, you may design numerous automation jobs and run them through a Jenkins pipeline.
Jenkins pipeline is recommended for the following reasons:
- Jenkins pipeline is developed using code, allowing numerous people to change and run the pipeline process.
- The pipeline will thus be immediately resumed if your server has to restart for some reason.
- The pipeline process can be stopped, and you can instruct it to not restart until the user provides input.
- Jenkins Pipelines assist with large projects. It's possible to use pipelines in a loop and perform numerous jobs.
A continuous delivery scenario made possible by the Pipeline plugin is illustrated in the flowchart below:
Pipeline Concepts
Pipeline: The pipeline is a set of instructions for continuous delivery that are provided as code and contains all the instructions required for the complete construction procedure. The application may be built, tested, and delivered using the pipeline.
pipeline{
}
Node: Jenkins runs on a machine known as a node. The programmed pipeline syntax makes extensive use of node blocks.
node{
}
Stage: An assembly of pipeline steps is called a stage block. In other words, a stage is where the procedures for build, test, and deployment are all together. In most cases, the Jenkins pipeline process is represented by a stage block.
pipeline {
agent any
stages {
stage ('Build') {
...
}
stage ('Test') {
...
}
stage ('QA') {
...
}
stage ('Deploy') {
...
}
stage ('Monitor') {
...
}
}
}
Step: Simply put, a step is a single job that completes a particular procedure at a specific time. There are several steps in a pipeline.
pipeline {
agent any
stages {
stage ('Build') {
steps {
echo 'Running build phase...'
}
}
}
}
Pipeline Syntax Overview
Your JenkinsFile can be defined using one of two styles of syntax.
Declarative:
A straightforward method for building pipelines is the declarative pipeline syntax. It has a preconfigured hierarchy that is used to build Jenkins pipelines. It gives you the capacity to easily and plainly handle every facet of a pipeline action.
Scripted:
A lightweight executor helps the Jenkins master run scripted Jenkins pipeline syntax. The pipeline is transformed into atomic commands with a minimal number of resources. Declarative and Scripted syntax are completely distinct from one another and are defined differently.
Getting Started With Pipeline
Code compilation, building, and deployment into a production environment are all done via pipelines, which are collections of processes. Pipelines enable the following:
- One process can be used to manage automated builds, testing, and deployments.
- From testing to staging to production automatically, provide high-quality goods regularly.
- Automatically encourage or hinder the deployment of built artifacts. If mistakes are found at any point in the process, the pipeline is stopped, and alarms are delivered to the relevant team for review.
Pipeline processes include the necessary checks and balances to make sure that the quality of the software development is not sacrificed for speed. They can be configured to release frequently and behave consistently by being set to activate upon a commit to the code.
Pipeline Example
Prerequisites
A few prerequisites must be met before Jenkins can be installed and run on your system:
- The most recent version of Java (JDK) needs to be installed on your machine because Jenkins is a Java-based programme.
- To deploy applications, Apache Tomcat must be updated. Good internet connection for downloading the Jenkins war file, which is necessary for installing Jenkins.
- To install and operate the software, you need 1 GB of free disc space.
- The necessary tools, such as Apache Ant, Maven, or Gradle, should be installed on your systems depending on your applications.
Defining a Pipeline
One of the methods listed below can be used to establish a pipeline:
- Through Blue Ocean: After setting up a Pipeline project there, the Blue Ocean UI assists you in creating your Pipeline's Jenkinsfile documentation and committing it to source control.
- Through the classic UI: You may enter a simple Pipeline into Jenkins directly using the traditional UI.
- In SCM: In source control management (SCM), you can manually create a Jenkinsfile example and commit it to the repository for your project.
Through Blue Ocean
Based on the user's Jenkins experience, Blue Ocean is developed. Although it may handle freestyle tasks, it is primarily intended for the pipeline process. Blue Ocean improves each team member's clarity while reducing the confusion and disarray brought on by Jenkins.
For a person with advanced skills who is familiar with Jenkins features, Blue Ocean is quite helpful. However, if you are a newcomer, you might need to understand the fundamentals of Jenkins, particularly with regard to the pipeline process.
Blue Ocean's main attributes include:
- Supports freestyle jobs despite being created for the pipeline stages. Even you can use it to run a single job.
- Personalization: Blue Ocean gives personalized performances. Each team member may quickly visualize the builds' execution and modifications.
- With pinpoint accuracy, Blue Ocean identifies the precise area where an issue has developed and where you can draw attention in the pipeline if a problem or intervention is needed.
- Pipeline editor: Blue Ocean makes it simple to design a pipeline by providing instructions and visual representations that the user can understand.
- It is possible to make adjustments to the Blue Ocean according to your needs because it is open source.
- Cost-free: There isn't a single fee to enter the blue water. You may get it by downloading it from the managed Jenkins site. It comes as a plugin for Jenkins.
How to use
You have two options for installing the Blue Ocean:
- Making use of Jenkins plugins
- By utilizing Docker as part of Jenkins.
The Jenkins plugin is utilized
The Jenkins 2.7.x version or a later version is required in order to install Blue Ocean in Jenkins.
It is quite simple to use the Blue Ocean. Within 10 minutes, you can start using the Blue Ocean if you have the Jenkins tool installed on your computer and a reliable network connection. Use Blue Ocean by following the instructions provided:
Step 1:
Start by entering your User ID and password into the Jenkins tool.
Step 2:
Go to the dashboard and choose the "Manage Jenkins" option that is shown on the left panel.
Source link
Step 3:
Your browser will then open the Manage Jenkins page. It contains a lot of options, as you can see in the image that is being displayed, and you must choose the "Manage Plugin" option.
Step 4:
The following sections can be seen on the page that opens after you click "Manage Jenkins":
- Update
- Available
- Install
- Advance
Select the second section under "Available."
Step 5:
Get the Blue Ocean plugin by typing "Blue Ocean" into the search bar.
Step 6:
The first result you see when searching for "Blue Ocean" is it. Select the checkbox in the install column, which is directly in front of the Blue Ocean plugin.
Step 7:
Put in the plugin now. It is possible to choose between "Install without restart" and "Download now or install after restart." When you select either choice, a new page will open up and the installation procedure will start. Here, the Jenkins docker plugins and any necessary updates will be downloaded. The download process will take some time. The plugin's status changes from pending to success once the download is finished.
Source Link
Step 8:
Now that Blue Ocean has been downloaded, you can use it. Restarting Jenkins will make it function properly. By selecting the "Open Blue Ocean" tab from the Jenkins web UI's top navigation bar after the installation is complete, you can quickly use the Blue Ocean.
If you change your Jenkins URL to include "/blue," as in the example below, you can also visit the Blue Ocean directly.
https://ci.Jenkins.io.blue
When utilizing the Blue Ocean, click the "exit" icon at the top of any page if you wish to return to your Jenkins web UI.
With Docker
The Jenkins/Jenkins official Jenkins Docker image, which is available from the Docker Hub source, does not include the Blue Ocean collection of plugins.
On the installing Jenkins page, under the Jenkins Docker build section, you can read more about running Jenkins and Blue Ocean inside of Docker.
Using the traditional UI
Jenkins stores a Jenkinsfile produced using the old UI (within the Jenkins home directory).
To build a fundamental Pipeline using Jenkins' old user interface:
- If necessary, make sure Jenkins has logged you in.
- Click a new item in the top left corner of the Jenkins dashboard (the home page for the old UI of Jenkins).
- Your new Pipeline project's name should be entered in the area under "Enter an item name."
- Jenkins creates disc directories using this item name; exercise caution. It is advised against using spaces in item names because doing so could lead to script errors if such scripts don't handle directory paths appropriately.
- The Pipeline setup page will open after you pick Pipeline in the scroll-down menu and then OK at the bottom of the page (whose General tab is selected).
- The Pipeline part can be found by scrolling down after selecting the Pipeline option at the top of the page.
- Ensure that the Pipeline script option is selected in the Definition field of the Pipeline section.
- Fill up the Script text box with your Pipeline code.
Jenkinsfile (Declarative Pipeline)
pipeline {
agent any
stages {
stage('Stage 1') {
steps {
echo 'Hello world!'
}
}
}
}
- To view the project or item for the Pipeline, click Save.
- To run the Pipeline, click Run Pipeline on the left side of this page.
- To view the specific Pipeline run's specifics, click #1 under Build History on the left.
- The complete output of the Pipeline run can be viewed by clicking Console Output. Your Pipeline was successfully executed, as evidenced by the output that follows.
In SCM
The Script text section of the Pipeline configuration page's old UI makes it challenging to create and maintain complex Pipelines. A text editor or integrated development environment (IDE) can be used to create your Pipeline's Jenkinsfile, which can then be saved to source control to make this process simpler. When your Pipeline project is being built, Jenkins can check out your Jenkinsfile from source control and then carry out your Pipeline's execution.
In order to set up Pipeline for the use of a Jenkinsfile from source control:
- Once you get to step 5, continue the steps outlined above for defining your Pipeline using the old UI.
- Select the Pipeline script from the SCM option in the Definition column.
- Select the source control system used by the repository holding your Jenkinsfile in the SCM box.
- Fill up the fields related to your repository's source code management system.
- Name and location of your Jenkinsfile should be entered in the Script Path section. Jenkins clones or checks out your Jenkinsfile from this location, which should have a file structure that matches the repository.
Your Jenkinsfile's name and the location at the repository's root are implied by the default value of this field, "Jenkinsfile".
If the Pipeline is set up with an SCM polling trigger, a fresh build is started whenever you make changes to the chosen repository.
Built-in Documentation
For making Pipelines of varied complexity easier to design, Pipeline comes with built-in documentation features. Based on the installed plugins in the Jenkins instance, this built-in documentation is generated and updated automatically.
The global location of the built-in documentation is $YOUR JENKINS URL/pipeline-syntax. Any Pipeline project that has been configured will have a link to the same documentation under Pipeline Syntax in the sidebar.
Snippet Generator
The integrated "Snippet Generator" tool is useful for producing little chunks of code for individual stages, finding new steps offered by plugins, or experimenting with various parameters for a certain step.
A list of the steps available to the Jenkins instance is dynamically added to the Snippet Generator. The installed plugins that explicitly offer steps for use in Pipeline will determine how many steps are accessible.
Use the Snippet Generator to create a step snippet:
- You can access the Pipeline Syntax link (mentioned above) from a Pipeline that has been configured or by going to $(YOUR JENKINS URL)/pipeline-syntax.
- Choose the required action from the Sample Action drop-down menu.
- Use the dynamically populating space beneath the Sample Step dropdown to set the chosen step's options.
- To create a small piece of pipeline code that can be copied and pasted into a pipeline, select Generate Pipeline Script from the menu.
Global Variable Reference
Pipeline offers a built-in "Global Variable Reference" in addition to the Snippet Generator, which simply surfaces stages. It utilizes plugins to dynamically populate data, just like the Snippet Generator. The Global Variable Reference, in contrast, only includes documentation for variables supplied by Pipelines or plugins that are accessible for Pipelines.
In Pipeline, the following variables are pre-defined:
Env: Environment variables, such as PATH or BUILD ID, are exposed through env. For a complete and current list of the environment variables available in Pipeline, look to the built-in global variable reference at $YOUR JENKINS URL>/pipeline-syntax/globals#env.
Params: As an illustration, params. MY PARAM NAME exposes all parameters that have been defined for the Pipeline as a read-only Map.
CurrentBuild: Can be used to learn details about the Pipeline that is now running, including attributes like currentBuild.result and currentBuild.displayName. For a complete and current list of properties available on currentBuild, look to the built-in global variable reference at $YOUR JENKINS URL/pipeline-syntax/globals.
Declarative Directive Generator
The sections and directives needed to establish a Declarative Pipeline are not covered by the Snippet Generator, but it does assist with creating steps for a Scripted Pipeline or for the steps block in a stage of a Declarative Pipeline. This is made less complicated with the "Declarative Directive Generator" tool. The Directive Generator works similarly to the Snippet Generator in that it enables you to select a Declarative directive, configure it in a form, and then generate the configuration for that directive, which you can then use in your Declarative Pipeline.
The Declarative Directive Generator can be used to create a declarative directive as follows:
- You can access the Declarative Directive Generator by going to the $YOUR JENKINS URL/directive-generator from a configured Pipeline's Pipeline Syntax link (mentioned above).
- The dropdown menu will allow you to choose the desired directive.
- Configure the chosen directive using the dynamically populating space below the dropdown.
- For the directive's configuration to be created and copied into your pipeline, click Generate Directive.
Although it cannot provide pipeline steps, the Directive Generator can generate settings for nested directives, such as conditions inside a when directive. The Directive Generator substitutes a placeholder comment for the contents of directives that comprise steps, such as steps inside of a stage or conditions like always or failure inside of a post. Your Pipeline will still require manual step addition.
Jenkinsfile (Declarative Pipeline)
stage('Stage 1') {
steps {
// One or more steps need to be included within the steps block.
}
}
Pipeline Example
Basic Pipeline examples in a variety of languages.
Python
Jenkinsfile (Declarative Pipeline)
pipeline {
agent { docker { image 'python:3.10.1-alpine' } }
stages {
stage('build') {
steps {
sh 'python --version'
}
}
}
}
Java
Jenkinsfile (Declarative Pipeline)
pipeline {
agent { docker { image 'maven:3.8.4-openjdk-11-slim' } }
stages {
stage('build') {
steps {
sh 'mvn --version'
}
}
}
}
Go
Jenkinsfile (Declarative Pipeline)
pipeline {
agent { docker { image 'golang:1.17.5-alpine' } }
stages {
stage('build') {
steps {
sh 'go version'
}
}
}
}
PHP
Jenkinsfile (Declarative Pipeline)
pipeline {
agent { docker { image 'php:8.1.0-alpine' } }
stages {
stage('build') {
steps {
sh 'php --version'
}
}
}
}
Conclusion
Shortening production times and raising application quality can both be accomplished with a well-defined Jenkins pipeline. Your current building, committing, automated testing, and deployment processes will have a clear structure thanks to it.
I sincerely hope my Jenkins pipeline blog was beneficial to you and that you were able to effectively establish your first Jenkins Pipeline as a result. Also, go through the KnowledgeHut Jenkins course for a deeper understanding.