Getting Started with Jenkins (Day19)

Araiz bin Saqib
6 min readJul 13, 2024

--

Hey Everyone!

In our previous discussion, we made Docker Cheat Sheet. Today, we will delve into Jenkins.

What is Jenkins?

Jenkins is a tool that is used for automation. It is mainly an open-source server that allows all the developers to build, test and deploy software. It is written in Java and runs on java only. By using Jenkins we can make a continuous integration of projects(jobs) or end-to-endpoint automation

Why Organizations use Jenkins?

Jenkins facilitates the automation of several stages of the software development lifecycle, including application development, testing, and deployment. Operating within servlet containers like Apache Tomcat, the technology is server-based. Continuous delivery (CD) and integration (CI) pipelines can be created and managed with Jenkins. The development, testing, and deployment of software applications are automated using CI/CD pipelines. You will be able to release software more regularly and with fewer problems if you do this.

  1. jenkins is flexible.
  2. You can add the n no.of plugins you want to add to the jenkins.
  3. You can automate the processes of CI/CD pipelines of all the projects.

What is Jenkins CI/CD Pipeline?

Jenkins CI/CD stands for Continuous Integration / Continuous Deployment first let us try to understand what is a pipeline. In computing, a pipeline is a set of stages or processes linked together to form a processing system. Each stage in the pipeline takes an input, processes it in accordance with a set of rules, and then sends the outputs to the stage that follows. Frequently, the pipeline’s overall output is its final step’s output. like the procedures outlined below

  1. Test code
  2. Build Application
  3. Push Repository
  4. Deploy to Server

All the steps mentioned above will perform in sequence one after the other if any step/stage get failed it will not move forward to another step/stage until the previous step got a success

What is Jenkins Continuous Integration (CI)?

Jenkins Continuous integration means whenever new code is committed to remote repositories like GitHub, GitLab, etc. Continuous Integration (CI) will continuously build, tested, and merged into a shared repository.

Benefits of Continuous Integration (CI)

  • We can maintain the reports of the projects
  • Deployments can be made within the given time
  • Bugs can be found quickly.

What is Jenkins Continuous Deployment/Delivery (CD)?

Continuous Deployment: Continuous Deployment means automating the further stages of the pipeline automatically or manually deploying the application/code to different environments like Dev, Test, and Production. Automating the build is the main component of Continuous Integration and Continuous Deployment.

Continuous Delivery: Each and every build that passed all automated tests and was able to be fully automated and delivered into production only required one click of human intervention is called Continuous Delivery.

What Is Jenkins Freestyle Project?

Jenkins freestyle project is an configuration and options based project which will allows you to build, test, and deploy the application with automation by selecting the required configuration based on the requirement following are the some of the available in the freestyle project.

  1. Building and testing code
  2. Packaging applications
  3. Deploying applications to production servers
  4. Running reports

What is Jenkins Pipeline?

DevOps professionals mostly work with pipelines because pipelines can automate the processes like building, testing, and deploying the application. Doing manually by UI takes lots of time and effort which will affect productivity. With the help of Continuous Integration / Continuous Deployment (CI/CD) Pipeline scripts we can automate the whole process which will increase productivity and save lots of time for the organization and can deliver quality applications to the end users.To know more about how to build the CI/CD pipeline please refer to the How to Make a CI-CD Pipeline in Jenkins?.

Setup and Install Jenkins on an AWS EC2 Instance:

Update the package list:

sudo apt update

This will update the package list on your AWS Ubuntu (free tier) machine.

Install Java: Before installing the Jenkins server, we need to install Java on our instance, as Jenkins won’t run without it. Jenkins is implemented in Java, so you need the Java setup on the instance.

The command to install Java:

sudo apt install openjdk-8-jdk -y

openjdk-8 is the version of Java we want to download.

Install Jenkins: Now we are ready to install Jenkins. Visit the site below to install Jenkins: https://pkg.jenkins.io/

Since Ubuntu is Debian-based, we’ll opt for the third option, Debian-stable. It will show some commands; run them on your terminal/instance.

run these commands to install jenkins

Start and Enable Jenkins:

Run these commands to install Jenkins. To start the Jenkins server:

sudo systemctl start jenkins

To enable it:

sudo systemctl enable jenkins

To check if it is running or not:

sudo systemctl status jenkins

If it shows the status as active (running), you are good to go. Now go to you AWS EC2 instance and open then Instance ID, copy your (Public IPv4 address) URL and paste it on a web browser and at the end of that URL use :8080.

We have successfully set up the Jenkins server inside our AWS EC2 instance. Jenkins by default runs on port 8080. If it’s not running on 8080, add port 8080 in the security group of the instance inside the inbound rules.

Access Jenkins:

After Jenkins runs on the web, it will ask for a password and show a path. Copy that path and paste it with the following command in your instance terminal

sudo cat <the path>

Now proceed with the Jenkins setup. After creating your account, you have successfully installed and run Jenkins.

Now a little twist, we can install Jenkins using Docker(Automation), avoiding all the above steps with just a single command. Before that only one Jenkins can run on a system at a time so, we’ll stop jenkins first by:

sudo systemctl stop jenkins

For reference you can watch this video1 or video2.

Create a freestyle pipeline to print “Hello World!!

Step 1: Access Jenkins Dashboard

Open Jenkins Dashboard:

  • Open your web browser and navigate to http://<your-ec2-instance-ip>:8080 to access the Jenkins dashboard.

Step 2: Create a New Job

Click on New Item:

  • On the Jenkins dashboard, click on New Item on the left-hand side menu.

Enter Job Name:

  • Enter a name for your job, for example, HelloWorldJob.

Select Freestyle project:

  • Select the Freestyle project option.

Click OK:

  • Click the OK button to proceed to the job configuration page.

Step 3: Configure the Job

Job Configuration Page:

  • You will be taken to the job configuration page.

Scroll to the Build Section:

  • Scroll down to the Build section of the configuration page.

Add Build Step:

  • Click on Add build step.

Select Build Step Type:

  • Choose Execute shell for Linux/macOS or Execute Windows batch command for Windows.

Step 4: Add the “Hello World” Script

Enter Shell Command (Linux/macOS):

  • If you selected Execute shell, enter the following command in the command box:
sinceecho "Hello World!!"

Enter Batch Command (Windows):

  • If you selected Execute Windows batch command, enter the following command:
echo Hello World!!

Since we are running Jenkins on a Linux-based AWS EC2 instance, you should use the Execute shell build step

Step 5: Save and Run the Job

Save the Job Configuration:

  • Scroll down to the bottom of the page and click the Save button.

Run the Job:

  • On the job page, click the Build Now link on the left-hand side to trigger a build.

Step 6: View the Output

Access Build History:

  • Once the build is complete, click on the build number (e.g., #1) in the Build History section on the left side of the job page.

View Console Output:

  • Click on Console Output to view the result of the build.
  • You should see “Hello World!!” printed in the console output.

--

--