Day 23: Jenkins Freestyle Project for DevOps Engineers🚀

Day 23: Jenkins Freestyle Project for DevOps Engineers🚀

Day#23 Of 90 Days Of Devops Challenge

⚡Introduction

In the preceding blog post from Day 22, our grasp extended to encompass Jenkins, the concepts of CI/CD, and the steps involved in Jenkins installation.

Let's start by understanding what Continuous Integration (CI) and Continuous Delivery (CD) are.

The CI is like teamwork for developers' code, where it's combined and checked automatically.

The CD is about smoothly giving those changes to customers without errors. Both help make better software faster.

Now, imagine a "Build Job" as a set of instructions for making software. Jenkins helps with different types of these jobs, like freestyle projects, which are flexible and powerful.

So, a Jenkins Freestyle Project is like a toolkit. You can use it to build, test, and put software out there in different ways. Today, we'll get into the nitty-gritty of what you can do with it. Ready to dive in? So let's start to learn these in detail🛠️

🔄What is CI/CD?

CI or Continuous Integration, is the process of automating the merging of code changes from various developers into a unified codebase.

In software development, developers frequently contribute their work to a central code repository, such as GitHub or Stash.

Automated tools then build the new code and conduct necessary tasks like code reviews during integration.

Continuous Integration aims to swiftly detect and resolve bugs, simplify code integration within developer teams, enhance software quality, and expedite the release of new features.

CD or Continuous Delivery, comes after Continuous Integration, making sure that changes are released to customers quickly and without errors.

This includes testing in an environment similar to the actual production setup (staging area) to catch any problems before the final release.

Automation is crucial here, ensuring the product is always prepared for release. This method enables smooth application deployment whenever needed, with a strong focus on efficiency and accuracy.

🏗️What Is a Build Job?

A Jenkins build job encompasses a configuration that automates a specific task or step within the application building process. These tasks range from gathering necessary dependencies and compiling code to archiving artifacts, code transformation, testing, and deploying code across various environments.

Jenkins offers a diverse array of build job types to cater to different needs:

  1. Freestyle Projects: These projects provide flexibility in configuring build steps and are suitable for simpler tasks or projects.

  2. Pipelines: Pipelines offer more advanced capabilities, enabling the definition of complex workflows using script-based approaches.

  3. Multi-Configuration Projects: These projects are designed for testing on multiple configurations, such as different operating systems.

  4. Folders: Folders help organize jobs into logical groups, enhancing the management of larger projects.

  5. Multibranch Pipelines: This type is used when managing multiple branches in a source code repository, automating builds for each branch.

  6. Organization Folders: Ideal for overseeing numerous projects across diverse repositories within an organization.

In essence, Jenkins build jobs serve as the backbone of automation in software development. They empower developers and teams to automate repetitive tasks, streamline code building and testing, and ensure consistent and reliable outcomes. By leveraging various build job types based on project complexity and requirements, development teams can optimize workflows, enhance efficiency, and maintain high software quality throughout the development lifecycle.

📝What is Freestyle Project?

A Freestyle Project within Jenkins represents a project category that empowers users to construct, test, and deploy software by utilizing a diverse range of configuration options. This project type offers an open canvas for crafting workflows tailored to specific needs, accommodating various tasks and processes involved in software development.

For example, within a Freestyle Project in Jenkins, you have the capability to:

  1. Compile Code: Transform source code into executable files or libraries.

  2. Execute Tests: Run different types of tests to verify the code's functionality and quality.

  3. Archive Artifacts: Preserve important files and outputs generated during the build process for future reference.

  4. Manage Dependencies: Integrate external resources or libraries required for the software's proper functioning.

  5. Configure Environments: Customize runtime environments or variables for the build and testing phases.

  6. Automate Deployment: Streamline the deployment of software to various environments, such as development, testing, or production.

  7. Define Triggers: Establish conditions that initiate the build process, like code commits or scheduled intervals.

  8. Integrate with External Tools: Collaborate with version control systems, continuous integration tools, and other external services.

The versatility of Freestyle Projects lies in their adaptability and comprehensiveness. Developers can tailor workflows, integrate diverse tools, and orchestrate various tasks to precisely match the demands of their software development process. Whether it's compiling code, testing, or deployment, a Freestyle Project in Jenkins enables the creation of a personalized pipeline that perfectly aligns with the project's unique requirements.

So let's start the tasks that you can accomplish while engaged in a freestyle project within Jenkins.

♻Task 01: Creating a Jenkins Freestyle Project with Docker Integration

Step 1: Setting Up a Docker Agent

Ensure Docker is installed on your Jenkins server before proceeding.

  1. Log in to your Jenkins dashboard.

  2. Navigate to "Manage Jenkins" in the left sidebar.

  3. Select "Manage Nodes and Clouds."

  4. Click on "New Node" or "New Agent."

  5. Assign a name to the agent, such as "Docker-Agent."

  6. Opt for the "Permanent Agent" option.

  7. Choose "Launch agents via SSH" under "Launch method."

  8. Fill in the SSH credentials and host information.

  9. Define the agent's root directory for storing files.

  10. Save the agent configuration.

