Jenkins Freestyle Project for DevOps Engineers
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:
Freestyle Projects: These projects provide flexibility in configuring build steps and are suitable for simpler tasks or projects.
Pipelines: Pipelines offer more advanced capabilities, enabling the definition of complex workflows using script-based approaches.
Multi-Configuration Projects: These projects are designed for testing on multiple configurations, such as different operating systems.
Folders: Folders help organize jobs into logical groups, enhancing the management of larger projects.
Multibranch Pipelines: This type is used when managing multiple branches in a source code repository, automating builds for each branch.
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
Launched two instances - One as a Jenkins Master Node and another as an agent.
Install Jenkins on the Master EC2 instance.
Create private and public keys via
ssh-keygen
to connect Jenkins Master and Agent server via SSH.Copy the Public key from the Master Server to the Agent Server in the below file.
cd .ssh/authorized_keys
Log in to your Jenkins dashboard.
Navigate to "Set up an agent".
Click on "New Node" or "New Agent."
Assign a name to the agent, such as "Todo-App-Dev" or "Docker-Agent".
Opt for the "Permanent Agent" option.
Choose "Launch agents via SSH" under "Launch method."
Copy the Master Private key.
Define the agent's root directory for storing files.
Save the agent configuration. Here is the successful console output.
Step 2: Creating a Jenkins Freestyle Project
Access your Jenkins dashboard.
Click "New Item" to initiate a new project.
Give the project a name.
Select "Freestyle project" as the project type.
Confirm by clicking "OK."
Step 3: Configuring Build Steps
On the project's configuration page, proceed to the "Build" section.
Add a new build step by selecting "Add build step."
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
Scroll down and click "Save" to store the project configuration.
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
Log in to the Jenkins dashboard.
Initiate the creation of a new project by selecting "New Item."
Assign a name to your project
Opt for "Freestyle project" as the chosen project type.
Confirm your selection by clicking "OK."
Step 3: Configuring the Build Stages
Within the project's configuration page, navigate to the "Build" section.
Choose "Add build step."
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
Scroll down to "Save" and store the project configuration.
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.
Subscribe to my newsletter
Read articles from Ajay Gite directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by