Day 23: Step-by-Step Jenkins Freestyle Project Setup for DevOps Engineers
Welcome to the World of CI/CD!
Imagine you're building an amazing app, and every time you make a change, you want it to automatically build, test, and deploy without lifting a finger. Sounds like magic, right? Well, it’s not! This process is known as CI/CD (Continuous Integration/Continuous Deployment), and it’s the backbone of modern software development. 🌟
But how does it work? How can we automate all these repetitive tasks so we can focus on the fun parts of coding? 🤔
In today’s blog, we’ll dive into two key tools that help make this automation process seamless: Jenkins and Docker. If you’ve ever wondered what the heck Build Jobs or Freestyle Projects in Jenkins are, or if you’ve been curious about running containers with Docker, you’re in the right place!
By the end of this blog, you’ll not only understand what CI/CD is and how Jenkins works, but you’ll also walk away with some hands-on experience setting up Jenkins jobs to build and deploy your app—automatically! 💻✨
So, grab your favorite drink, and let’s dive into the world of automation and Docker containers! 🧑💻
Prerequisites
Before you jump into the tasks, make sure you have:
Docker installed on your machine (and some basic understanding of Docker commands like
docker build
anddocker run
).Jenkins installed and running on your system.
Docker Compose: You’ll need docker-compose to manage multi-container Docker applications.
Basic Knowledge of Docker & Jenkins: Familiarity with Docker commands and Jenkins basics will help you breeze through these tasks.
Once you have these in place, we’re all set to build, run, and automate like never before! 🔥
What is CI/CD? 🤔
CI/CD stands for Continuous Integration and Continuous Deployment/Delivery. It’s like a magical pipeline that automates your software development process 🚀. Here’s how it works:
Continuous Integration (CI):
Imagine developers working on the same project. Instead of waiting days or weeks to combine their code (and fix all the messy conflicts 😵💫), CI ensures that every code change is tested and integrated into the main project immediately after submission.Continuous Deployment/Delivery (CD):
Once the code is integrated, CD ensures it’s packaged, tested, and delivered to the production environment automatically. No manual approvals, no delays—it’s like setting your app free into the world! 🌍
What is a Build Job in Jenkins? 🛠️
A Build Job in Jenkins is like asking Jenkins to do some work for you. This "work" can be:
Compiling your code.
Running tests.
Building Docker images.
Deploying your app.
Think of Jenkins as your super-smart assistant 🤖. You define tasks (jobs), and it executes them reliably every time.
What is a Freestyle Project in Jenkins? 🎯
A Freestyle Project in Jenkins is the simplest type of job you can create. It’s flexible and customizable. You can configure it to:
Run shell commands.
Execute build scripts.
Use plugins to integrate with tools like Docker or GitHub.
In short, it's your go-to project for setting up straightforward automation tasks.
Now that you understand the basics, let’s roll up our sleeves and implement CI/CD pipelines using Docker and Jenkins! 🐳
TASKS
Task 1: Build and Run Your App Using Docker in Jenkins
Create an agent for your app (which you deployed using Docker in a previous task).
Create a new Jenkins freestyle project for your app.
In the "Build" section of the project, add a build step to run the
docker build
command to build the image for the container.Add a second step to run the
docker run
command to start a container using the image created in the previous step.
Step 1: Create an Agent for Your App
Open Jenkins and navigate to Manage Jenkins > Manage Nodes and Clouds.
Add a new agent (a machine where Jenkins will run your jobs). Connect it to your app's Docker environment.
Step 2: Create a New Freestyle Project
Go to Jenkins Dashboard and click New Item.
Choose Freestyle Project, and give it a name (e.g., DockerAppBuild).
Step 3: Add Docker Build and Run Steps
In the Build Section, click Add Build Step > Execute Shell.
Add the Docker build command:
docker build -t <your-dockerhub-username>/my-web-app:1.0 .
Add another build step to run the Docker container:
docker run -d -p 8080:80 <your-dockerhub-username>/my-web-app:1.0
This starts your app on the port
8080
.
Test It Out!
Save the project and click Build Now.
Jenkins will:
Build your Docker image 🛠️.
Run the container in seconds 🐳.
Task 2: Manage Multiple Containers with Docker Compose
Create a Jenkins project to run the
docker-compose up -d
command to start multiple containers defined in the compose file (Hint: use the application and database docker-compose file from Day 19).Set up a cleanup step in the Jenkins project to run the
docker-compose down
command to stop and remove the containers defined in the compose file.
Let’s scale things up by running multiple services (like a web app and database) together.
Step 1: Create a New Jenkins Project
- Again, create a Freestyle Project (e.g., “Docker Compose Project” or “MultiContainerApp”).
Step 2: Add Docker-Compose Commands
In the Build Section, add a shell step to start your containers:
docker-compose up -d
This will spin up all services (e.g., app + database) defined in your
docker-compose.yml
.Add another step for cleanup after the job is done:
docker-compose down
Now, Jenkins will start your multi-container setup and clean up afterward.🧹
Step 3: Use the Hint!
Use the Docker Compose file from the previous task that includes both your application and database services.
Example
docker-compose.yml
:sudversion: '3' services: web: image: nginx:latest ports: - "8080:80" # Expose port 8080 on your machine to access the web service environment: ENV: production volumes: - .:/app db: image: mysql:latest environment: MYSQL_ROOT_PASSWORD: mypassword123 # Set environment variables
And that's a wrap for today's CI/CD adventure! From building your app’s Docker image in Jenkins to running multiple containers with Docker Compose, you've just leveled up your automation skills! 💪
By now, you should have a solid understanding of how Jenkins can streamline your DevOps pipeline with a simple freestyle project and Docker commands. Whether you're building single containers or scaling up with Compose, Jenkins makes it all seamless and efficient.
Don't forget, the journey doesn’t stop here—there's always more to learn, optimize, and automate!
✨ Bonus Tip:
Add notifications to your Jenkins jobs, so you get updates on success or failure.
Let’s Stay Connected! 🤝
Learning is more fun when shared! 🌟 Let’s connect and grow together.
🔗 Follow my journey here:
👉 LinkedIn – Share your thoughts, let’s network!
👉 Hashnode – Read my blogs and dive deeper.
👉 GitHub – Check out my projects and code snippets.
Until next time, keep automating and innovating! 💡🚀
Happy Learning! ✨