Project: Set Up a CI/CD Jenkins Pipeline to Deploy a Dockerized Node.js Application on an EC2 Instance Using a Declarative Groovy Pipeline

Project: Set Up a CI/CD Jenkins Pipeline to Deploy a Dockerized Node.js Application on an EC2 Instance Using a Declarative Groovy Pipeline

·

4 min read

Steps to Set Up an EC2 Instance for Jenkins

  1. Navigate to EC2

  2. Launch an Instance

    • Click on the Launch Instance button.
  3. Configure the Instance

    • Name: Set the instance name to jenkins-server.

    • Application and OS Images (Amazon Machine Image): Select Ubuntu Server 24.04 LTS.

    • Instance Type: Choose t2.micro (eligible for the AWS Free Tier).

  4. Configure the Security Group

    • select a new security group or modify an existing one with the following rules:

      • SSH (Port 22): Allow from anywhere (0.0.0.0/0) or restrict it to your IP address to give you better security. and allow HTTP and HTTPS accordingly
  5. Launch the Instance

    • Review your configuration.

    • Click Launch Instance to initialize the server.

  6. Access the EC2 Instance

    • connect the instance to your terminal through SSH

Your EC2 instance is set up and ready to install and configure Jenkins.

  1. update the packages sudo apt-get update

  2. Before the installation of Jenkins, you have to install java sudo apt install fontconfig openjdk-17-jre java -version

  3. Select the long-term support of Jenkins to install

    add Jenkins repository key sudo wget -O /usr/share/keyrings/jenkins-keyring.asc \https://pkg.jenkins.io/debian/jenkins.io-2023.key

  4. echo "deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc]" \ https://pkg.jenkins.io/debian binary/ | sudo tee \ /etc/apt/sources.list.d/jenkins.list > /dev/null

  5. Update the package again and install the Jenkins

    sudo apt-get update sudo apt-get install jenkins

  6. enable the Jenkins and start it.

     sudo systemctl enable jenkins #enable the jenkins
     sudo systemctl start jenkins #start the jenkins
     sudo systemctl status jenkins #check the status of jenkins
    
    1. Install docker and docker-compose

       sudo apt update 
       sudo apt install docker.io docker-compose-v2 -y 
       sudo usermod -aG docker $USER && newgrp docker
       sudo usermod -aG docker jenkins #//add jenkins user to dockergroup
       sudo newgrp docker 
       sudo systemctl restart jenkins
      
    2. Go to the security group and add port 8080 Jenkins by default runs on port 8080 after that copy the public IP and paste it to the browser to access Jenkins public IP of ec2:8080

    3. The initialAdminPassword is required to unlock Jenkins during the initial setup. To retrieve it, you can follow these steps in your terminal:

      . Open your terminal.

      . Use the cat command sudo to access the initialAdminPassword file:

       sudo cat /var/lib/jenkins/secrets/initialAdminPassword
      

      . Copy the displayed password.

      . Paste it into the Jenkins setup screen under "Unlock Jenkins."

    4. Now, click on Install suggested plugins

      1. After that Jenkins asks to create a first user admin

      2. After creating the admin user, you’re prompted to configure the Jenkins URL (automatically pre-filled with the server's address).

        Once setup is complete, you'll land on the Jenkins dashboard, showing options to create jobs, manage plugins, and configure system settings.

      3. Click on Manage Jenkins in the left-hand menu. Search for the Plugin In the search bar, type Pipeline Stage View. Install the Plugin:

        Check the box next to the Pipeline Stage View plugin. Click on Install after that Restart Jenkins when installation is complete

      4. click on the new item put the name and select pipeline then ok

      5. put the descriptions and then choose In the Source Code Management section:

        • Select Git and paste the URL of your GitHub repository

      6. Go to Advanced Project Options Scroll down to the Pipeline section and define your pipeline script:

      7. click Build now

        pipeline{
            agent any;
            stages{
                stage("Code Clone"){
                    steps{
                       git url:"https://github.com/fauzeya67/node-todo-cicd.git", branch:"master"
                       echo "code clone stage done" 
                    }
                }
                stage("Image Build and test"){
                    steps{
                        sh "whoami"
                        sh "docker build -t node-app ."
                        echo "code build stage done"
                    }
                }
                stage("scan the image"){ 
                    steps{ 
                        echo 'I will scan and add code further' 
                    } 
                } 
                stage("Deploy"){
                    steps{
                        sh "docker compose down && docker compose up -d --build"
                        echo "code deploy stage done"
                    }
                }
            }
        }
        
      8. Click on Stage View and then see logs in the console output and access the deployed application

      9. To achieve automatic deployment of changes from a GitHub repository to a web application whenever a developer commits code, you can configure Jenkins with GitHub Webhooks and a deployment pipeline

        Configure Webhooks in GitHub

        1. Go to your repository on GitHub.

        2. Navigate to Settings > Webhooks.

        3. Click Add webhook.

        4. In the Payload URL field, enter the URL of your Jenkins server

      10. Now make changes in the code, it will automatically trigger the pipeline and deploy the updates to the web application without the need to manually click on "Build Now.

I hope this was helpful! Stay tuned for more content like this. 🚀