Ultimate Jenkins tutorial for DevOps Engineers

Jenkins is an open source automated CI/CD tool where CI stands for continuous integration and CD stands for Continuous delivery. Jenkins has its own built-in Java servlet container server which is Jetty. Jenkins can also be run in different servlet containers such as Apache tomcat or glassfish.

  • Jenkins is used to perform smooth and quick deployment. It can be deployed to local machine or on premises data center or any cloud.
  • Jenkins takes your code any sort of code such as python, java or go or JS etc. and compiles it using different compiler such as MAVEN one of the most used compiler and then builds your code in war or Zip format and sometimes as a docker Image. Finally once everything is built properly it deploy as an when required . It integrates very well with lots of third party tools.

JAVA_HOME and PATH are variables to enable your operating system to find required Java programs and utilities.

JAVA_HOME: JAVA_HOME is an (OS) environment variable that can optionally be set after either the (JDK) or (JRE) is installed. The JAVA_HOME environment variable points to the file system location where the JDK or JRE was installed. This variable should be configured on all OS’s that have a Java installation, including Windows, Ubuntu, Linux, Mac, and Android. 

The JAVA_HOME environment variable is not actually used by the locally installed Java runtime. Instead, other programs installed on a desktop computer that requires a Java runtime will query the OS for the JAVA_HOME variable to find out where the runtime is installed. After the location of the JDK or JRE installation is found, those programs can initiate Java-based processes, start Java virtual machines and use command-line utilities such as the Java archive utility or the Java compiler, both of which are packaged inside the Java installation’s \bin directory.

  • JAVA_HOME if you installed the JDK (Java Development Kit)
    or
  • JRE_HOME if you installed the JRE (Java Runtime Environment) 

PATH: Set the PATH environment variable if you want to be able to conveniently run the executables (javac.exejava.exejavadoc.exe, and so on) from any directory without having to type the full path of the command. If you do not set the PATH variable, you need to specify the full path to the executable every time you run it, such as:

C:\Java\jdk1.8.0\bin\javac Myprogram.java
# The following is an example of a PATH environment variable:

C:\Java\jdk1.7.0\bin;C:\Windows\System32\;C:\Windows\;C:\Windows\System32\Wbem

Installing Jenkins using msi installer on Windows Machine

MSI is an installer file that installs your program on the executing system. Setup.exe is an application (executable file) that has MSI file(s) as one of the resources. The MSI is the file extension of MSI files. They are Windows installers. An MSI file is a compressed package of installer files. It consists of all the information pertaining to adding, modifying, storing, or removing the respective software.  MSI file includes data, instructions, processes, and add-ons that are necessary for the application to work normally.

EXE is short for Executable. This is any kind of binary file that can be executed. All windows programs are exe files. Prior to MSI files, all installers were EXE files. The exe is a file extension of an executable file. An executable file executes a set of instructions or a code when opening it. An executable file is compiled from source code to binary code. It can be directly executed by the Windows OS. These files are understandable by the machine, and they can be directly executed by the operating system

MSI is a file extension of windows installer which is a software component of Microsoft Windows used for the installation, maintenance, and removal of software. Whereas, exe is a file extension of an executable file that performs indicated tasks according to the encoded instructions. 

  1. Navigate to https://www.jenkins.io/download/ and select windows option and your download of Jenkins msi will begin.
  1. Once downloaded click on the jenkins.msi
  1. Continue the Jenkins setup.
  1. Select the Port 8080 and click on Test Port and then Hit Next.
  1. Provide the admin password from the provided Path mentioned in RED color.
  1. Further install the plugins required for jenkins.
  1. Next,it will prompt for First admin user. Please fill the required information and keep it safe with you , as you will use this to login.
  1. Now Jenkins URL configuration screen will appear , keep it as it is for now.
  1. Click on Save and Finish.
  1. Now your Jenkins is ready , click on Start using Jenkins. Soon, you will see Jenkins Dashboard. You can create New Jobs by clicking on New Item.

Installing Jenkins using jenkins exe on Windows Machine

  1. Similarly now install jenkins.war from jenkins URL and click on Generic Java package(.war).
  2. Next run the command as below.
java -jar jenkins.war -http=8181
  1. Next, copy the Jenkins password from the log output and paste it in the as you did earlier in windows msi section point (5) and follow rest of the points.

