Jenkins Freestyle Project for DevOps Engineers (Day20)
Hey Everyone!
In our previous discussion, we delve into Jenkins. If you haven’t read it yet, you can check it out [here].
Today, we will see Jenkins in more detail by setting up a freestyle CI/CD pipeline.
What is CI/CD?
It stands for continuous integration and continuous delivery/deployment, a set of practices that help speed up and streamline the software development process. CI/CD is a key part of DevOps methodology, which aims to improve collaboration between development and operations teams. This helps ensure that code is always in a releasable state and that new features and bug fixes reach customers as quickly as possible. It can also help maintain software quality by reducing the chance of errors.
What is a CI/CD Pipeline?
A pipeline is a method that guides software development through the steps of developing, testing, and delivering code, often known as CI/CD. The goal of automating the process is to reduce human error while maintaining a consistent method for releasing software. The pipeline may comprise tools for compiling code, unit testing, code analysis, security, and binary production. For containerized settings, this pipeline would include packaging the code as a container image that could be distributed across a hybrid cloud.
You are getting bored with theory, Right? Let’s move on to hands-on. We will be setting up a freestyle pipeline using Jenkins by creating a new Jenkins Job.
What is Jenkins’s Job?
A Jenkins build job defines the configuration for automating a single operation or phase in the application development process. These responsibilities involve acquiring dependencies, compiling, archiving, or converting code, as well as testing and delivering code across several environments.
Jenkins supports a variety of build jobs, including freestyle projects, pipelines, multi-configuration projects, folders, multibranch pipelines, and organizational folders.
Today we will be working on a freestyle project.
Freestyle project in Jenkins allows you to build, test, and deploy software with a range of settings and configurations.
Creating a Node:
We will start building the pipeline. For that head over to the Jenkins dashboard and create a new node first.
Go to Create an Agent at the dashboard or click on the Build Executor Status at the dashboard to create a new node.
We will be creating a new node here, by clicking on the + New Node, and give it a name.
We have created a node here with the name ToDo-App-Dev.
Now add a Remote root directory of your EC2 instance by running pwd command. This will give you a path, enter that path address in the remote root directory and save. Your Node has been finally created.
Creating a New Job:
Now that we have Jenkins and our node, we will start building the pipeline. For that head over to the Jenkins dashboard and create a new job.
We will be creating a new job and for that click on the + New item, we will select the freestyle project, give it a name i.e. Todo-dev, and press ok.
It will take us to the pipeline where we will be configuring it.
Now in the configuration scroll down to the Build steps and select execute shell and write the following commands.
To build a Docker image from a Dockerfile located at
/home/ubuntu/projects/django-todo/Dockerfile
, you can use the following command:
sudo docker build -t todo-dev -f /home/ubuntu/projects/django-todo/Dockerfile /home/ubuntu/projects/django-todo
To run a Docker container from the
todo-dev
image and map port 8000 of the container to port 8000 of the host, use the following command:
sudo docker run -d -p 8000:8000 todo-dev
after adding these two commands in the execute shell, save it.
Now before running the pipeline, go to your terminal and do a little modification:
Open the /etc/sudoers
file with visudo
:
sudo visudo
Add the following line under root, to grant jenkins
user passwordless sudo access:
jenkins ALL=(ALL) NOPASSWD: ALL
Ensure there are no trailing spaces or special characters after ALL
. Save and exit the editor.
Now build the pipeline, by going to the jenkins dashboard and selecting the pipeline and clicking on the build now.
This should guide you through setting up a Jenkins node, creating a job, configuring it to build and run a Docker container, and granting the necessary permissions for the Jenkins user to execute the required commands. If you encounter any issues or have any questions, feel free to ask!
This was all from today. Thank you for giving it a read.