top of page

Jenkins - Everything you need to Know

  • Writer: Amit Dhanik
    Amit Dhanik
  • Oct 25, 2022
  • 6 min read

Hi Guys, This is going to be a comprehensive guide on Jenkins and I'll try to cover all of the important things that you should know. Jenkins is a standard automation tool used for testing and CI/ CD deployment by all companies. It's a great tool to learn about the deployment process, so without wasting further time, let's jump in.


Prerequisite - Since this is going to be a hands-on tutorial, it is expected that you have a Jenkins account. If you don't have one, just download one from here. If you wish to install on a specific operating system(like CentOS), I have covered installing Jenkins using Docker in this post.


Contents -


  • Introduction

  • Building a Parameterized project.

  • Installing new plugins in Jenkins.

  • Connecting to a remote host using Jenkins with the help of SSH keys

  • Using Environment variables in Jenkins.


Introduction


a. Sign in to your Jenkins Account. The Welcome screen looks like this.

b. We will click on the New Item in the top left corner and start building our first Project. For the moment, choose this to be a freestyle project, and click ok after you have specified the project name.



c. Now, a Dashboard will appear for your new project which might overwhelm you at the start, but don't worry, we will cover all of these step by step. At the moment, all you need is to just look at the Dashboard and get a hang of it. There are five main attributes of any project -


  • Source Code Management - All of the teams today use GIT for storing their code. GIT is a version control system, and we can directly give the path of our GIT repository so that Jenkins can directly pick the code from the Repository URL and build it for us.

  • Build Triggers - You can specify when your build should be triggered.

  • Build Environment - You can execute your script on the remote host using ssh as well.

  • Build - You can choose to build from a shell script or other things.

  • Post-build Actions - You can choose to send an E-mail on the successful completion of your build.



Let's see all of the steps for a build to be successful in detail. Be ready to get your hands dirty!! I'll be covering the main use cases of every step that are used in day-to-day life.


General - Building a Parameterized Project


We are going to build a simple project with parameters. We can add parameters for our project from the General section, by clicking on - This project is parameterized. Instead of hardcoding the parameters in our script, we can use this Jenkins functionality and pass parameters in real-time.

a. As you can see, Jenkins provides us with a lot of options for parameters. Here, I am going to choose String Parameters. You can play with all of them on your own.



b. Note - I have added two String Parameters here. You can add as many by just clicking the Add Parameter and it will again add your choice of Parameters for you.


I have given the variable as First_Name and Last_Name and also assigned a Default value to them(it is not necessary). These variables will take the value that is provided to them at runtime, or otherwise assume default values.

Here, I have added another parameter, which is the choice Parameter. The choice parameter is like a multi-value option, from which you have to choose one. Depending on the value that the user picks up, it will be assigned to the Env variable.


c. Now, we can come directly to the Build section, and here we will be executing a shell script.

We give a simple echo command in our script.




d. Now click on the Save button at the bottom, and we can see our project. The arrow points out to your Build history, and there you can know if the build was successful or not. Let's click on Build with Parameters, as we don't have any build history yet!



e. On clicking Build with Parameters, we get the variables that we have defined. If you want to change these values, change them. And, then click on Build.





f. As you might have guessed, my first project failed(the shell script command was wrong!). We can click on #1 and see the console output to see what the error was.


g. After making the changes, now when I run my build again, it is successful. #3

That's it, now you know how to create a parameterized project in Jenkins. That's a good start!!


Installing Plug-ins in Jenkins


Installing Plugins in Jenkins is quite easy. Let's install an SSH Plugin. You will come to know from where we can install any plugin and where we can see it after installation. Let's start


a. In the Dashboard, click on Manage Jenkins.



b. Choose Manage Plugins


c. In the search bar, choose SSH. It shows us a list of available plugins. We choose the first one which will help us in connecting to our remote machine.

Click on Install without Restart at the bottom



d. Once Jenkins installs the plugins, click on the restart checkbox. Jenkins will restart automatically.

e. Now if you go back to manage Jenkins -manage plugins- Installed and search for SSH, you can see that it was installed successfully.




Adding a Private SSH key of a Host machine in Jenkins


a. Let's see how we can store the credentials of a host machine on Jenkins and be able to connect to it through Jenkins. The majority of times you will be deploying your code on a remote host, so this is an absolutely important point to remember.


b. We will add the credentials of our host machine in Jenkins. Go to Manage Credentials.


c. Click on Manage Credentials and click on Jenkins.



d. Click on Global Credentials.

e. Click on Add Credentials.

f. To connect to a remote host, you would have created a user. I have named my user as remote_user and entered the private ssh key as well.

g. Now, my private key is successfully added. We will check if the connection to our remote host is successful via Jenkins or not.


Integrating a Remote host SSH Servier with Jenkins


Let's now check if the connection is successful or not. I have created a remote host on my Docker that I want to connect with my Jenkins. For connecting any SSH sites, we can do the following in Jenkins -


a. Go to Configure System.


b. Scroll down below to SSH Remote hosts. Give your Hostname, and choose the credentials that you added from Manage Credentials. Then just click on Check connection and if the connection is successful, you will get a successful connection message. Click on Save and your remote host configuration is done!!


c. Let's create another Freestyle project and in the build section, we can select Execute shell script on remote host using ssh.



d. Give a simple command to execute on our SSH site. The output of our file will be stored in the remote host machine. SSH site is automatically populated by Jenkins as it takes it from the remote host configuration you did above.



e. We can see the build was successful in the Console Output.


h. If we login into our remote host container, and go to the /tmp/remote-file path, we can see our file present. It means Jenkins build was successful in connecting with our host machine. So now you can deploy anything on your remote host as well using Jenkins.



Environment Variables in Jenkins


What do you do when a build is successful, and you want to inform the team members by saying that Build No #101, Build Name - XYZ, with the Build URL was successful?


Here Jenkin's in-built(default) environment variables come in play. Some of the most commonly used are -

  • BRANCH_NAME

  • BUILD_NUMBER

  • BUILD_ID

  • JOB_NAME

  • BUILD_URL

Here you can refer to all of the environment variables being used. Let's take an example and run a build using the environment variables.



After the build is successful, if we look at the console output, we get all the details of our build. Pretty Nice!!



Creating our Own Global Variables in Jenkins


Jenkins allows us to define our own global variables as well. Let's see how.


a. Go to Manage Jenkins - Configure System. Scroll down and just above the SSH remote host, you will find environment variables under Global properties. You can click on Add and add as many as you want. I have added a variable Country as you can see with Value= India. Click on Save.



b. After you have added the Global variable, you can configure the same build again and add the new Global variable you have defined. Click on Save and Build the project again.

c. You will now see the value of your variable as well in the console output. Build no, Build URL all change and are present with the new environment variable you have defined. This way, you can successfully define the global variables if you need any specific one.



In this way, you can also add variables in Jenkins that you want. So, in this post we covered Building a Parameterized project, installing new plugins in Jenkins, connecting to a remote host using Jenkins with the help of SSH keys, and using Environment variables in Jenkins.


That was a lot of learning in this post !! I will continue this in another post otherwise the article would become too long. Hope you get to know about the basics of Jenkins and how we can run a build with the help of parameters, shell script, remote host, and global variables. In the upcoming post, we will cover a lot more things. Till then, stay tuned.

 
 
 

Comments


Post: Blog2 Post
bottom of page