Installing jenkins on Apache Tomcat server on Windows Machine

  1. Install the Apache Tomcat on windows machine from https://tomcat.apache.org/download-90.cgi and click on tomcat installer as per your system. This tutorial is performed on 64 bit windows machine.
  1. Next, unzip the tomcat installation folder and copy the jenkin.war file in the webapps folder.
  1. Next, go inside the bin folder and run the tomcat by clicking on the startup batch script.
  1. Finally you will notice that Apache Tomcat has started and Jenkins as well.
  1. Now, navigate to localhost:8080 URL and you should see tomcat page as shown below.
  1. Further, navigate to localhost:8080/jenkins to redirect to Jenkins Page.

Configuring the Jenkins UI

  1. First click on Manage Jenkins and then navigate to Configure system.
  1. Next, add the system message and save it which should display this message on Jenkins everytime as below.
  1. To configure the name of the Jobs add the name Pattern as below.
  1. Next, try creating a a new Jenkins Job with random name then it will not allow you and display the error message.

Managing User’s and Permission’s in Jenkins UI

  • Go to Manage Jenkins and Navigate to Manage users in the Jenkins UI.
  • Then Create three users as shown below admin, dev, qa.
  • Next, Navigate to Manage Jenkins and choose Configure Global Security.
  • Next select Project-based Matrix Authorization Strategy and define the permissions for all users as you want.

Role Based Stratergy

  • In Previous section you noticed that adding all users and grnating all permissions is little tough job. So, instead create a role and add users in it. To do that first step is to install the Plugin as shown below.
  • Next select Role based Stratergy as shown below and define the permissions for all users as you want.
  • Next, navigate to Manage Jenkins and then to Manage and Assign Jenkins and then click on Manage Roles.
  • Add 3 Global Roles named DEV Team, QA Team and admin.
  • Add 2 Items Roles developers and Testers with define patterns so that Jobs names are declared accordingly.
  • Next, Click on Assign Role
  • Assigning the roles as shown below.

Conclusion

In this tutorial you learnt how to install jenkins on windows through various ways , how to configure Jenkins Dashboard UI and how to manager users and Permissions.

Advertisement

The Ultimate Guide: Getting Started with Jenkins Pipeline

Are you struggling to automate the application deployment? Why not use Jenkins and Jenkins Pipeline to automate all the deployments more easily and effectively.

With Jenkins Pipeline, the deployment process is earlier and gives you more features to incorporate while deploying or managing the infrastructure.

In this tutorial, you will learn what is Jenkins Pipeline in depth. Let’s get started.

Join 50 other followers

Table of Content

  1. What is CI/CD ( Continuous Integration and Continuous deployments)?
  2. What is Jenkins Pipeline?
  3. How to create a basic Jenkins Pipeline
  4. Handling Parameters in Jenkins Pipeline
  5. How to work with Input Parameters
  6. Conclusion

Prerequisites

What is CI/CD ( Continuous Integration and Continuous deployments)?

CI/CD products are delivered to clients ingeniously and effectively using different automated stages. CI/CD saves tons of time for both developer and operations team, and there are very few chances of human errors. CI/CD stands for continuous integration and continuous deployments. It automates everything starting from integrating to deployments.

Continuous Integration and Continuous deployments
Continuous Integration and Continuous deployments

What is Continuous Integration?

Continuous integration is primarily used by developers so that developers’ code is built, tested, and then pushed to a shared repository whenever there is a change in code.

For every code push to the repository, you can create a set of scripts to build and test your application automatically. These scripts help decrease the chances that you introduce errors in your application.

This practice is known as Continuous Integration. Each change submitted to an application, even to development branches, is built and tested automatically and continuously.

What is Continuous Delivery?

Continuous delivery is a step beyond continuous integration. With Continuous Delivery, the application is not only continuously built and tested each time the code is pushed but the application is also deployed continuously. However, with continuous delivery, you trigger the deployments manually.

Continuous delivery checks the code automatically, but it requires human intervention to deploy the changes.

What is Continuous Deployment

Continuous deployment is again a step beyond continuous integration and the only difference between deployment and delivery is: deployment automatically takes the code from a shared repository and deploys the changes to environments such as Production where customers can see those changes.

This is the final stage of the CI/CD pipeline. With Continous Deployment, it hardly takes a minute to deploy the code to the environments. It depends on heavy pre-automation testing.

Examples of CI/CD :

  • Spinnaker and Screwdriver built platform for CD
  • GitLab, Bamboo, CircleCI, Travis CI and GoCD are built platform for CI/CD.

What is Jenkins Pipeline?

Jenkins Pipeline uses a group of plugins that help deliver a complete continuous delivery pipeline starting from building the code till deployment of the software right up to the customer.

