Day 23 of 90 Days of DevOps Challenge: Jenkins Freestyle Project for DevOps Engineers

The #90daysofdevops journey continues to impress, and today’s challenge is particularly exciting! We’re diving into the creation of a Jenkins Freestyle Project, a crucial component for automating build, test, and deployment processes in a DevOps pipeline. This task is an excellent opportunity to refine your Jenkins skills and showcase your capabilities as a DevOps engineer.

Ready to dive in and elevate your Jenkins expertise?

Let’s get started! 😍


Understanding CI/CD

Before we jump into Jenkins Freestyle Projects, let’s refresh our understanding of some fundamental concepts:

  • Continuous Integration (CI): CI is a practice where developers frequently commit code changes into a shared repository. This code is then automatically built and tested to identify and fix bugs early. The goal of CI is to ensure that code changes are integrated smoothly, improve software quality, and expedite the release of new features.

  • Continuous Delivery (CD): CD builds on CI by ensuring that the software can be reliably and quickly released to users. It involves running comprehensive tests in a staging environment that mirrors production to catch any issues before deployment. CD automates the release process, making the software deployment more efficient and reducing the risk of errors in production.


What Is a Build Job in Jenkins?

In Jenkins, a build job is a configuration that automates a specific set of tasks in the software development lifecycle. These tasks can include:

  • Fetching Dependencies: Automatically retrieving necessary libraries or packages.

  • Compiling Code: Transforming source code into executable programs.

  • Running Tests: Executing tests to ensure code quality and functionality.

  • Deploying Applications: Publishing the application to different environments.

Jenkins offers several types of build jobs, including:

  • Freestyle Projects: Simple and flexible, allowing various configurations and integrations.

  • Pipelines: Advanced setups using DSL (Domain-Specific Language) scripts for complex workflows.

  • Multi-Configuration Projects: Jobs that run with different configurations to test various scenarios.

  • Multibranch Pipelines: Automatically manage branches in a repository, making it easier to handle multiple feature branches.

  • Organization Folders: Manage projects in a folder structure for better organization and scalability.


Freestyle Projects: A Detailed Overview

A Freestyle Project in Jenkins is a versatile job type that allows you to configure and automate a wide range of tasks for building, testing, and deploying applications. It provides a user-friendly interface to set up various build steps and integrations. Let’s dive into how to create and configure Freestyle Projects with two specific tasks.


Task 01: Creating a Freestyle Project for Docker

  1. Create an Agent for Your App:

    • Ensure you have a Docker container running your application. This setup should be ready for Jenkins to interact with. If you haven’t already deployed your app using Docker, refer to previous tasks to set this up.
  2. Create a New Jenkins Freestyle Project:

    • Navigate to the Jenkins dashboard and click on “New Item.”

    • Enter a name for your project and select “Freestyle project.”

    • Click “OK” to create the project.

  3. Add a Dockerfile:

    • Navigate to the directory where the Jenkins project is running.
      In this case /var/lib/jenkins/workspace/myapp/

    • Add a Dockerfile

    • Example of a Dockerfile

        # Use a base image with Python
        FROM python:3.9-slim
      
        # Set the working directory inside the container
        WORKDIR /app
      
        # Create a simple Python application directly in the Dockerfile
        RUN echo 'print("Hello, World!")' > app.py
      
        # Define the command to run the Python script
        CMD ["python", "app.py"]
      
  4. Configure the Build Steps:

    • Go to the project configuration page and find the “Build” section.

    • Add a build step by selecting “Execute shell” to run shell commands.

    • Build Step 1: Enter the docker build command to create the Docker image for your container.

      For example:

        docker build -t myapp:latest .
      

      This command builds the Docker image using the Dockerfile in the project directory and tags it as myapp:latest.

    • Build Step 2: Add another build step to run the Docker container using the image created. Enter the docker run command:

        docker run -d --name myapp_container myapp:latest
      

      This command starts a new container named myapp_container from the myapp:latest image in detached mode.

    • Save your configuration and build the project. Jenkins will now execute the specified commands and manage your Docker container.


Task 02: Using Docker-Compose in Jenkins

  1. Create a Jenkins Project for Docker-Compose:

    • Follow a similar process to create a new Freestyle Project in Jenkins.
  2. Add a docker-compose.yml file:

    • Navigate to the directory where the Jenkins project is running.
      In this case /var/lib/jenkins/workspace/myapp/

    • Add a docker-compose.yml

    • Example of a docker-compose file

        version: '3.8'
      
        services:
          web:
            image: nginx:alpine
            ports:
              - "80:80"
            volumes:
              - ./html:/usr/share/nginx/html
            depends_on:
              - app
      
          app:
            image: python:3.9-slim
            working_dir: /app
            volumes:
              - ./app:/app
            command: python app.py
            ports:
              - "5000:5000"
      
  3. Add Build Steps for Docker-Compose:

    • In the “Build” section, add a build step to execute shell commands.

    • Step 1: Use the docker-compose up -d command to start multiple containers defined in your docker-compose.yml file. This file should be the one you created in Day 19:

        docker-compose up -d
      

      This command starts all services defined in the docker-compose.yml file in detached mode.

    • Step 2: Add another build step for cleanup. Use the docker-compose down command to stop and remove the containers:

        docker-compose down
      

      This command stops and removes all the containers defined in the docker-compose.yml file.

    • Save the project configuration and trigger a build to see Docker-Compose in action.


Best Practices and Tips

  • Version Control: Keep your Jenkins configuration and scripts under version control to track changes and collaborate with your team.

  • Parameterize Builds: Use parameters in your Freestyle Projects to make your builds more flexible and reusable.

  • Monitor Builds: Regularly check Jenkins logs and build results to ensure everything is running smoothly.

  • Automate Testing: Integrate automated tests into your build process to catch issues early and maintain high-quality code.


Conclusion

Congratulations on completing Day 23’s task! By creating and configuring Jenkins Freestyle Projects, you’ve gained valuable experience in automating build, test, and deployment processes. This hands-on exercise not only enhances your Jenkins skills but also contributes to the efficiency and reliability of your DevOps practices. Keep experimenting, refining your setups, and sharing your progress with the #90daysofdevops community. 🚀

As you continue your DevOps journey, stay curious and open to learning new tools and techniques. Each challenge you tackle brings you one step closer to mastering the art of DevOps. 😃