Complete Jenkins CI/CD Project

Β·

9 min read

Table of contents

Task 1

  • Fork this repository:

  • Create a connection to your Jenkins job and your GitHub Repository via GitHub Integration.

  • Read About GitHub WebHooks and make sure you have a CICD setup.

1. Fork the Repository

  • Go to the repository link: node-todo-cicd.

  • Click the Fork button (top-right corner) to fork the repository into your GitHub account.


2. Clone the Forked Repository

  • Copy the URL of your forked repository.

  • Clone it to your local system:

      git clone <URL-of-your-forked-repository>
      cd node-todo-cicd
    

3. Install Dependencies and Verify the Project Locally

  • Install required dependencies for the project:

      npm install
    
  • Run the project to ensure it works as expected:

      npm start
    
  • Access the app at http://localhost:3000.


4. Set Up Jenkins

Install Jenkins

  • Make sure Jenkins is installed and running on your system.

  • Access Jenkins at http://localhost:8080.

Install Required Jenkins Plugins

  • Go to Manage Jenkins > Manage Plugins > Available and install:

    • GitHub Integration

    • Pipeline

    • NodeJS

    • Git

    • Webhook Trigger Plugin

Create a New Jenkins Job

  • Go to Jenkins Dashboard > New Item.

  • Choose a pipeline and name your job (e.g., Node-Todo-CICD), and click OK.

  • Configure the pipeline:

    1. In the Pipeline section, select Pipeline script from SCM.

    2. Choose Git as SCM and provide the URL of your forked repository.

    3. Enter the branch name, e.g., main.

    4. Save the job.


5. Set Up GitHub Integration with Jenkins

Generate a Jenkins API Token

  1. Go to Jenkins > Your Profile > Configure.

  2. Click Add New Token, generate it, and copy the token.

Add Credentials to Jenkins

  1. Go to Manage Jenkins > Manage Credentials.

  2. Add a new credential:

    • Scope: Global

    • Username: Your GitHub username

    • Password: Your GitHub Personal Access Token

    • ID: github-credentials (or any identifier)


6. Configure GitHub Webhook

Enable Webhook in Your Repository

  1. Go to your forked repository on GitHub.

  2. Navigate to Settings > Webhooks > Add webhook.

  3. Provide the Jenkins webhook URL:

     http://<Jenkins-URL>:8080/github-webhook/
    
  4. Select application/JSON as the content type.

  5. Enable push events and save.

Test the Webhook

  • Make a change in your repository (e.g., update README.md) and push it.

  • Check if Jenkins triggers a build automatically.


7. Create the Jenkinsfile for CI/CD

  • In the root of your repository, create a file named Jenkinsfile:

      pipeline {
          agent any
          stages {
              stage('Checkout Code') {
                  steps {
                      git branch: 'main', url: 'https://github.com/<your-username>/node-todo-cicd.git'
                  }
              }
              stage('Install Dependencies') {
                  steps {
                      sh 'npm install'
                  }
              }
              stage('Run Tests') {
                  steps {
                      sh 'npm test'
                  }
              }
              stage('Build and Deploy') {
                  steps {
                      sh 'npm run build'
                  }
              }
          }
      }
    

8. Validate the Setup

  • Trigger the job manually or by making changes to the repository.

  • Check Jenkins logs to confirm that the CI/CD stages execute successfully.

Task-02

  • In the Execute shell run the application using Docker compose

  • You will have to make a Docker Compose file for this Project (Can be a good open-source contribution)

  • Run the project.

1. Create a docker-compose.yml File

To containerize the application and run it using Docker Compose, follow these steps:

1.1 Define the Services in docker-compose.yml

  • Create a docker-compose.yml file in the root directory of the project:

      version: '3.8'
    
      services:
        app:
          build:
            context: .
            dockerfile: Dockerfile
          ports:
            - "3000:3000"
          volumes:
            - .:/app
            - /app/node_modules
          environment:
            - NODE_ENV=development
    
        mongo:
          image: mongo:latest
          container_name: mongodb
          ports:
            - "27017:27017"
          volumes:
            - mongo_data:/data/db
    
      volumes:
        mongo_data:
    

