Jenkins is a DevOps tool which can be used to automate your build, test and delivery of software code. If you are new to Jenkins, this tutorial will help you to understand how to create Jenkins pipeline using one of the following methods:
- Classic Jenkins User Interface
- Jenkins Blue Ocean User Interface which reduces clutter and increases clarity
- Jenkinsfile Pipeline Script from a SCM like Git Repository
Method 1: Create Pipeline from Classic Jenkins UI
From the Jenkins menu, click on “Create New Item”. Enter the name of your pipeline: Test-Pipeline. Choose Pipeline from the list of choices. Click Ok as shown below.
Once the test pipeline is created, edit the pipeline. Click on “Pipeline” Tab, and paste the following code. This sample code has three simple stages and steps defined inside each stage. For this simple example, the steps section just has echo command which will display a message.
pipeline { agent any stages { stage('Stage 1: Integrate Web and DB') { steps { echo '1.1 Getting application web files' echo '1.2 Getting database files' echo '1.3 Combining web and db files' } } stage('Stage 2: Integration testing') { steps { echo '2.1 Performing integration testing' } } stage('Stage 3: Release to Prod') { steps { echo '3.1 Releasing code to production' } } } }
When the pipeline is selected, click on “Build Now” from the side menu bar which will execute the pipeline as shown below.
Once the pipeline is executed, you can view the logs from the Console output menu item on the side menu bar as shown below.
Method 2: Create Pipeline from Blue Ocean Interface
Blue Ocean gives a better user experience for Jenkins. This UI is designed from ground up for Jenkins pipeline and reduces clutter and increases UI clarity.
First, search for “blue ocean” and install Blue Ocean Plugin from Jenkins Plugin Manager . After you install the plugin, restart Jenkins.
On a related note, refer to this to keep your Jenkins and Plugins upto date: How to Upgrade Jenkins to New Version and Update Plugins
On the Jenkins server, install a local git repository for blueocean to use in the pipeline.
sudo yum install git -y cd /var/lib/jenkins mkdir localgit cd localgit git clone https://my-git-repor-url/v1/repos/BinCode
Note: I gave the git repository path as /var/lib/jenkins/localgit/BinCode
After the plugin is installed, you’ll see a new menu item on the side bar called “Open Blue Ocean” as shown below:
You can also access blue ocean UI directly by appending /blue to the jenkins URL. For example: http://192.168.101.100:8080/blue
First time, Blue Ocean will look for Jenkinsfile in your local git repository and will run a pipeline for each branch containing Jenkins file.
Since we have not created a Jenkinsfile yet, it will display a prompt create a new pipeline from the user interface.
The pipeline creation UI will walk you through the steps to create a new pipeline. First, Click on the + next to the start. Click on “Add a Step” and select “Print Message” as shown below.
After entering the message, click on the back arrow that is in front of the stage name to go back. Don’t click on Save yet.
Add another Step – Print Message – 1.2 Getting database files – Click on the back arrow in front of the stage name to go back.
Repeat above for the 3rd echo step as shown below.
After creating all the three stages, click on the Save button to save the pipeline. You can commit your changes to the master, or create a new branch. For this example, select “Commit to new branch” and give the name as “jenkins” as shown below.
Once the pipeline is created and executed, it will display the status of each stage. Click on a particular stage name to see the corresponding stage output as shown below.
On the Jenkins server, you can see a new branch called “jenkins” is now created for your repository
# git branch -a jenkins * master remotes/origin/HEAD -> origin/master remotes/origin/master
Method 3: Create using Jenkinsfile Pipeline Script From Git Repository
First, create Jenkinsfile under your repository as shown below. In this example, I’m using the Jenkinsfile that already exists in the branch. If you don’t have this file, create one.
# cd /var/lib/jenkins/localgit/BinCode # git checkout jenkins Switched to branch 'jenkins' # git branch -a * jenkins master remotes/origin/HEAD -> origin/master remotes/origin/master # cat Jenkinsfile pipeline { agent any stages { stage('Stage 1: Integrate Web and DB') { steps { echo '1.1 Getting application web files' echo '1.2 Getting database files' echo '1.3 Combining web and db files' } } stage('Stage 2: Integration testing') { steps { echo '2.1 Performing integration testing' } } stage('Stage 3: Release to Prod') { steps { echo '3.1 Releasing code to production' } } } }
First, follow similar step as explained in Method 1 to create a new pipeline from Classic UI and name this pipeline as: SCM-Test-Pipeline.
Next, select SCM-Test-Pipeline and under “Pipeline” Tab, for Definition select “Pipeline script from SCM” option as shown below. Make sure the Script Path says “Jenkinsfile” as shown below.
Now this will also ask for your SCM repository details. Enter your git repository details here. If your Jenkinsfile is in a specific branch, make sure to specify the branch name.
To understand how to setup Jenkins credentials for Git, refer to this: How to Setup Git Repository and Credentials for Jenkins Jobs
After you specify the git repository detail, execute the pipeline. Once the pipeline is executed, from the side menu bar click on Console output to view the execution log of the pipeline.