Jenkins Pipeline plugin is automatically installed while installing the Jenkins with suggested plugins and allows you to write complex operations and code deployment as code using DSL language ( Domain-specific language). Some of the benefits of the Jenkins pipeline are:

Jenkinsfile ( declarative vs scripted pipeline)

Jenkins pipeline is written in code with Jenkinsfile that is easier, gives more ability to review, and supports various extensions & plugins. Also if Jenkins stops you can still continue to write or update the Jenkinsfile. With code capabilities you can allow waiting, approvals, stop, and many other functionalities. Jenkinsfile follows two syntaxes:

  • Declarative Pipeline: This is newer and writing code with this is much easier.
  • Scripted Pipeline: This is older and writing code with this is a little complicated.

Scripted pipeline syntax

Jenkins provides you an easier way to generate the Scripted pipeline syntax by navigating to the below URL.

http://Jenkins-server:8080/pipeline-syntax/
Scripted pipeline syntax
Scripted pipeline syntax

Declarative Pipeline syntax

Jenkins also provides you a way to generate the Declarative pipeline syntax by navigating to the below URL.

http://Jenkins-server:8080/directive-generator/
Declarative Pipeline syntax
Declarative Pipeline syntax

Jenkins variables

Let’s quickly look at the Jenkins Pipeline environmental variables that are supported.

  • BUILD_NUMBER: Displays the build number
  • BUILD_TAG: Displays the tag which is jenkins-${JOB_NAME}-${BUILD_NUMBER}
  • BUILD_URL: Displays the URL of the result of Build
  • JAVA_HOME: Path of Java home
  • NODE_NAME: It specifics the name of the node. For example, set it to master is for Jenkins controller
  • JOB_NAME: Name of the Job

You can set the environmental variables dynamically in the Jenkins pipeline as well.

    environment {
        AWS_ACCESS_KEY_ID     = credentials('jenkins-aws-secret-key-id')
        AWS_SECRET_ACCESS_KEY = credentials('jenkins-aws-secret-access-key')
        MY_KUBECONFIG = credentials('my-kubeconfig')
   }

Jenkinsfile example

Previously you learned about Jenkinsfile syntax and variables that can be included in Jenkinfile. In this section lets learn by seeing a basic example declarative pipeline. 

Below are all the arguments that are used within the Jenkinsfile and their function is specified below.

  • agent: Agent allows Jenkins to allocate an executor or a node. For example Jenkins slave
  • Stages: It include multiple tasks which Pipeline needs to perform. It can have a single task as well.
  • Stage: Stage is one single task under stages.
  • steps: These are steps which needs to be executed in every stage.
  • sh: sh is one of the step which executes shell command.
pipeline {
   agent any 
    stages {
        stage('Testing the Jenkins Version') {
            steps {
                echo 'Hello, Jenkins'
                sh 'service jenkins status'
               //  sh("kubectl --kubeconfig $MY_KUBECONFIG get pods")
            }
        }
    }
}

Jenkins Job example to run Jenkins Pipeline

In this section, lets quickly learn how you can execute jenkins job with Jenkinsfile and Jenkins Pipeline. Lets create Jenkins job quickly.

  • Navigate to Jenkins URL and click on New Item.
Click on new item in Jenkins Job
Click on new item in Jenkins Job
  • Next, choose Pipeline as the type of Jenkins job from the option as shown below, provide the of Jenkins Job as pipeline-demo and click OK.
Choosing the Pipeline and naming the Jenkins Job
Choosing the Pipeline and naming the Jenkins Job
  • Now in the Jenkins job provide the description as my demo pipeline.
  • Further in the script copy/paste the below code.
pipeline {
   agent any 
    stages {
        stage('Testing the Jenkins Version') {
            steps {
                echo 'Hello, Jenkins'
                sh 'service jenkins status'
            }
        }
    }
}
  • Finally click on save & click on Build Now button.
Running the Jenkins Job to run Jenkins Pipeline
Running the Jenkins Job to run Jenkins Pipeline

Now that you have successfully run your first Jenkins Job using Jenkins Pipeline. lets verify the code execution from console output of the Job and to do that click on the build number from build history.

Build History of Jenkins Job
Build History of Jenkins Job
Verifying the Jenkins Job using console output
Verifying the Jenkins Job using console output

Handling Parameters in Jenkins Pipeline

If you wish to use Build with Parameters , so those parameters are accessible using params keyword in pipeline.

