Jenkins Declarative Pipeline with copying code from GitHub and build code through docker by pushing the image to dockerHub and deploying using AWS.

  • Installing Java on Master Node

    sudo apt update

sudo apt install openjdk-11-jre

  • Checking Java version

java --version

  • Installing Jenkins

  • Now, go to the Master node ec2 instance and add port 8080 in the security group.

-> Copy and paste the IP address of the master node ec2 instance with port 8080

  • Copy the password using the below command on the console of the master node

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

Copy the password and paste it to unlock Jenkins

Now, Jenkins is installed on your master node.

  • Go to the master node console and generate ssh keys

  • Go in .ssh

cd .ssh/

  • Generate ssh key

ssh-keygen

then press enter.

  • To view all the files

ls: keys have been generated

id_rsa is a private key

id_rsa.pub is a public key

NOTE:

->We have to generate the ssh-key in the Master node keep the private key, and the agent node keeps the public key.

  • View and copy the public key of the master node

cat id_rsa,pub

  • Then, go to the agent node and paste the public keys of the master node in the authorized_keys

vim authorized_keys

  • Now, go to the master node and ssh to the agent node from the master node

ssh ubuntu@IP address of the agent node

Now, the master node can be connected to the agent node via ssh.

  • Build the connection of the master and agent node in Jenkins.

  • Distributed Build

It is a type of build having a master node that gives tasks to multiple agent nodes.

  • Set up a distributed build

Set up an agent

  • Agent Node name

dev-node

  • Name of the project in the master node

node-todo-cicd

Fill out the description of the job/project

  • Enter the application URL on the GitHub

Project URL: github.com/ManishNegi963/node-todo-cicd

  • Enable webhook on Github

GitHub hook trigger for GitSCM polling

(As any commits in the GitHub will automatically trigger a build)

  • Enter the groovy syntax for Pipeline

->agent { label 'dev-node' }: refers to the name of the agent node in which the jobs will run

->It refers to stage named 'code' in which the steps will performed are git cloning of the branch master

git url: github.com/ManishNegi963/node-todo-cicd.git, branch 'master'

-> It refers to the stage named 'Build and deploy' where steps performed will be printing "Building and testing.."

->It refers to the stage named 'Deploy' where the steps performed will be printing "Deploying.."

  • Create a webhook that polls any commit and triggers a build on Jenkins.

Enter the IP address of the master node instance and add /git-webhook/ at the end

  • Change the READ.me file in GitHub and commit to checking if the pipeline is working.

add "or Run by docker-compose"

=>Error: ssh-authentication error and java error:

Resolution: Install Java on the agent node

sudo apt install openjdk-11-jre (To resolve java error)

Now go to the master node

step 1: cd .ssh/

step 2: ssh-keygen ed25519 press enter and name it jenkins_keys and enter, it will generate jenkins_keys private key and jenkins_keys.pub public keys

Step 3: Copy the public key from jenkins_keys.pub and paste it into the agent node authorized_keys

cat jenkins_keys.pub-> authorized_keys

Step 4: Copy the private keys in jenkins_keys and paste them into the node_todo_cicd project on the Jenkins website.

Go to Manage Credentials-> add credentials-> paste private keys of jenkins_keys->now go to configure node-todo-cicd and replace it with new credentials and build now.

The pipeline working but not auto-triggered with a commit in GitHub.

Manually click on build now.

Build successful.

All the 3 stages are working in the master node.

  • Now, go to the agent node console to check if the code is being copied (stage code: git cloning) into the agent node

workspace directory has been created

node-todo-cicd has been copied from the GitHub repository to the agent node

  • Now, install docker on the agent node to build and run the container

sudo apt install docker.io

  • Now, install docker-compose to run docker-compose

sudo apt-get install docker-compose

  • Now add the current user to the docker group to prevent access denied error.

sudo usermod -aG docker $USER

sudo reboot

  • View all the running containers in the agent node

docker ps

Now, edit stage 'build and test' with steps to build an image from dockerfile with the name node-todo-app

and stage 'deploy' with steps to docker-compose down and then up in detach/background mode.

Edit security rules in agent node: add port 8000 as Node js port is 8000 ( so that node js application can run on agent node)

Now, click on build now, build is successful.

Now copy the IP address of the agent node and add 8000 at the end.

Node js application is deployed.

Editing 'testing' in readme file. and edited the webhook with Send me everything being checked.

Now, as we commit in the GitHub. the pipeline is triggered and the build has been generated.

  • Auto triggered pipeline

  • Now, set up to push the image to the DockerHub and then deploy to the agent node on ec2.

  • Go to manage Jenkins-> plugins -> available plugins->Environment injector(to enable environment variable to enter credentials of docker hub)

  • Install, download, and check the Restart Jenkins.

  • Jenkins is restarting.

  • Now, go to manage credentials-> credentials

System-> Global-> add credentials(to add DockerHub credentials in env variable)

Kind: select username with password

Scope: Global

Username: (DockerHub usename)

Password: (DockerHub password)

ID: any name

Description: Short note of credentials

DockerHub ID has been created with the name 'dockerHub'

Now, go to Groovy syntax and add the stage "logging into docker hub and pushing image"

with steps add credentials to environment variable withCredentials([usernamePassword(credentialsID:'dockerHub', passwordvariable:'any name for password variable', usernameVariable:'any name for username variable')])

then adding login credentials in the env variable

sh "docker login -u ${env.username variable created above} -p ${env.password variable created above}"

Now, click on build now.

Build successful.

DockerHub login is successful.

Pipeline is working

Now, to push the image after building the image we have to edit the name and add a username tag before the image in stage"build and test"

sh 'docker build . -t mnmanish/node-todo-app:latest

Click on build now.

And the image has been pushed to DockerHub.

  • Setting up to pull the image from DockerHub

Now, go to GitHub, edit the docker-compose file to edit the build, and change to image to pull the image from dockerHub.

image: mnmanish/node-todo-cicd:latest

All the stages are working.

Now, go to node-todo-cicd/view/todo.ejs and edit "Jenkins is just Amazing and Awesome" and commit.

The build is triggered automatically.

The code is first cloned from the GitHub repository to the agent node then built using docker and pushed the image build to the DockerHub, and after that, it is pulled to the agent and the container has been made and then deployed to the ec2 instance.

Jenkins is just Amazing and Awesome has been updated automatically.

  • Enabling Declarative pipeline.

Now, go to the Groovy syntax copy the code paste it to Jenkinsfile in GitHub, and commit.

and clear the syntax on the Jenkins.

Go to pipeline and edit the settings:

Definition: Pipeline script from SCM( As now the script is on the GitHub)

SCM: Git

Repository URL: github.com/ManishNegi963/node-todo-cicd.git

Now, click on build now.

Declarative Checkout SCM(as it is taking instructions of groovy syntax from GitHub Jenkinsfile.

The declarative pipeline has been set up completely.