Jenkins Freestyle Project for DevOps Engineers

Ajay GiteAjay Gite
5 min read

What is CI/CD?

CI/CD stands for Continuous Integration and Continuous Delivery (or Deployment). Continuous Integration is the practice of frequently merging code changes into a shared repository. It involves automating the build, unit testing, and code quality checks to detect issues early. Continuous Delivery focuses on automating the deployment process to make software releases ready for production at any time.

What Is a Build Job?

A Jenkins build job contains the configuration for automating a specific task or step in the application building process. These tasks include gathering dependencies, compiling, archiving, or transforming code, and testing and deploying code in different 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 Projects ?? ๐Ÿค”

A Freestyle Project in Jenkins allows you to create a job without adhering to a strict pipeline structure. It provides a graphical user interface (GUI) where you can configure various build steps, triggers, post-build actions, and more. This approach is especially useful for projects that don't follow a standard build and deployment process or for teams that prefer a more manual and ad-hoc approach to setting up their automation tasks.

Task 01: Creating a Jenkins Freestyle Project with Docker Integration

  1. Launched two instances - One as a Jenkins Master Node and another as an agent.

  2. Install Jenkins on the Master EC2 instance.

  3. Create private and public keys via ssh-keygen to connect Jenkins Master and Agent server via SSH.

  4. Copy the Public key from the Master Server to the Agent Server in the below file.

    cd .ssh/authorized_keys

  5. Log in to your Jenkins dashboard.

  6. Navigate to "Set up an agent".

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

  8. Assign a name to the agent, such as "Todo-App-Dev" or "Docker-Agent".

  9. Opt for the "Permanent Agent" option.

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

  11. Copy the Master Private key.

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

  13. Save the agent configuration. Here is the successful console output.

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:

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.

Ensure 8000 is open on Agent server.

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:

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.

Done with both the Tasks.

0
Subscribe to my newsletter

Read articles from Ajay Gite directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ajay Gite
Ajay Gite