#90DaysOfDevOps Challenge - Day 23 - Jenkins Freestyle Project for DevOps Engineers

#90DaysOfDevOps Challenge - Day 23 - Jenkins Freestyle Project for DevOps Engineers

Welcome to Day 23 of the #90DaysOfDevOps challenge. Today, we will dive into Jenkins freestyle projects and explore how they can be utilized by DevOps engineers to automate CI/CD processes. We will also discuss the concepts of CI/CD, build jobs, and freestyle projects. So, let's get started!

Understanding CI/CD

CI/CD stands for Continuous Integration and Continuous Delivery/Deployment. It is a set of practices that aim to automate and streamline the software development lifecycle. CI focuses on merging code changes frequently and automating build and test processes to catch issues early. CD extends this further by automating the delivery or deployment of applications to various environments.

Exploring Build Jobs

In Jenkins, a build job represents a single execution of a build process. It includes tasks such as compiling code, running tests, and packaging the application. Build jobs play a vital role in CI/CD pipelines as they ensure that each code change is validated, tested, and built consistently.

Understanding Freestyle Projects

Freestyle projects are a flexible and customizable project type in Jenkins. They provide a simple and intuitive way to configure and automate complex build processes. With freestyle projects, you have the freedom to define your build steps, configure triggers, and set up post-build actions according to your project's requirements.

Now, let's proceed with the resolution of the tasks for today.

Task 1: Building and Running a Docker Container

  1. Ensure the Jenkins user is added to the Docker Group to be able to run docker commands via Jenkins:

  2. Create a new Jenkins freestyle project for your app:

    • In the Jenkins dashboard, click on "New Item" to create a new project.

    • Enter a name for your project and select "Freestyle project."

    • Click "OK" to proceed.

  3. Configure the build steps:

    • In the Source Code Management section, I will use my GitHub Repository URL to access the Dockerfile

    • In the project configuration page, scroll down to the "Build" section.

    • Click on "Add build step" and select "Execute shell"

        cd /var/lib/jenkins/workspace/90daysofdevops-day23/2023/day23/tasks
        docker build . -t dockerapp:latest
        echo "Image created"
  4. Add a step to run the container:

    • Click on "Add build step" again and select "Execute shell."

        cd /var/lib/jenkins/workspace/90daysofdevops-day23/2023/day23/tasks
        docker run -d -p 3000:3000 dockerapp:latest
        echo "Container is created and running"

  1. Apply and save your project configuration.

  2. Click on "Build Now" to start the build process and wait until the docker image is created and running on a docker container accessible from port 3000

  3. Verify the docker container is running and access the app

Task 2: Running Docker Compose with Jenkins

  1. Create a Jenkins project for Docker Compose:

    • Create a new freestyle project in Jenkins, following the steps mentioned earlier.
  2. Configure the build steps:

    • Scroll down to the "Build" section of the project configuration page.

    • Click on "Add build step" and select "Execute shell."

    • Enter the command "docker-compose up -d" to start the multiple containers defined in your Docker Compose file.

    • Click on "Build Now" to start the build process

  3. Set up the cleanup step:

    • Scroll down to the "Post-build Actions" section.

    • Click on "Add post-build action" and select "Execute shell."

    • Enter the command "docker-compose down" to stop and remove the containers defined in your Docker Compose file.

    • Click on "Build Now" to start the build process

Congratulations! You have successfully created Jenkins freestyle projects to automate the build and deployment of your application using Docker and Docker Compose.

Jenkins, with its flexibility and wide range of integrations, provides DevOps engineers with a powerful platform to automate CI/CD processes. By leveraging the capabilities of freestyle projects, you can customize your build steps, configure triggers, and automate deployment tasks to achieve efficient and reliable software delivery.

Stay tuned for tomorrow's challenge, where we will continue exploring Jenkins with a new CI/CD Project.

Did you find this article valuable?

Support Esteban Moreno by becoming a sponsor. Any amount is appreciated!