How to Build a Project in Jenkins.

How to Build a Project in Jenkins.

An Introduction to Jenkins:

Jenkins is a powerful open-source automation tool that enables developers and DevOps teams to build, test, and deploy code efficiently. It supports continuous integration (CI) and continuous delivery (CD) practices, allowing teams to automate various stages of their software development lifecycle.

Build in Jenkins:

In Jenkins, a build refers to the execution of a job. The build process often includes tasks such as fetching code from a repository, running tests, compiling code, and creating artifacts that can be deployed to production or other environments. Each build is tracked and logged, allowing for detailed monitoring and troubleshooting.

Upstream Projects in Jenkins:

Upstream Projects are jobs or projects that serve as triggers for other jobs (called Downstream Projects). It allow you to create a dependency chain. For example, if Project A is an upstream project for Project B, completing a build for Project A can trigger Project B to start automatically.

Downstream Projects in Jenkins:

Downstream Projects are jobs or projects that are triggered automatically after an Upstream Project completes successfully. It allow for the chaining of jobs, where the execution of one job (the upstream) automatically triggers subsequent jobs (the downstream).

Screenshot 2024 10 27 111251

In this guide, I will demonstrate how to set up and run build jobs in Jenkins.

STEP1:Select the instance and Copy the public IP of your instance listed in the details. 

Screenshot 2024 10 26 22343911

STEP2:Enter the browser http://<your-ec2-instance-public-ip>:8080.

STEP3:You see this following page.

Screenshot 2024 10 26 223700

STEP4:On the initial setup page, enter the administrator password.

Screenshot 2024 10 26 224218

STEP5:Select the install suggested plugins.

Screenshot 2024 10 26 224019
Screenshot 2024 10 26 224036

STEP6:Create the first admin user and click the save and continue.

Screenshot 2024 10 26 224338

STEP7:Click save and finish.

Screenshot 2024 10 26 22444611

STEP8:Click start using jenkins.

Screenshot 2024 10 26 224502

STEP9:You will see the jenkins dashboard page and select the create a job.

Screenshot 2024 10 26 22453111

STEP10:Enter the item name , select the Freestyle project and click the ok button.

Screenshot 2024 10 26 22464011

STEP11:Scroll down to the Build steps section, click on “Add build step”, and choose “Execute shell”. 

Screenshot 2024 10 26 22480511 1
Screenshot 2024 10 26 22481411

STEP12:Type the following commands in the shell, and then select Save to apply your changes.

echoHello>> Message
Screenshot 2024 10 26 224901

STEP13:Your first job is now created and visible on the dashboard.

Screenshot 2024 10 26 224940

STEP14:Then next you navigate to EC2 instance and then click on Connect.

Screenshot 2024 10 26 22343911 1

STEP15 : Run the following commands in the terminal.

cd /var/lib/jenkins
ls
cd workspace
ls
Screenshot 2024 10 26 22515211
Screenshot 2024 10 26 225459

STEP16:Don’t close the EC2 terminal and now go back to the Jenkins dashboard.

STEP17:Jenkins 1st job“, click on “Build Now“.  

Screenshot 2024 10 26 22531211

STEP18:After the build is completed, go to the EC2 terminal again and run the following commands .

ls
cd 'Jenkins first job'
ls
cat Message
Screenshot 2024 10 26 225708
Screenshot 2024 10 26 225752

STEP19:Click on “New Item” On the left side panel of the Jenkins dashboard and select the new item.

STEP20:Enter the item name and click the freestyle project.

Screenshot 2024 10 26 225927

STEP21:choose build triggers from the left side panel and click “Build after other projects are built”. 

STEP22:Choose jenkins1st job and click the “Trigger only if build is stable” .

Screenshot 2024 10 26 23022411

STEP22:Scroll down and expand the build steps tab , Select the execute shell.

Screenshot 2024 10 26 230330

STEP23:Type the following command and Click the save button.

echoThis is the current build>> info
Screenshot 2024 10 26 230516

STEP24: Go to dashboard and click on build now for “Jenkins 1st Job”. 

Screenshot 2024 10 26 230652

STEP25:After the build is successful for ‘Jenkins 1st job’ refresh the page, you will see that the Jenkins 2nd job build will be automatically built. 

Screenshot 2024 10 26 23080011

STEP26:Go to EC2 terminal and enter the following commands.

cd /var/lib/jenkins
ls
cd workspace
ls
Screenshot 2024 10 26 230929
cd 'Jenkins second job'
ls
cat info
Screenshot 2024 10 26 231111

You will see that the info file contains the message that we had put in the execute shell. 

CONCLUSION

A “build” represents a sequence of steps or processes defined in a Jenkins job, such as compiling code, running tests, or deploying applications. Jenkins triggers these builds based on predefined conditions, like a code commit or a scheduled time, ensuring that changes are consistently tested and integrated.

Tags: No tags

Add a Comment

Your email address will not be published. Required fields are marked *