Lets see a quick example. In below code we have Profile as a parameter and it can be accessed as ${params.Profile} .Lets paste the code in pipeline script as we did earlier

pipeline {
  agent any
  parameters {
    string(name: 'Profile', defaultValue: 'devops-engineer', description: 'I am devops guy') 
}
 stages {
    stage('Testing DEVOPS') {
       steps {
          echo "${params.Profile} is a cloud profile"
       }
     }
   }
}
  • Lets build the Jenkins pipeline now.
  • Next verify the console output
  • Similarly we can use different parameters such
pipeline {
    agent any
    parameters {
        string(name: 'PERSON', defaultValue: 'AutomateInfra', description: 'PERSON')
        text(name: 'BIOGRAPHY', defaultValue: '', description: 'BIOGRAPHY')
        booleanParam(name: 'TOGGLE', defaultValue: true, description: 'TOGGLE')
        choice(name: 'CHOICE', choices: ['One', 'Two', 'Three'], description: 'CHOICE')
        password(name: 'PASSWORD', defaultValue: 'SECRET', description: 'PASSWORD')
    }
    stages {
        stage('All-Parameters') {
            steps {
                echo "I am ${params.PERSON}"
                echo "Biography: ${params.BIOGRAPHY}"
                echo "Toggle: ${params.TOGGLE}"
                echo "Choice: ${params.CHOICE}"
                echo "Password: ${params.PASSWORD}"
            }
        }
    }
}

How to work with Input Parameters

Input parameter allows you to provide an input using a input step. Unless input is provided the pipeline will be paused. Lets see a quick example in which Jenkins job will prompt for “should we continue” message. Unless we approve it will remain as it is else finally it will abort.

pipeline {
    agent any
    stages {
        stage('Testing input condition') {
            input {
                message "Should we continue?"
                ok "Yes, we should."
                submitter "automateinfra"
                parameters {
                    string(name: 'PERSON', defaultValue: 'Automate', description: 'Person')
                }
            }
            steps {
                echo "Hello, ${PERSON}, nice to meet you."
            }
        }
    }
}
  • Lets paste the content in Jenkins pipeline script and click on build now.
  • Let us verify by clicking on build Now

Conclusion

In this tutorial we learnt what is CI/CD and CI/CD open source tool Jenkins. We covered how to write pipeline and syntax of Jenkins pipeline using its language known as DSL ( domain specific language ) . Also we learnt in depth of Jenkins pipeline and created basic Jenkins pipeline and executed it.

Hope this tutorial will help you a kick start to how to work with Jenkins pipeline and execute them. If you like this please share it.

How to Install Jenkins on Ubuntu Machine Step by Step

Are you spending a lot of time deploying code in your infrastructure and with many steps and software? Don’t worry; you are at the right place to learn about the famous open-source tool Jenkins, which helps build and deploy the code with a click of a button and has various other benefits.

In this tutorial, you will learn what is Jenkins and how can you install Jenkins on ubuntu 18.04 and setup step by step. Still Interested?

Join 50 other followers

Table of Content

  1. What is Jenkins
  2. Prerequisites
  3. How to Install Java on ubuntu 18.04 machine
  4. How to Install Jenkins on Ubuntu 18.04 LTS
  5. How to setup Jenkins on ubuntu
  6. Workflow of Jenkins and Code deployment
  7. Conclusion

What is Jenkins?

Jenkins is an open-source automated CI/CD tool where CI stands for continuous integration and CD stands for Continuous delivery. Jenkins has its own built-in Java servlet container server, which is Jetty. Jenkins can also be run in different servlet containers such as Apache tomcat or glassfish.

  • Jenkins can be deployed to the local machine or premises data center, or any cloud.
  • Jenkins takes your code, any sort of code such as python, java, or go or JS, etc., and compiles it using different compilers such as MAVEN, one of the most used compilers, and then builds your code in war or Zip format and sometimes as a docker image.
  • Jenkins deploys the code when required and integrates easily with various third-party tools.

Related: Python Flask Tutorial: All about Python flask

Prerequisites

  • Ubuntu 18.0.4 machine preferably; if you don’t have any machine, you can create an ec2 instance on the AWS account.
  • It is recommended to have 4GB of RAM
  • At least 5GB of drive space

You may incur a small charge for creating an EC2 instance on Amazon Managed Web Service.

How to Install Java Version 11 on ubuntu 18.04 machine

