"Mastering Jenkins: A Guide to Configuring Nodes, Deploying Web Apps, and Pushing Images to Docker Hub Using Jenkins pipeline,"

"Mastering Jenkins: A Guide to Configuring Nodes, Deploying Web Apps, and Pushing Images to Docker Hub Using Jenkins pipeline,"

Β·

5 min read

πŸš€ In this guide, I'll walk you through deploying a web application using Jenkins, a powerful automation tool.

Step 1: Set Up Jenkins-Master & Jenkins-Agent

1.🌐 Create two EC2 instances in AWS: one for the Jenkins master and another for the Jenkins agent.

  • jenkins-master

  • jenkins-agent

2.πŸ”‘ Generate an SSH key on the Jenkins master using $ssh-keygen.

$ssh-keygen

3.Now go to .ssh folder and copy the id_rsa.pub which is public key.

$ cd .ssh $ ls -l $ cat id_rsa.pub

4.Copy the public key from the Jenkins master to the Jenkins agent's authorized_keys file.

$cd .ssh

$ls -l

$ vim authorized_keys

Paste public key

5.🀝 Connect the Jenkins master and agent using

$ssh ubuntu@<public_ip_Jenkins-server>.

6.β˜• Install Java on the Jenkins agent using

$sudo apt-get update

$sudo apt-get install openjdk-11-jre

7.Check installed Java version

$java -version

Step 2: Install Jenkins on Jenkins-Master

1.Go to jenkins-Master instance ,and installed java , jenkins ,and check installed java version,jenkins running status

$sudo apt-get update

Install Java on Jenkins-server

$sudo apt-get install openjdk-11-jre

Check installed Java Version

$java -version

Install Jenkins on Jenkins-server instance

$sudo wget -O /usr/share/keyrings/jenkins-keyring.asc https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key

echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] https://pkg.jenkins.io/debian-stable binary/ |

sudo tee /etc/apt/sources.list.d/jenkins.list > /dev/null

sudo apt-get update sudo apt-get install jenkins

2.🚦 Check Jenkins running status with

$systemctl status jenkins

  • Step 3: Adjust Inbound Rules

  • Don't forget to adjust the inbound rules in your EC2's security group to allow traffic on port 8080, ensuring that our Jenkins can be accessed.

Step 4: Access Jenkins Dashboard

🌐 Access the Jenkins server in your browser using http://<public_ip>:8080.

http://<public_ip>:8080

2.Copy initalAdminpassword path ,and go to your jenkins-serve instance ,and run below command

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

3.you will get temporary password copy it ,paste it in Administrator Password textbox, and click on continue button

Step 5: Create First Admin User and Install Plugins

πŸš€ Follow the on-screen instructions to create the admin user and install suggested plugins.

2.Started Jenkins plugin install process

3.Create First Admin User Password

4.Click on save and continue

5.Click on Start Using Jenkins

6.Congratulation your Jenkins Dashboard is ready now

Step 6: Set Up Jenkins Node

πŸš€ Go to the Jenkins Dashboard, click on Manage Jenkins, then Nodes, and add a new node.Now click on Nodes and click on β€œ+ New Node”

2.Click on New Node

3. Add details of the second agent node(make sure to fill in the Labels)

Node Name :agent-node

Click on Type:permanent Agent

4.Enter below details in Nodes

Name: agent Desc: This is our agent which will run Node JS application Number of executaion: 1 (cpu) Remote root directory: /home/ubuntu/jenkins-agent Labels: dev-server Usage: Only build jobs with label expression matching this node Launch method: launch agents via SSH Host: <Public-IPv4-addr-agent> Credentials: Kind: SSH username with private key ID: jenkins-ssh-key Description: jenkins-ssh-key Username: ubuntu Private Key: <Private-key-master-node> Host Key Verification Strategies: Non verifying verification strategy (SSH check with keys)

5.Click on save button ,and refresh agent-node ,Node will be connected and online.

Step 7: Install Docker-Compose

🐳 Install Docker and Docker-Compose on the agent for future requirements.

$sudo apt-get install docker.io docker-compose

$sudo usermod -aG docker $USER

$sudo reboot

Run $sudo usermod -aG docker $USER

$sudo reboot

Step 8: Set Up Node Application

  1. 🌐 Create a new pipeline item with the pipeline script for your Node.js application,Click Ok

2.Now give it a description β€œThis is a declarative pipeline for Node JS App” Put the GitHub repo

3. Build Triggers βœ… GitHub hook triggers for GITScm polling.

4.Now we have to pass the pipeline script in the pipeline to test the script.

pipeline { agent { label "dev-server" } stages{ stage("Clone Code"){ steps{ git url: "https://github.com/sprasadpujari/node-todo-cicd.git", branch: "master" } } stage("Build and Test"){ steps{ sh "docker build . -t node-app-test-new" } } stage("Push to Docker Hub"){ steps{ echo "Docker Hub will receive the image" } } stage("Deploy"){ steps{ sh "docker-compose down && docker-compose up -d" } } } }

5.Go to Manage Jenkins > Manage Plugin and install the β€œEnvironment Injector” and Download and install after restart Jenkins. It will store all the environment variables where we can pass the credentials.

6.Now go to Manage Jenkins > Credentials > System > Global Credentials > globals > + Add Credentials

Step 10: Connect GitHub and Jenkins

1.Now go to your github repo>settings>webhook>Add new webhook

🌐 Add a new webhook in your GitHub repository settings, pointing to your Jenkins URL.

Enter your jenkins url

2.🀝 Ensure GitHub and Jenkins are connected.

Step 11: Build and Deploy

  • ▢️ Click on "Build Now" in Jenkins.

🌐 Open port 8000 on the Jenkins agent to access the deployed app from the browser.

Our application image is also pushed to DockerHub

Create Jenkins file in your repo ,and paste your pipeline script ,and configure pipeline again with Pipeline Script from SCM ,and pass your repo URL ,Click save

Click on Build now option, and observe Declarative Checkout SCM is triggered

πŸŽ‰ Congratulations! Your web app is now deployed using Jenkins.

  • If you found value in this post, I sincerely appreciate your time and attention. Your support means the world to us! To further express your encouragement and appreciation:

  • Follow for More Insights:

    • Consider clicking the "Follow" button to stay updated on our latest posts. Join our community and be part of the ongoing conversation.
  • Like and Share:

    • Show your support by hitting the "Like" button. Sharing is caring – spread the knowledge and insights with your network.

Your engagement is a powerful motivator for us to continue sharing valuable content. Thank you for being a part of our community and investing your time in learning and growing.

Best Regards,

Sprasad 🌐✨

Β