Step 2: Creating a Jenkins Freestyle Project

  1. Access your Jenkins dashboard.

  2. Click "New Item" to initiate a new project.

  3. Give the project a name.

  4. Select "Freestyle project" as the project type.

  5. Confirm by clicking "OK."

Step 3: Configuring Build Steps

  1. On the project's configuration page, proceed to the "Build" section.

  2. Add a new build step by selecting "Add build step."

  3. Opt for "Execute shell" to add a shell script build step.

Step 4: Implementing the "docker build" Command

Within the shell script build step, include the following commands:

#!/bin/bash

# Navigate to your app's directory
cd /path/to/your/app

# Build the Docker image
docker build -t your-app-image .

Replace /path/to/your/app with your app's actual directory path and your-app-image with the preferred Docker image name.

Step 5: Executing the "docker run" Command

  1. Choose "Add build step" once more.

  2. Select "Execute shell" to add another shell script build step.

In the shell script build step, insert these commands:

#!/bin/bash

# Launch the Docker container
docker run -d -p 8000:8000 your-app-image

Substitute your-app-image with the same image name used earlier.

Step 6: Saving and Triggering the Project

  1. Scroll down and click "Save" to store the project configuration.

  2. Click "Build Now" to initiate the project's build process.

Jenkins will execute the configured build steps. It will first create the Docker image using the "docker build" command and then initiate a Docker container via the "docker run" command. The container will expose port 8000 from the app to port 8080 on the host.

💻Task-02: Jenkins Project for Docker Compose Management

In this task, your objective is to set up a Jenkins project that effectively utilizes Docker Compose to manage the deployment and cleanup of multiple containers as defined in a compose file.

Step 1: Preparing Your Jenkins Environment

Ensure that Docker and Docker Compose are properly installed on your Jenkins server before proceeding.

Step 2: Creating a Jenkins Freestyle Task

  1. Log in to the Jenkins dashboard.

  2. Initiate the creation of a new project by selecting "New Item."

  3. Assign a name to your project

  4. Opt for "Freestyle project" as the chosen project type.

  5. Confirm your selection by clicking "OK."

Step 3: Configuring the Build Stages

  1. Within the project's configuration page, navigate to the "Build" section.

  2. Choose "Add build step."

  3. Opt for "Execute shell" to incorporate a shell script build stage.

Step 4: Executing the "docker-compose up -d" Command

Inside the shell script build stage, integrate the following commands:

#!/bin/bash

# Navigate to the directory containing your docker-compose.yml file
cd /path/to/your/compose/directory

# Initiate the containers defined in the compose file
docker-compose up -d

Replace /path/to/your/compose/directory with the specific path to your directory containing the docker-compose.yml file.

Step 5: Introducing a Cleanup Stage

  1. Choose "Add build step" once more.

  2. Opt for "Execute shell" to introduce another shell script build stage.

Within this shell script build stage, add the following commands:

#!/bin/bash

# Navigate to the directory containing your docker-compose.yml file
cd /path/to/your/compose/directory

# Halt and remove the containers defined in the compose file
docker-compose down

Again, replace /path/to/your/compose/directory with the appropriate path.

Step 6: Saving and Initiating the Project

  1. Scroll down to "Save" and store the project configuration.

  2. Initiate the build process by clicking "Build Now."

Jenkins will proceed to execute the configured build stages. It will employ the "docker-compose up -d" command to activate containers as specified in the docker-compose.yml file. Subsequently, it will perform cleanup by discontinuing and removing those containers, all facilitated through the "docker-compose down" command.

👏Conclusion

In summary, our journey through these tasks in the DevOps and Jenkins domains has showcased the significance of automation and integration in software development.

From Jenkins Freestyle Projects to Docker integration, we've explored the efficiency and control these tools offer. Continuous Integration and Continuous Delivery principles have demonstrated the benefits of quick bug detection and seamless software releases.

Jenkins' versatile build job types, like freestyle projects and pipelines, provide tailored automation options. Docker's containerization simplifies deployment and management, further optimized through Docker Compose.

By marrying Jenkins, automation, and Docker, we empower DevOps engineers to drive innovation, accelerate workflows, and deliver top-tier software efficiently. This integration forms a solid foundation for agile, high-quality development practices in an ever-evolving tech landscape.

Thank you for taking the time to read this blog. I hope you found the information helpful and insightful. So please keep yourself updated with my latest insights and articles on DevOps 🚀 by following me on :

Hashnode: vishaltoyou.hashcode.dev

LinkedIn: linkedin.com/in/vishalphadnis

So, Stay in the loop and stay ahead in the world of DevOps!

Happy learning 🚀😊🐧📦🔧🛠️💻💼🔍📈🚀🌟📊📚