Before you dive into the installation of Jenkins on your ubuntu machine, you will need Java as a prerequisite, without which Jenkins will not work. So, let’s begin and learn how to Install Java Version 11 on an Ubuntu 18.04 machine using a few basic steps.

  • Firstly, install java using the apt install command as shown below.
sudo apt install default-jdk  # Here we are installing Java Version: Java SE 11 (LTS)
  • Next, verify the java version on your machine.
java -version               # To check the Installed Java Version
Checking the Java version
Checking the Java version
  • Check the location of java by using the which and where command.
which java :         # It will locate executable file location which is /usr/bin/java 
whereis java         # It will give the location  of all the files related to Java 
Checking the Java location
Checking the Java location

The Installation directory of Java is /usr/lib/jvm/java-1.8.0-openjdk-amd64, and this confirms that Java is successfully installed on our ubuntu 18.04 machine. Now, let us install Jenkins.

How to Install Jenkins on Ubuntu 18.04 LTS

Now that you have Java installed on your ubuntu machine. Now, it’s time to install Jenkins, an easier and more straightforward task. Let’s dive in to install Jenkins on Ubuntu 18.04 LTS machine.

  • First, update your system packages on the ubuntu machine using the below command.
sudo apt update
  • Next, add Jenkins Public key to your system so that this key can trust the jenkins.io site to download the repository or any packages.
wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add -
  • Now, add the Jenkins repository to your system and update your system.
sudo sh -c 'echo deb https://pkg.jenkins.io/debian-stable binary/ > \
    /etc/apt/sources.list.d/jenkins.list'
sudo apt-get update
  • Now install the Jenkins package using the apt-get command.
sudo apt-get install jenkins

As of now, Jenkins service should be up and running. To check the status of Jenkins, run the below command as shown.

service jenkins status
Checking the Java Service status
Checking the Java Service status

How to setup Jenkins on ubuntu

Now that you have installed Jenkins on your ubuntu machine successfully and verified by running the service command. But the installation is just an initial stage; you will need to set the Jenkins to make it work properly. Let’s learn step by step how to set Jenkins instance.

  • Open the Jenkins instance on your favorite web browser by going to <Jenkins-installation-server-ip-address>:8080.
  • As soon as the URL is opened, it will prompt for the administrator password. The password can be found on /var/lib/Jenkins/secrets/initialAdminPassword directory, as shown below.
Unlocking the Jenkins initial password
Unlocking the Jenkins initial password
  • Now obtain the password by going to the Jenkins server and viewing the following content.
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
  • After successfully adding the Jenkins admin password, you will be prompted for Plugins installation. We will select the Install Suggested Plugins option as this is the demo.
Installing Plugins in Jenkins
Installing Plugins in Jenkins
Plugins installing and Jenkins getting started
Plugins installing and Jenkins getting started.
  • Once the plugins are successfully installed, then it will prompt to create a First admin user. Please fill in the required information and keep it safe with you, as you will use this later to log in.
Creating first Admin User in Jenkins
Creating the first Admin User in Jenkins
  • Now Jenkins URL configuration screen will appear, and you can keep the default URL as it is and click on save and finish.
Configuring the Jenkins Instance
Configuring the Jenkins Instance
Jenkins is ready to be used
Jenkins is ready to be used.
  • Now Jenkins instance is ready for use. Click “Start using Jenkins.” After you click, you will see Jenkins Dashboard, where you create New Jobs by clicking on New Item.
Jenkins Dashboard
Jenkins Dashboard

Workflow of Jenkins deployment

This section is an additional section that provides you a basic overview of how a Jenkins deployment happens step by step and what Jenkins can do for you. Let’s get into it.

  • Once Jenkins is installed, install plugins as per the requirement, whether terraform, bitbucket, python, blue-ocean, etc.
  • Jenkins is configured so that it can fetch the code from any of the repositories such as GIT, bitbucket, SVN, or TFS.
  • Once you define the code repository, Jenkins pulls the code, compiles it using plugins if required, and later builds it. You can include a few test cases along with a build to test some functionalities of code.
  • Later you deploy the code on required environments such as AWS, GCP, Windows machines, Linux machines, etc.
Jenkins deployments workflow
Jenkins deployments workflow

Join 50 other followers

Conclusion

In this tutorial, you learned what Jenkins is, how to install Java on the ubuntu 18.04 machine, how to Install Jenkins on Ubuntu 18.04 LTS, and how to set up Jenkins on ubuntu and setup.

Jenkins is a handy tool for CI and CD purposes and is most widely used, which helps manage your organization’s deployments.

Now that you have Jenkins installed and set up, which application are you planning to deploy?