2. Create a Dockerfile for the Application

  • In the root of your project, create a file named Dockerfile:

      # Use Node.js as the base image
      FROM node:16
    
      # Set the working directory
      WORKDIR /app
    
      # Copy package.json and package-lock.json
      COPY package*.json ./
    
      # Install dependencies
      RUN npm install
    
      # Copy the rest of the application code
      COPY . .
    
      # Expose the port the app runs on
      EXPOSE 3000
    
      # Command to run the app
      CMD ["npm", "start"]
    

3. Build and Run the Project Using Docker Compose

3.1 Build the Docker Images

  • Run the following command to build the Docker images for your services:

      docker-compose build
    

3.2 Start the Application

  • Start the application using:

      docker-compose up
    

3.3 Access the Application

  • The application should now be running on http://localhost:3000.

  • MongoDB will be available on the port 27017.


4. Run the Application in Jenkins

4.1 Add Docker Compose Command to Jenkins Pipeline

  • Update your Jenkinsfile to include a stage that runs the application using Docker Compose:

      pipeline {
          agent any
          stages {
              stage('Checkout Code') {
                  steps {
                      git branch: 'main', url: 'https://github.com/<your-username>/node-todo-cicd.git'
                  }
              }
              stage('Build and Run with Docker Compose') {
                  steps {
                      sh 'docker-compose down || true'
                      sh 'docker-compose up -d --build'
                  }
              }
          }
      }
    

4.2 Verify Jenkins Pipeline

  • Push changes to the repository.

  • Trigger the Jenkins job and ensure the docker-compose commands execute successfully.


5. Test and Validate

  • Open http://localhost:3000 in your browser to confirm the app is running.

  • Use docker ps to verify that the app and mongo containers are running.

  • Check logs if any issues arise:

      docker-compose logs
    

6. Optional: Contribute Your Docker Compose File

  • Once the setup works perfectly, consider contributing to the docker-compose.yml and Dockerfile back to the original repository as an open-source contribution.

Steps to Contribute:

  1. Commit Your Changes:

     git add Dockerfile docker-compose.yml
     git commit -m "Added Docker Compose setup for the project"
    
  2. Push Changes:

     git push origin main
    
  3. Create a Pull Request:

    • Go to the original repository: node-todo-cicd.

    • Click Pull Requests > New Pull Request.

    • Add a title and description, then submit your PR.

Task-03

  • Document the process from cloning the repository to adding webhooks, Deployment, etc. as a README, go through this example

  • A well-written readme file will help others to understand your project and you will understand how to use the project again without any problems.

Node Todo App CI/CD

This project demonstrates how to set up a CI/CD pipeline for a Node.js application using Jenkins, GitHub, and Docker. The app is a simple TODO list application that can be built, tested, and deployed automatically whenever changes are pushed to the repository.


Steps to Set Up the Project

1. Fork and Clone the Repository

  1. Fork the repository: node-todo-cicd to your GitHub account.

  2. Clone your forked repository to your local machine:

     git clone https://github.com/<your-username>/node-todo-cicd.git
     cd node-todo-cicd
    

2. Run the Application Locally

  1. Install dependencies:

     npm install
    
  2. Start the application:

     npm start
    
  3. Open your browser and navigate to http://localhost:3000 to verify the app is running.


3. Set Up Jenkins

3.1 Install Jenkins

  • Download and install Jenkins from the official website.

  • Start Jenkins and access it at http://localhost:8080.

3.2 Install Required Jenkins Plugins

  • Navigate to Manage Jenkins > Manage Plugins > Available and install:

    • GitHub Integration

    • Pipeline

    • NodeJS

    • Git

    • Webhook Trigger Plugin


4. Configure GitHub Webhooks

