CI/CD DevOps

CI/CD with Jenkins pipeline & Nodejs into K8S (Part-2)

This is a series on how to CI/CD a Nodejs application using Jenkins as an integration server locally in personal computer

We’re going to see how to continuously deploy a Node.js application with Jenkins based on changes made to a GitHub repository. So let’s figure out the plan here. We’re going to build a Node.js app and upload it to Github. When changes are made to this repository, Jenkins will build the application and deploy or run the application.

In the earlier part, we build a simple NodeJs app and built a docker image, pushed it to the registry, and wrote deployment files to deploy into k8s.

In this part, we will write a pipeline for CI/CD in Jenkins.

The pipeline actually consists of build, test, build a docker image, and push to Dockerhub, Deploy into k8s. Since we did not write any test cases for our app, let us skip it.

Let us first install Jenkins.

Run the Jenkins service file. The first time it asks for an adminpassword which can be found at jenkins/secrets folder.

Then in the next step install suggested plugins.

After installing, the dashboard opens up. Go to Manage Jenkins and select Manage Plugins.

  • Install plugins for Nodejs, Docker, Kubernetes, and GitHub.
  • Go to Global Tool Configurations and find Git installation. Either provide the git.exe path in your system or choose to install automatically.
  • Then go to Nodejs installations. Either provide the .exe path in your system or choose to install automatically. Choose the version.
  • Let’s create a sample project and execute simple shell commands.

Click on New Item, Enter a name, and select Freestyle project.

Choose SourceCode Management(SCM) as Git and provide your repo address. Go to the Build phase and choose to execute a shell command.

Type npm install. save it. Click on Build Now to start building. It gets the source code from github and starts installing nodemodules. It shows status as success if there is no error.

  • Configure Docker Hub credentials in Jenkins to push the image into the registry.
  • Create an environmental variable in your system as KUBECONFIG and provide the .kube\.config path. Jenkins goes to this path to execute kubectl commands.
KUBECONFIG
  • Now let's create a pipeline.
pipeline{  
environment {
registry = "<Your-registry-username>/node-helloworld"
registryCredential = '<dockerhub_credentials_id_in_jenkins>'
dockerImage = ''
}
agent any
stages {
stage('Build'){
steps{
script{
sh 'npm install'
}
}
}
stage('Building image') {
steps{
script {
dockerImage = docker.build registry + ":latest"
}
}
}
stage('Push Image') {
steps{
script {
docker.withRegistry( '', registryCredential){
dockerImage.push()
}
}
}
}
stage('Deploying into k8s'){
steps{
sh 'kubectl apply -f deployment.yml'
}
}
}
}

Let’s breakdown the Jenkinsfile

Firstly, We are setting the environment variable to be used in the file.

The registry is the docker hub registry where we want to push the image and the image name.

registry credentials are of the docker hub. The docker hub credentials id which we have saved in Jenkins a while ago.

Then we have specified the stages of our pipeline and what each stage has to do in the pipeline.

Push this file into your Github-repository.

  • Now lets create a pipeline project in Jenkins.

Click on new Item, Put any name and choose pipeline and save it.

Creating Job

Then, Go to Build Trigger section and choose GitHub hook trigger for GITScm polling

Pipeline Phase, choose Git as SCM and provide your GitHub repo address

Now save this file and check the process by building it manually.

It should show as

stage view

Now we want to make this pipeline run whenever a commit is made. So we have to use github webhooks in our project.

Click on Add webhook. Now we have to place our Jenkins server URL here. so that whenever changes are identified it triggers our pipeline. But they have to communicate over the public internet. We cannot place the URL as http://localhost:8080 there. Now I’ll introduce you to ngrok.

Ngrok exposes local servers behind NATs and firewalls to the public internet over secure tunnels. Download the ngrok file from ngrok downloads.

Run the ngrok.exe file and type command

“ngrok.exe http 8080”

This command exposes our 8080 port over public internet and gives us an URL for 8 hours.

This URL has to be copied into github webhooks and pasted in the url section.

append /github-webhook/ to URL.

Then scroll down and choose the events for which you want to trigger the pipeline by clicking on “let me select individual events.”. And click on add webhook.

Thats it. Now you are all ready to check the CI/CD.

Make any change and commit into the github. That should trigger the event and run the pipeline.

This brings us to the end of our CI/CD With Jenkins and NodeJs series.

Hope you understood this post and series. Thankyou !!

Full stack developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store