Jenkins Playground Part 1

Jenkins Playground Part 1

ยท

4 min read

In the Journey to DevOps, let's continue to learn and do the hands-on part on Jenkins.

So, in this blog, we are going to do a practical demo of a basic Jenkins Freestyle project.

Let's first learn what is a Freestyle type of project and its uses in Jenkins.

Freestyle Project

Jenkins freestyle projects allow users to automate simple jobs, such as running tests, creating and packaging applications, producing reports, or executing commands. Freestyle projects are repeatable and contain both build steps and post-build actions.

Uses of a Freestyle Project

  • Quick Setup: Freestyle projects are easy to set up and configure through the Jenkins web interface, making them suitable for quick and simple CI/CD pipelines.

  • Simple Build and Deployment Tasks: For straightforward build and deployment tasks that don't involve complex dependencies or intricate logic, freestyle projects can be sufficient. They offer a graphical interface to configure common build steps and post-build actions.

  • Single-Step Jobs: Freestyle projects are suitable for jobs that involve a single step or task. For example, running a script, executing a command, or triggering a simple build without the need for a more elaborate pipeline structure.

While freestyle projects are suitable for simple and quick setups, they may not be the best choice for organizations aiming for more advanced, flexible, and version-controlled CI/CD workflows. Scripted or declarative pipelines are often recommended for managing complex build and deployment processes in a more maintainable and scalable manner.

Setting up Jenkins and a Freestyle project

Now we will learn the step-by-step process to run Jenkins and create a basic freestyle project.

Step 1 - If you haven't installed Jenkins, do watch my previous blog on how we can install Jenkins on an AWS EC2 Instance. You can refer to my previous blog from here - Installation of Jenkins.

Step 2 - I have already configured and installed Jenkins on an EC2 Instance using User Data so that when I connect with my instance Jenkins is already installed in it.

So now my Jenkins is installed and configured and it's running on port 8080.

Step 3: Now click on "New Item" to create a Freestyle project. Add a job name "demo-freestyle" and click "Freestyle project". Go to the next step.

Step 4: Add all the basic configuration details such as Description, and Soucre Code Management (Git). We will integrate Github with Jenkins (MAKE SURE GITHUB PLUGIN IS INSTALLED). My index.html file is stored in a GitHub repository and I will provide my GitHub repository URL, and branch name along with the credentials (Username & Password, or you can add a Personal Access Token) of Github to integrate it with Jenkins.

You can use my Github repository URL to create your project - Github Repo URL

Branch Name - Main

Add the Github credentials to connect Jenkins with Github.

Step 5: After this add a Github webhook, which means to build the project again whenever the code is edited in the github repository. You have to add the Jenkins webhook URL in GitHub by navigating to the "Settings" of your repository, you will see the option to add a webhook in the left navigation menu.

Copy the Jenkins URL and paste it in the "Payload-URL" field and add 'github-webhook/' after the URL as shown in the image above. Select the events when you want to trigger the build. It will send the notification to Jenkins that some change has been made and build the project again. For now, select to trigger the webhook when any change is made. Now click on "Add Webhook" to add the webhook. In Jenkins check the "GitHub hook trigger for GITScm polling" to trigger the webhook.

Step 6: Now, In Jenkins click on "Save" to save the job and click on "Build Now" to build the job. You will see on the left side under "Build History" that our first build was successful.

In the workspace our Git repository is successfully linked with Jenkins and our index.html file is there.

Step 7: Now to test the webhook make a small change in the 'index.html' file in GitHub to trigger the webhook and the build will automatically get started.

I have committed some changes in the 'index.html' file to test the Webhook. Now wait for 10 seconds to test the webhook. You will see that the build automatically gets started when I commit a change in the repository.

You will see that the 'index.html' is updated and the build is successful using webhook.

This is the whole process of how we can execute a freestyle project, integrate it with GitHub, and test a webhook.

Step 8: At last after playing with Jenkins and exploring it, terminate the EC2 instance, so that no charges will be incurred.


Conclusion

So now we have created a basic demo of a Freestyle project in Jenkins, integrated it with Github, and tested the webhook to trigger the build automatically.

That's a wrapup for this blog, in the next blog, we will create a basic Declarative Pipeline and learn how to write the Jenkinsfile.

Do subscribe to my hashnode channel for regular updates on my upcoming blogs.

Thank you! Happy Learning! ๐Ÿš€

ย