Implementation of Containerization using Jenkins, GIT, Node.js & Docker

Implementation of Containerization using Jenkins, GIT, Node.js & Docker


Jenkins' Continuous Integration Pipeline serves as a powerful tool for automating diverse tasks pertaining to building, testing, deploying, and monitoring software. It seamlessly integrates with various tools and platforms, including Docker. In this exercise, we aim to establish a streamlined CI/CD pipeline for deploying a container within Docker, leveraging GIT as the repository for source code management.

Before proceeding, ensure that Jenkins plugins such as the Git plugin and Credentials Plugin are installed in the Jenkins environment. Additionally, Docker must be installed on the system. To initiate the process, navigate to the Jenkins dashboard and click on "New Item" to create a new project. Assign a suitable name for the project and select the "Pipeline" option before clicking "Ok".

Upon creation, the project named "first-docker-project" will be visible on the dashboard. Access the project and click on "Configure" to define its parameters. Within the pipeline definition, opt for "Pipeline script from SCM" and select "Git" under SCM. Specify the Repository URL where the source code, including the "Jenkinsfile", is stored. For this example, we will employ a Git URL.

The workflow will encompass five stages as illustrated below: Checkout SCM, Code, Build, Test, and Deploy.

Going through the provided Jenkinsfile, let's delve into the four stages:

pipeline {
    agent any 

    stages {
        stage('Code') { 
            steps {
                git branch: 'main', url: 'https://github.com/srinivish/my-node-app.git'
            }
        }

        stage('Build') { 
            steps {
                sh 'docker build . -t my-node-app:1.0'
            }
        }

        stage('Test') { 
            steps {
                echo "Testing" 
            }
        }

        stage('Deploy') { 
            steps {
                script {
                    def containerName = "my_node_app_jenkins"
                    def isRunning = sh(script: "docker inspect -f '{{.State.Running}}' $containerName", returnStatus: true) == 0
                    if (isRunning){
                        // Container is running, stop and remove it
                        sh "docker stop $containerName"
                        sh "docker rm -f $containerName"
                    }
                // Deploy container with new build
                sh "docker run -d --name $containerName -p 3000:3000 my-node-app:1.0"
                echo "created new container: $containerName"
                }
            }
        }
    }
}        

In the "Code" stage, we fetch the git location using the git branch command to specify the branch and URL. Adjust accordingly if there are no branches or if the root has a different name.

The "Build" stage involves a simple shell command to build the Docker container with a designated tag name.

For the "Test" stage, we currently publish an output using the echo command. Detailed unit testing scripts will be included in subsequent articles.

In the "Deploy" stage, we verify if the container is already running in Docker. If affirmative, it is gracefully stopped and forcefully removed to ensure no remnants remain. Following this step, the docker command is executed to deploy the latest build generated in the previous stage.

It's imperative to configure the SCM as the initial stage, as demonstrated in the following images. This facilitates Jenkins' access to the repository location, branch specification if any, and the configuration file name. In this instance, "Jenkinsfile" is utilized as the default configuration file.

Jenkins > Dashboard > <project name> > Configure > Pipeline.

Following github reference can be used to download source code used in this article include Docker container application.

Docker Implementation through Jenkins - Source Code

Hope you liked this article explaining basics of CI/CD pipeline!

Doyin Adeyemi

QA Engineer at Sook

1 年

Greetings I am happy that I came across your profile, I am having issues connecting my Jenkins pipeline to Git, I have tried all sorts ,are u able o put me through please if I send a recording of the issue? Kind regards

回复

要查看或添加评论,请登录

Srinivasan (Srini) Viswanathan的更多文章

社区洞察

其他会员也浏览了