Jenkins Free Sytle Project for DevOps Engineers

Jenkins Free Sytle Project for DevOps Engineers


Understanding the Key Concepts 🧐

What is CI/CD?

  • Continuous Integration (CI) involves automatically testing and merging code changes from different developers into a shared repository.

  • Continuous Delivery (CD) ensures the application is always ready for deployment. It means the code passes through various tests and is prepared to go live without any hitches.

Think of CI/CD as a workflow that helps you deliver code changes reliably and quickly, making development and deployment more efficient.

What is a Jenkins Freestyle Project? 👷‍♂️

A Freestyle Project in Jenkins is a simple way to automate tasks in your application lifecycle:

  • Build your application.

  • Test the code automatically.

  • Deploy the app in different environments.

Let’s break down the tasks!


Task 1 : Create a Jenkins Freestyle Project to Build and Run a Docker Container for Your App 🐳

  1. Create an Agent for Your App:

    • An agent is the server where your Jenkins job runs. If your Jenkins server is on the same machine as Docker, this setup should work fine.

    • Go to Manage Jenkins > Manage Nodes and Clouds and make sure you have an active agent. If not, set one up with Docker installed on it.

  2. Create a New Jenkins Freestyle Project:

    • In Jenkins, click New Item and choose Freestyle project.

    • Name it something like Build-and-Run-Docker-App and click OK.

  3. Add Build Steps:

    • Under the Build section, click Add build step and select Execute shell.

    • For your first step, enter:

        docker build -t myapp-image .
      
      • This command tells Docker to build an image from your Dockerfile (in the current directory) and tag it as myapp-image.
    • For your second step, click Add build step again, and choose Execute shell. Enter:

        docker run -d --name myapp-container myapp-image
      
      • This command starts a new container from your myapp-image in detached mode (-d), so it runs in the background.
  4. Save and Run the Job:

    • Click Save to save the project.

    • Click Build Now to start the job. You should see the Jenkins console output as Docker builds the image and runs the container!


Task 2: Set Up a Jenkins Freestyle Project to Manage Multiple Containers with Docker Compose 🧩

In this task, you’ll create a Jenkins project to use Docker Compose for managing multiple containers (like an app and its database).

  1. Create a New Jenkins Freestyle Project:

    • Go back to New Item and choose Freestyle project.

    • Name it something like Docker-Compose-App and click OK.

  2. Add Build Step for Docker Compose Up:

    • Under the Build section, click Add build step and choose Execute shell.

    • Enter the following command:

        docker-compose -f docker-compose.yml up -d
      
    • This command tells Docker Compose to start all services defined in your docker-compose.yml file in detached mode (-d).

  3. Add Cleanup Step for Docker Compose Down:

    • Click Add build step again, select Execute shell, and add this command:

        docker-compose -f docker-compose.yml down
      
    • This command stops and removes the containers, networks, and volumes defined in the docker-compose.yml file. It's essential for cleanup after the job finishes.

  4. Schedule the Job (Optional):

    • If you want Jenkins to run this job periodically, go to the Build Triggers section.

    • Select Build periodically and specify a schedule (e.g., H * * * * to run every hour).

  5. Save and Run the Job:

    • Click Save to store the project settings.

    • Click Build Now to start the job. Jenkins will use Docker Compose to manage the multiple containers defined in your setup.


Why Jenkins CI/CD and Freestyle Projects Are Beneficial 🌟

  • Automation: Jenkins frees up your time by automating repetitive tasks, allowing you to focus on improving your app rather than managing builds.

  • Consistency: Each job runs the same way every time, ensuring a stable and predictable process.

  • Flexibility: You can set up different jobs for different tasks, use plugins to expand Jenkins’ capabilities, and customize workflows to meet your needs.

By creating these tasks, you’re now automating steps that ensure your application is always ready to deploy. Jenkins Freestyle Projects are powerful tools that simplify CI/CD, making it easier to maintain quality and speed in development. 💪

Happy Learning and Automating! 🎉