Jenkins for CI/CD

The entire process of creating software and delivering it to customers has evolved. It isn’t just about development, testing, and delivery. Delivering a reliable and robust software while following an agile process is difficult. That is why DevOps has become so popular. This practice unifies the entire process, ensuring that all phases of coding, building, testing, releasing, configuring, and monitoring the product are handled correctly and with the utmost integrity.

The question now arises. Is there a tool that can keep all of these processes running smoothly? Because it is impractical to manually monitor all phases in an agile environment with time constraints.

The answer is Yes. There is so many tools that plays the role for CI/CD. And Jenkins is one of the most popular opensource tool for continuous integration and continuous deployment/delivery.

Jenkins

Jenkins is a server for automating tasks that is available for free and open source. It facilitates continuous integration and continuous delivery by automating the parts of software development related to building, testing, and deploying.

What is CI/CD

The combined practices of continuous integration and either continuous delivery or continuous deployment are referred to as CI/CD or CICD. By enforcing automation in the building, testing, and deployment of applications, CI/CD bridges the gap between development and operations activities and teams.

Get started with Jenkins

Enough with the definitions. Let’s get started with Jenkins. The following topics I am gonna cover in this post.

  • Download Jenkins server and run
  • Setup Jenkins for the first time
  • Create your first job with Jenkins
  • Scheduling a job
  • Chaining jobs
  • Jenkins plugins
  • Jenkins server running from docker

Download and Installation – Jenkins

Visit the following link to download Jenkins war file. I am assuming you are using windows OS. If you are using Linux or any other OS just follow the procedures from the link. I am describing the process considering that you are using windows OS – https://www.jenkins.io/download/

You can directly download the installer or you can download the war file. I am going with the war file. You need to have java installed on your machine first.

Now go the directory of that war file and run the following command in the terminal: java -jar jenkins.war. Jenkins server will be started after few seconds.
You can now use Jenkins by visiting the following address – visit http://localhost:8080 though some initial setup is required. Wait for the page to appear and continue with the post installation setup.

In the beginning Jenkins will ask for initialAdminPassword. Monitor the terminal, a new file will be created in the following location – C:\Users\****\.jenkins\secrets. Open that file and use that passwords initially. All these steps are stated in Jenkins official site that’s why I am not giving that much details in this section rather I am giving the official link – https://www.jenkins.io/doc/book/installing/war-file/

Create your first job with Jenkins

After completing all the initial steps you will be redirected to the login screen. Log in as a user you just created. Now do the following steps to create your first job which is to print a line in the terminal-

  • Click on “New Item”
  • Give a name of your job, Ex: TestProject
  • Select “Freestyle Project”
  • Click “Ok”
  • Now go to that job and go to the “Build” section
  • Now click “Add a build step” and select “Execute Windows batch command”
  • An editor will appear to write a windows command, write down your terminal command in that box and click “Save”
  • Now from the dashboard again go to that job and click “Build now” from the left panel
  • From left panel click on that build number, you will see the terminal execution commands from Jenkins

So you have just created your first job in Jenkins. This job will print a line in the terminal.

Now let’s make some other job. Let’s say the job is to create some text file . So following the same procedure just write down your command and run the job. The command to execute – type nul > %BUILD_NUMBER%.txt. (Here BUILD_NUMBER is an environment variable of Jenkins which passes the current build number) You can also scheduled this job to a specific time. Periodically the job will get executed by Jenkins. To do so just check the Build periodically box and give the time.

To see the effects run the job and go to the workspace directory of your job. in my case it is – C:\Users\****\.jenkins\workspace\testProject

I’m sure you’ve figured out how Jenkins will assist us with CI/CD. If I were to summarize the basic steps or jobs that Jenkins must perform for CI/CD, they would be:

  • Cloning projects from repository
  • Building the project
  • Deploy the application
  • Run tests
  • Publish report etc.

These are the fundamental steps in CI/CD. A variety of other steps can be added as needed. However, this is the primary goal of a CI/CD tool. To perform some tasks related to the entire development and distribution process following DevOps practices.

Job chaining

In Jenkins, job chaining is the process of automatically starting other jobs after a job has completed. This method allows you to create multi-step processes.

Let’s assume you want to do some more tasks performed along with creating file on scheduled basis. But the other tasks have to be done after the first job which is creating that text file. How you can do so. You can chain all your required jobs together to execute one after another. Let’s assume your second job is to copy that file to a location when the file creation job is done. Yes of course you can do so in the same job. But for the chaining concept I am considering both of the task as separate job. So what can you do. Just create another job with copy command for windows and add that job as downstream job of the first job. So what will happen. After finishing the first job Jenkins will trigger the second job which is included as downstream job under the first one. So you have made a chain of jobs.

Jenkins Plugin

Jenkins has a wide range of plugins to choose from. Go to Manage Jenkins > Manage Plugins to install a plug-in. You can see which plugins are already installed and which are available from the plugin manager. Any plugin can be installed and used according to your requirements. By default only the basic plugins are installed.

Jenkins with docker

With docker we can easily skip all the download and installation process of Jenkins very easily. In the docker hub there is an official Jenkins image available. If you are familiar with docker you can easily run the following command to start your Jenkins container and start using it. The docker command is – docker run -p 8080:8080 -p 50000:50000 -v /your/home:/var/jenkins_home jenkins

You can visit the following links for more info –

https://www.jenkins.io/doc/book/installing/docker/

https://hub.docker.com/_/jenkins

That concludes this post. I was only attempting to convey the most basic concept of Jenkins. I hope you found this article useful.

Leave a comment

Create a website or blog at WordPress.com

Up ↑