Day 24 Task: Complete Jenkins CI/CD Project

Building a CI/CD Pipeline with Jenkins and Docker Compose for a Node.js Application 🚀

In today’s tech-driven world, automating your development and deployment process is crucial. A well-configured CI/CD pipeline not only speeds up delivery but also ensures your application is robust and error-free. In this blog, we’ll walk through building a CI/CD pipeline using Jenkins, Docker, and Docker Compose for a Node.js application.


What is CI/CD?

CI/CD stands for Continuous Integration/Continuous Deployment. It’s a methodology used to automate the software delivery process.

  • Continuous Integration: Ensures that code changes are automatically built, tested, and merged.

  • Continuous Deployment: Delivers code to production automatically after successful tests.


Project Overview

We’ll:

  1. Set up a GitHub repository for the Node.js application.

  2. Configure Jenkins for CI/CD.

  3. Integrate Docker Compose to build and deploy the application.


Step 1: Setting Up GitHub and Jenkins

Fork the Repository

  1. Go to the repository (shared by your team or mentor) and click Fork to clone it into your GitHub account.

  2. Clone the forked repository to your local machine to make changes as needed.

Integrate GitHub with Jenkins

  1. Open Jenkins and ensure the GitHub Integration plugin is installed:

    • Go to Manage Jenkins > Plugin Manager and install it if necessary.
  2. Create a new Jenkins job:

    • Select New Item > Freestyle Project.

    • Under Source Code Management, select Git and add your repository URL.

    • Under Build Triggers, enable GitHub hook trigger for GITScm polling.

  3. Add GitHub WebHook:

    • Go to your GitHub repository’s Settings > Webhooks.

    • Add a webhook with:

      • Payload URL: http://<your-jenkins-url>/github-webhook/

      • Content type: application/json.


Step 2: Using Docker Compose for Deployment

Create a Docker Compose File

Docker Compose simplifies multi-container setups. Create a docker-compose.yml file in your project directory:

version: '3.8'

services:
  app:
    build: .
    ports:
      - "3000:3000"
    volumes:
      - .:/usr/src/app
    environment:
      NODE_ENV: production
  db:
    image: mongo
    ports:
      - "27017:27017"

This configuration:

  • Builds the Node.js application from the current directory.

  • Exposes the app on port 3000 and a MongoDB service on port 27017.

Run Docker Compose via Jenkins

  1. Add a build step in your Jenkins job:

    • Select Execute Shell.

    • Add the following commands:

        # Navigate to the workspace
        cd $WORKSPACE
      
        # Build and run the application
        docker-compose up --build -d
      
  2. Save and apply your Jenkins job configuration.


Step 3: Trigger the CI/CD Pipeline

  1. Push your changes (including the docker-compose.yml file) to your GitHub repository.

  2. Trigger a Jenkins build:

    • This can happen manually or automatically through GitHub WebHooks.
  3. Jenkins will:

    • Pull the latest code from GitHub.

    • Build and deploy the application using Docker Compose.


Verification

  1. Access your application:

    • Navigate to http://<your-docker-host>:3000 in a browser.
  2. Review the Jenkins logs to ensure all steps were executed successfully.

  3. Test the application to verify functionality.


Why Use Jenkins and Docker Compose?

  • Jenkins provides a powerful, flexible automation server for CI/CD workflows.

  • Docker Compose simplifies managing multi-container applications, making deployment seamless.


Conclusion

Building a CI/CD pipeline might seem daunting initially, but with tools like Jenkins and Docker Compose, the process becomes efficient and rewarding. Not only does it automate repetitive tasks, but it also enhances reliability and reduces the risk of human error.

💡 Have questions or want to share your thoughts? Drop a comment below. Happy coding! 🎉