Day 23 -Jenkins Freestyle Project
Introduction
Welcome to Day 23 of the #90DaysOfDevOps challenge! Today, we'll dive into Jenkins Freestyle Projects and explore how they can be utilized by DevOps engineers for Continuous Integration and Continuous Deployment (CI/CD). In this blog post, we will cover the basics of CI/CD, understand the concept of a build job, and explore the creation of Jenkins Freestyle Projects for Dockerized applications.
What is CI/CD?
Continuous Integration (CI) and Continuous Deployment (CD) are practices that aim to automate the process of software delivery. CI involves the frequent integration of code changes into a shared repository, allowing teams to detect and address errors early in the development cycle. CD extends CI by automatically deploying code changes to production after passing various stages of testing.
What Is a Build Job?
A build job is a critical component of CI/CD pipelines. It involves compiling source code, running tests, and creating executable artifacts. In the context of DevOps, the build job is responsible for transforming source code into a deployable package.
What is Freestyle Projects?
Jenkins Freestyle Projects provide a simple and flexible way to create CI/CD pipelines. They allow developers and DevOps engineers to define build and deployment steps using an intuitive graphical interface.
Task-01: Dockerizing Your Application
Certainly! Let's walk through the step-by-step process of creating a new Jenkins Freestyle Project and setting up a webhook for Jenkins. For this example, I'll use GitHub as the version control system.
Step 1: Create a New Jenkins Freestyle Project
Log in to Jenkins: Open your Jenkins instance in a web browser and log in.
Navigate to Dashboard: Once logged in, you'll land on the Jenkins dashboard.
Create a New Item (Project):
Click on "New Item" on the left sidebar.
Enter a name for your project (e.g., "MyApp_CI_CD") in the "Item name" field.
Select "Freestyle project" as the project type.
Click "OK" to create the project.
Step 2: Configure General Settings
General Tab:
In the project configuration, go to the "General" tab.
Optionally, provide a description for your project.
Step 3: Source Code Management (SCM)
Source Code Management Tab:
Choose your version control system (e.g., Git).
Enter the repository URL.
Specify the branch you want to build (e.g., "main" or "master").
Step 4: Build Triggers
Build Triggers Tab:
- Check the "GitHub hook trigger for GITScm polling" option.
Step 5: Build
Build Tab:
In the "Build" section, click on "Add build step."
Choose the appropriate build step for your project. For example, you can use "Execute shell" for running shell commands.
Enter the build commands you want to execute.
Step 6: Post-Build Actions
Post-Build Actions Tab:
In the "Post-build Actions" section, you can configure actions to be performed after the build.
For now, you can skip this step, or you can add actions like archiving artifacts, sending notifications, etc.
Step 7: Save Configuration
Save Configuration:
- Scroll down and click "Save" to save the Jenkins Freestyle Project configuration.
Setting Up Webhook for Jenkins
Now, let's set up a webhook on GitHub to trigger the Jenkins build whenever changes are pushed to the repository.
Go to GitHub:
- Navigate to your GitHub repository.
Repository Settings:
Click on "Settings" in the repository menu.
Webhooks:
In the left sidebar, select "Webhooks."
Click on "Add webhook."
Configure Webhook:
Enter the Payload URL, which is the Jenkins URL with
/github-webhook/
appended (e.g.,http://your-jenkins-url/github-webhook/
).Set the Content type to "application/json."
Choose "Just the push event" or customize as needed.
Click "Add webhook" to save.
Test the Setup
Make a small change in your repository (e.g., update a file) and push the changes. Check Jenkins to see if it triggers a build automatically.
Builds-11
Application Before Commit
ANSAR-->SHAIK
Commit Change
Build-12
ERROR
Solved Error
Build-13
Application After Commit
That's it! You've successfully created a Jenkins Freestyle Project and set up a webhook for Jenkins to trigger builds on code changes.
Task-02: Orchestrating Containers with Docker Compose
1. Create Jenkins Project for Docker-Compose:
In Jenkins, create a new Freestyle Project as described earlier.
Add a build step:
bashCopy code# Run Docker Compose to start containers docker-compose -f path/to/your/docker-compose.yml up -d
2. Cleanup Step with "docker-compose down":
Add a post-build action:
bashCopy code# Run Docker Compose to stop and remove containers docker-compose -f path/to/your/docker-compose.yml down
Ensure that the Jenkins user has the necessary permissions to interact with Docker and Docker Compose.
Add jenkins to docker group ( sudo usermod -aG docker jenkins)
Change visudo ( jenkins ALL=(ALL) NOPASSWD:ALL )
- Restart jenkins
Conclusion
Jenkins Freestyle Projects provide a user-friendly approach to building and deploying applications, making them a valuable asset in the DevOps toolkit. By completing today's tasks, you've gained hands-on experience in orchestrating Docker containers and automating the deployment process. Stay tuned for more exciting challenges in the #90DaysOfDevOps journey!
Keep coding, automating, and deploying efficiently! ๐
Subscribe to my newsletter
Read articles from ANSAR SHAIK directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
ANSAR SHAIK
ANSAR SHAIK
AWS DevOps Engineer