How to install and create pipelines in Jenkins

Last updated: November 10th 2022

Introduction

Jenkins is an open-source tool written in Java to set up continuous integration and continuous deployment (CICD) pipelines for faster software delivery. It is the main part of software development life cycle automation. This guide describes the step-by-step procedure of installing Jenkins and creating pipelines in Jenkins.

Prerequisites

Jenkins installation

First update the apt packages on ubuntu

$ sudo apt-get update -y

Install openjdk

$ sudo apt-get install default-jdk

Setup Jenkins' stable repository using following command

$ curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo tee /usr/share/keyrings/jenkins-keyring.asc > /dev/null 
$ echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] https://pkg.jenkins.io/debian-stable binary/ | sudo tee /etc/apt/sources.list.d/jenkins.list > /dev/null

Update the apt packages

$ sudo apt-get update -y

Install Jenkins using the following command

$ sudo apt-get install jenkins -y

After installation, enable and start Jenkins service.

$ sudo systemctl enable jenkins
$ sudo systemctl start jenkins                   

Check the status of Jenkins service

$ sudo systemctl status jenkins

In case of our Webdock perfect server stacks, open port 8080

$ sudo ufw allow 8080

Set Up Jenkins

After installing Jenkins, access the server on port 8080 of the Webdock instance like http://www.example.com:8080

Jenkins will ask for an administrator password to unlock it. Get the administrator password from the Webdock instance using following command.

$ sudo cat /var/lib/jenkins/secrets/initialAdminPassword

webdock1.png

Next it will ask for plugins installation. You can install plugins of your own choice or click on install suggested plugins button to install mostly used plugins.

After successful installation of plugins, it will take you to create the admin user for Jenkins.

After creating the admin user, Jenkins is properly configured and is ready to use.

Types of Jenkins pipelines

Freestyle project

Freestyle projects can be configured from Jenkins GUI by adding steps. It provides a lot of flexibility but it is much more difficult to write complex pipelines using the freestyle project. Using freestyle project you can

  • Execute windows batch commands
  • Invoke gradle script
  • Execute shell commands
  • Run workflow with timeout

Post build actions can also be added to the freestyle project using GUI. The post build action includes

  • Archive the artifacts
  • Build other project
  • Send email notification
  • Delete workspace after build is completed
  • Aggregate downstream test results
  • Publish JUnit test result report

Scripted pipeline

Scripted Pipeline was the first version of pipeline as a code in Jenkins. It was designed as a general-purpose DSL (Domain Specific Language) built with Groovy syntax. Jenkinsfile in the scripted pipeline is written on the Jenkins UI.

Scripted pipelines have only two types of basic blocks i.e, node and stage block. A node block specifies the machine that executes a particular pipeline and stage block can be a single stage or multiple as the task goes.

Declarative pipeline

Declarative Pipeline is a modern and more simplified way of writing Jenkinsfile. Declarative Pipeline is also written in a groovy syntax. In the declarative Pipeline, Jenkinsfile is stored and checked into a source control management system like git. On run time, the Jenkinsfile from git repo is cloned and executed.

Adding credentials to Jenkins

To enable Jenkins to interact with many third party applications like servers on cloud or github, credentials are added in Jenkins. Jenkins uses these credentials to connect with third party services. In order to add credentials to Jenkins, follow these steps.

Install credentials plugin

Before adding credentials, first install credentials plugin in Jenkins. The credentials plugin allows the user to store credentials in Jenkins.

While setting up the Jenkins, if you have installed suggested plugins, then this plugin is already installed. Otherwise click on Manage Jenkins from Jenkins dashboard

webdock2.png

From the Manage Jenkins dashboard, click on the Manage Plugins

webdock3.png

From the Plugin Manager, go to the Available tab.

webdock4.png

Search for the Credentials Plugin plugin from the available plugins and install it.

webdock5.png

If you do not see any Credentials Plugin plugin in the available tab, check it in the installed tab. If this plugin exists in the installed tab then it is already installed.

Add credentials

After installing the Credentials plugin, now click on the Manage Credentials under the Security section from Manage Jenkins dashboard.

webdock6.png

Under Stores scoped to Jenkins, click on the Jenkins.

webdock7.png

Click on the global credentials (unrestricted).

webdock8.png

From the left side panel, click on the Add Credentials button.

webdock9.png

Now it will ask for different attributes of the credentials you want to store. Following types of credentials can be added to Jenkins.

  • Username with password
  • Github app
  • SSH username with private key
  • Secret file
  • Secret text
  • Certificates

In order to add a simple username with password, add the details as follows

webdock10.png

After adding details of credential, click on the OK button and it will add the credentials.

webdock11.png

Writing CICD pipeline in Jenkins

In order to create a CICD pipeline in Jenkins, click on the New Item from the left side panel in Jenkins dashboard

webdock12.png

Add the name of pipeline and select Pipeline as the type of pipeline.

webdock13.png

It will take you to the configuration page of the pipeline. Scroll down to the Pipeline section and add the following script to the pipeline.

webdock14.png

                        pipeline {

                          agent any

                          stages {

                            stage('clone repo') {

                              steps {

                                script {

                                  sh '''

                                    echo "clone github repo"

                                  '''

                                }

                              }

                            }

                            stage('build project') {

                              steps {

                                script {

                                  sh '''

                                    echo "building the project"

                                  '''

                                }

                              }

                            }

                            stage('run tests') {

                              steps {

                                script {

                                  sh '''

                                    echo "running automated test suite"

                                  '''

                                }

                              }

                            }

                            stage('deploy application') {

                              steps {

                                script {

                                  sh '''

                                    echo "deploy the application to server"

                                  '''

                                }

                              }

                            }

                          }

                        }

                    

This pipeline script has multiple stages to deploy the application to the server. After writing the pipeline script, save the pipeline. Now from the pipeline, click on the Build now button to trigger the pipeline manually.

webdock15.png

After the pipeline runs, it will show the status of the pipeline on Jenkins UI as follows.

webdock16.png

Conclusion

Jenkins is the mostly used tool to build CI/CD pipelines for software delivery. This guide briefly describes the installation process of Jenkins and then writing the first CICD pipeline in Jenkins.

Related articles