4.1 Generate Jenkins API Token

  1. Go to Jenkins > Your Profile > Configure.

  2. Click Add New Token, generate it, and copy the token.

4.2 Add Webhook in GitHub

  1. Go to your forked repository on GitHub.

  2. Navigate to Settings > Webhooks > Add webhook.

  3. Set the payload URL to:

     http://<Jenkins-URL>:8080/github-webhook/
    
  4. Choose application/JSON as the content type and enable push events.


5. Create a Jenkins Job

  1. Go to the Jenkins Dashboard and create a new job:

    • Click New Item.

    • Choose Pipeline, name the job (e.g., Node-Todo-CICD), and click OK.

  2. Configure the job:

    • Under Pipeline, select Pipeline script from SCM.

    • Choose Git as the SCM and provide the repository URL of your forked project.

    • Set the branch to main.


6. Write the Pipeline Script

Create a file named Jenkinsfile in the root directory of your repository:

pipeline {
    agent any
    stages {
        stage('Checkout Code') {
            steps {
                git branch: 'main', url: 'https://github.com/<your-username>/node-todo-cicd.git'
            }
        }
        stage('Install Dependencies') {
            steps {
                sh 'npm install'
            }
        }
        stage('Run Tests') {
            steps {
                sh 'npm test'
            }
        }
        stage('Build and Deploy') {
            steps {
                sh 'npm start'
            }
        }
    }
}

7. Test the CI/CD Pipeline

  1. Commit and push the Jenkinsfile to your repository:

     git add Jenkinsfile
     git commit -m "Added Jenkinsfile for CI/CD pipeline"
     git push origin main
    
  2. Trigger a build in Jenkins:

    • The build will be triggered automatically if the webhook is set up correctly.

    • Alternatively, run the job manually from the Jenkins dashboard.

  3. Verify the build logs to confirm that the pipeline stages (Checkout Code, Install Dependencies, Run Tests, Build and Deploy) executed successfully.


8. Optional: Dockerize the Application

  1. Create a Dockerfile to containerize the app:

     FROM node:16
    
     WORKDIR /app
    
     COPY package*.json ./
    
     RUN npm install
    
     COPY . .
    
     EXPOSE 3000
    
     CMD ["npm", "start"]
    
  2. Build and run the Docker container:

     docker build -t node-todo-app .
     docker run -p 3000:3000 node-todo-app
    

9. Troubleshooting

  • Webhook Not Triggering Build:

    • Ensure the webhook URL is correct and publicly accessible.

    • Check the GitHub webhook delivery logs for errors.

  • Build Failing:

    • Review Jenkins logs for issues.

    • Verify the Jenkinsfile syntax and commands.


Contributing

Feel free to contribute by:

  • Improving the pipeline configuration.

  • Adding new features to the application.

  • Enhancing documentation.

  1. Fork the repository.

  2. Create a new branch:

     git checkout -b feature-branch
    
  3. Commit your changes:

     git commit -m "Description of changes"
    
  4. Push to your fork and create a pull request.

Task-04

  • Also, it's important to keep smaller goals, as it’s a small task, think of a small Goal you can accomplish.

  • Write about it using this template

  • Have small goals and strategies to achieve them, and also have a small reward for yourself.

πŸ“Œ February Goal Planner

πŸ“ My Goal:
Set up a working CI/CD pipeline for the Node-Todo project using GitHub Webhooks and Jenkins.

πŸ”§ My Strategies:

  • Connect Jenkins with GitHub via Webhooks.

  • Automate build and deployment using a Jenkins pipeline.

  • Test the pipeline by pushing changes and verifying automatic builds.

  • Debug and refine the setup if needed.

πŸ’‘ Note to self:
Take it step by step! A small but properly working CI/CD pipeline is better than a complex, broken one. πŸš€

πŸŽ‰ Reward:
A cup of my favorite coffee β˜• and 30 minutes of watching tech videos! 🎬

Β