Jenkins Freestyle Project for DevOps Engineers[Day-23 Task]

Jenkins Freestyle Project for DevOps Engineers[Day-23 Task]

1. What Is CI/CD?

Continuous Integration is a process of integrating code changes from multiple developers in a single project many times. The software is tested immediately after a code commit. With each code commit, code is built and tested. If the test is passed, the build is tested for deployment. If the deployment is successful, the code is pushed to production.

Continuous Delivery prepares deployments for production, allowing development teams to deploy changes easily with the push of a button.

Continuous delivery produces artifacts deployable to production—the next step after continuous integration (CI). It allows the organization to wait before deploying each new release to evaluate the change.

In continuous delivery is that a human must still push the button to deploy the release. Companies often set up a CD process without realizing its full benefits, because manual approval takes time and holds up the release of new features to customers.

Continuous Deployment involves deploying every push to production automatically. There is no human intervention, so the organization doesn’t hold up the pipeline for approval. The only difference between continuous delivery and continuous deployment is the “button” that employees must push to approve the promotion of a build to production.

The pipeline automatically and frequently pushes changes to production without waiting for a human to approve each deployment. The organization must have full trust in the CI/CD process to ensure new releases are of high quality and ready to be pushed to customers with no human intervention.

2. What is a Build Job in Jenkins?

Jobs are the main tasks of the Jenkins build process. A job can be considered as a particular task to achieve a required objective in Jenkins. But we can create and build these jobs to test our application or any project. Jenkins provides various types of build jobs, that a user can create as per requirement.

Types Of Build Jobs:

Let's Do Some Tasks:

Task 1

  • Create a new Jenkins freestyle project for your app.

  • In the "Build" section of the project, add a build step to run the "docker build" command to build the image for the container.

  • Add a second step to run the "docker run" command to start a container using the image created in step 2.

Step 1:

To install Docker on your Jenkins instance, you generally need to follow these steps:

  1. Access Jenkins Server: Log in to your Jenkins server either through the web interface or by accessing the server directly.

  2. Install Docker: The method to install Docker might vary depending on the operating system your Jenkins server is running on. Here are general steps for common operating systems:

    • Ubuntu:

    • CentOS:

      • sudo yum install docker

      • sudo systemctl start docker

      • sudo systemctl enable docker

    • macOS: Install Docker Desktop for Mac from the Docker website.

    • Windows: Install Docker Desktop for Windows from the Docker website.

  3. Add Jenkins User to Docker Group (Linux Only): By default, the Docker daemon runs with root privileges. To allow Jenkins to interact with the Docker daemon without using sudo, you can add the Jenkins user to the docker group.

    • sudo usermod -aG docker jenkins
  4. Restart Jenkins and Docker: After installing Docker and making any necessary changes, restart both Jenkins and Docker services to apply the changes.

    • Ubuntu:

      • sudo systemctl restart jenkins

      • sudo systemctl restart docker

    • CentOS:

      • sudo service jenkins restart

      • sudo systemctl restart docker

  5. Install Docker Plugin in Jenkins (Optional): While you can use Docker commands directly in Jenkins pipelines, there are also plugins available that can simplify Docker integration. You can search and install the "Docker" plugin from the Jenkins Plugin Manager.

For Windows: Here's how you can set up Docker access for the Jenkins user:

  1. Install Docker Desktop: If you haven't already, install Docker Desktop for Windows from the Docker website.

  2. Configure User Permissions:

    • Ensure that the Jenkins service is running under a user account. By default, the Jenkins service might run under the "Local System" account, which might not have access to Docker. You can change the user account by following these steps:

      • Open the Services application (you can search for "Services" in the Start menu).

      • Find the "Jenkins" service, right-click on it, and select "Properties."

      • In the "Log On" tab, choose "This account" and provide the credentials of a user that has access to Docker.

  3. Grant Docker Access: After changing the Jenkins service account, the new user should have the necessary permissions to interact with Docker. You don't need to add the Jenkins user to a "Docker group" as you would on Linux.

  4. Restart Jenkins: After making changes to the Jenkins service account, restart the Jenkins service to apply the changes.

Step 2:

Log in to the Jenkins dashboard.

Today as per the task will build a docker image, so this will be a simple node-based project and here already have the dockerfile present.

Now give a project name and choose the Freestyle project.

Step 3:

Give a project description and also give the project GitHub URL(node-todo-cicd project).

Now scroll down to the same page and give the git repository URL in the source code management section as git here.

Again scroll down to the same page and in the build steps section choose to execute shell and give the shell commands to create a docker image(as per the dockerfile it will create the image), after the creation of the docker image run a container of the same image. Click on save.

Step 4:

Image and container creation was done successfully.

Using docker images and docker ps commands, you can check it from CLI.

To access the app from outside allow port 8000 to the security group in the inbound rule of your instance.

This is the application.

Task 2:

  1. Create a Jenkins project to run the "docker-compose up -d" command to start the multiple containers defined in the compose file.

  2. Set up a cleanup step in the Jenkins project to run the "docker-compose down" command to stop and remove the containers defined in the compose file.1.

Step 1:

Install docker-compose in your Jenkins instance, the command is

apt-get install docker-compose -y

Give a project name and choose a freestyle project.

Step 2:

Give a project description and GitHub project URL(as it is a GitHub project)

[Btw this is the same project, just building with docker-compose file].

Scroll down to the same page and add the Git repository URL.

This is the docker-compose command in a shell script.

This is the compose file to create the image (from dockerfile) and run the container.

Step 3:

Run the Jenkins job to automate your build process with docker and GitHub integration.

Image and container creation did.

This is the application running successfully.

Now as a 2nd part, you can execute the docker-compose down command in the execute shell script, the same as the above steps. It will bring down the containers.

Thank you for reading :)