Add 2 Nodes in Jenkins & Made Push to Branches & Pull Content to Git Folder — Jenkins Assignment 2

Visal Tyagi
DevOps-Guides
Published in
17 min readMay 3, 2024

Tasks to Be Performed:

1. Add 2 nodes to Jenkins master

2. Create 2 jobs with the following jobs:

a. Push to test

b. Push to prod

3. Once a push is made to the test branch, copy Git files to the test server

4. Once a push is made to the master branch, copy Git files to the prod server.

Trigger Pipeline & Fetch Content to Git Local
Trigger Pipeline & Fetch Content to Git Local

Git Hub Repository Link for Copying All Commands From Here:

Before checking this assignment solution, kindly check theJenkins Assignment1 Solution”.

Problem (1) Solution: Add 2 Nodes to Jenkins Master

a. Create Jenkins Master, Test & Prod Server

Step 1: Go to the “Services” section & search “EC2” here. Put the cursor over “EC2” & click on “Instances”.

Search the EC2 Service
Search the EC2 Service

Step 2: Click on “Launch Instance”.

Launch Instance
Launch Instance

Step 3: Write “Jenkins-Master” in “Name” under the “Name and tags” section.

Instance Name
Instance Name

Step 4: Choose “AMI” as “ubuntu”.

Ubuntu AMI
Ubuntu AMI

Step 5: Choose “Instance type” as “t2.micro”.

t2.micro instance
t2.micro instance

Step 6: Choose “key pair (login)” as “Jenkins2”.

Choose key pair
Choose key pair

Step 7: In “Network settings”, choose the following options:

Firewall (security groups): — Select existing security group

Common security groups: — launch-wizard-9

Choose Security Group Here
Choose Security Group Here

Step 8: In “Summary”, put “Number of Instances” as 3. Click on “Launch Instance”.

Launch Instance
Launch Instance

Step 9: The instance will be successfully created. Click on “Instances” in “Breadcrumbs”.

Instances Launched
Instances Launched

Step 10: Your “3 Instances” has been successfully run.

Instances
Instances

Step 11: Rename the second instance as “Test”. Click on “Right Arrow” & instance will be saved as “Test”.

Rename Second Instance
Rename Second Instance

Step 12: Rename the third instance as “Prod”. Click on “Right Arrow” & instance will be saved as “Prod”.

Rename the Third Instance
Rename the Third Instance

Step 13: Your three instances have been successfully saved as “Jenkins-Master”, “Test” & “Prod”.

Instances Renamed
Instances Renamed

b. Install “Java” over “Jenkins-Master”

Step 1: Paste this command over “Jenkins-Master” & press “Enter” from the keyboard. ”

sudo apt install fontconfig openjdk-17-jre
Install Open JDK
Install Open JDK

Step 2: Type “Y” & “Java Installation” and start running.

Type “Y”
Type “Y”

Step 3: Type this command to check the “java version”.

java --version
Check Java Version
Check Java Version

c. Install “Jenkins” Over “Jenkins-Master”

Step 1: Select the “Jenkins-Master” Instance & click on “Connect”.

Select Jenkins Master Instance
Select Jenkins Master Instance

Step 2: Click on “Connect” Again.

Again Connect
Again Connect

Step 3: First, update the machine using this command:

sudo apt-get update
Update the Machine
Update the Machine

Step 4: Paste this command & press “Enter” from the keyboard.

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

Step 5: Paste this command & press “Enter” from the keyboard.

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

Step 6: Run the below-given update command again & press “Enter” from the keyboard.

sudo apt-get update
Update the Machine Again
Update the Machine Again

Step 7: Type this command to install the “Jenkins”:

sudo apt-get install jenkins
Install Jenkins
Install Jenkins

Step 8: Type “Y” & it will start continuing to install “Jenkins”.

Type “Y”
Type “Y”

Step 9: To start Jenkins, run the below-given command & press “Enter” from the keyboard.

sudo systemctl enable jenkins
Enable Jenkins
Enable Jenkins

Step 10: To start Jenkins, run the below-given command & press “Enter” from the keyboard.

sudo systemctl start jenkins
Start Jenkins
Start Jenkins

Step 11: To view the status, type the below-given command & press “Enter” from the keyboard.

sudo systemctl status jenkins
Jenkins Installation Status
Jenkins Installation Status

Step 12: Type this URL (http://52.66.23.183:8080/login) & press “Enter” http://52.66.23.183:8080/login keyboard.

Copy the Given Command
Copy the Given Command

Step 13: Type the below-given command & press “Enter” from the keyboard.

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

Copy the given token from here.

Copy the Token
Copy the Token

Step 14: Paste the token in the “Unlock Jenkins” section & click on “Continue”.

Paste Token In Jenkins
Paste Token In Jenkins

Step 15: Click on “Install Suggested Plugins”.

Install Suggested Plugins
Install Suggested Plugins

Step 16: It will start setting up the needed “Jenkins” plugin.

Setting up the Jenkins
Setting up the Jenkins

Step 17: Put the following entries here:

Username: admin

Password: admin

Confirm Password: admin

Put Username & Password Here
Put Username & Password Here

Full name: admin

E-mail address: admin@admin.com

Click on “Save and continue”.

Save and Continue
Save and Continue

Step 18: Click on “Save and Finish”.

Save and Finish
Save and Finish

Step 19: Click on “Start using Jenkins”.

Click Start using Jenkins
Click Start using Jenkins

Step 20: “Jenkins Dashboard” will be ready.

Jenkins Dashboard Set Up
Jenkins Dashboard Set Up

c. Install “Java” on “Test Server”.

Step 1: Select the “Test Instance” & click on “Connect”.

Select the Test Instance
Select the Test Instance

Step 2: Again, click on “Connect”.

Click Connect
Click Connect

Step 3: First, update the machine using the below-given command & Press “Enter” from the keyboard.

sudo apt-get update
Update the Test Machine
Update the Test Machine

Step 4: Paste the below-given command over “Test” & press “Enter” from the keyboard.

sudo apt install fontconfig openjdk-17-jre
Install Java on Test Machine
Install Java on Test Machine

Step 5: Type “Y” & “Java Installation” and start running.

Type “Y” to Continue
Type “Y” to Continue

Step 6: Type the below-given command to ’check’ the “java version”.

java --version
Java Version
Java Version

d. Install “Java” on “Prod Server”.

Step 1: Select the “Prod Instance” & click on “Connect”.

Select the Prod Instance
Select the Prod Instance

Step 2: Again, click on “Connect”.

Again, Connect Prod Instance
Again, Connect Prod Instance

Step 3: First, update the machine using the below-given command & Press “Enter” from the keyboard.

sudo apt-get update
Update the Prod Machine
Update the Prod Machine

Step 4: Paste this command over “Prod” & press “Enter” from the keyboard.

sudo apt install fontconfig openjdk-17-jre
Install OpenJDK on Prod
Install OpenJDK on Prod

Step 5: Type “Y” & “Java Installation” and start running.

Type “Y” to continue
Type “Y” to continue

Step 6: Type the below-given command to check the “java version”:

java --version
Java Version
Java Version

e. Add “Test Server” to Jenkins Master

Step 1: Go to “Jenkins Master” & click on “New Node”.

Click on New Node
Click on New Node

Step 2: Write “Node name” as “Test”. Choose “Type” as “Permanent Agent”.

Click on “Create”.

Create Node

Step 3: Choose the following options here:

Name: Test

Description: This is the test node.

Number of executors: 1

Remote root directory: /home/ubuntu/jenkins

Put Test Node Name, Description & Root Directory
Put Test Node Name, Description & Root Directory

Label: Write “Test” here because “Jenkins” identifies the node using this feature.

Launch Method: Launch agents via SSH

Host: 172.31.38.60 (Test IP)

Credentials: Click on “Add”. Again, click on “Jenkins”.

Choose Launch Method
Choose Launch Method

In “Add Credentials”, choose “kind” as “SSH Username with private key”.

Scope: Global (Jenkins, nodes, items, all child items, etc) — Remain as it is.

ID: PEM

Description: PEM

Username: ubuntu

Put Username & Description with Kind
Put Username & Description with Kind

In “Private Key”, choose “Enter Directly”. Click on “Add”.

Click on Add
Click on Add

Now, paste the. pem key content here. We have taken the pem key as “Jenkins2”.

pem content
pem content

Click on “Add”.

Add Private Key
Add Private Key

Step 4: Choose “Credentials” as “ec2-user (PEM)”.

Choose “Host Key Verification Strategy” as “Non verifying Verification Strategy”.

Click on “Save”.

Save All Test Node Settings
Save All Test Node Settings

Step 5: “Test Node” has been successfully launched like this:

Test Node Added
Test Node Added

f. Add “Prod Server” to Jenkins Master

Step 1: Go to “Jenkins Master” & click on “New Node”.

Click New Node Again
Click New Node Again

Step 2: Write “Node name” as “Prod”. Choose “Type” as “Copy Existing Node”. In “Copy Existing Node”, choose “Test”.

Click on “Create”.

Choose Test Node Settings Here
Choose Test Node Settings Here

Step 3: Fill the following options here:

Description: This is for the prod node

Labels: Prod

Host: 172.31.40.194

Choose Description, Root Directory & Labels
Choose Description, Root Directory & Labels

Remain all entries are as it is.

Click on “Save”.

Click Save
Click Save

Step 4: The “Prod” Node has been successfully launched.

Prod Node
Prod Node
Prod Created Successfully
Prod Created Successfully

Problem (2) Solution: Create 2 jobs with the following jobs:

a. Push to test

b. Push to prod

a. Create a Repository in Git Hub as “Assignment-2-Jenkins”

Step 1: Click “New” in the “Top Repositories” section.

Click New
Click New

Step 2: Choose the following options to create a repository.

Repository name: — Assignment-2-Jenkins

Description (optional): — Jenkins Assignment 2 Repository

Choose “Public” Option.

Put Repository Name & Description with Public Option
Put Repository Name & Description with Public Option

Click on “Create Repository”.

Create Repository
Create Repository

Step 3: Repository has been created as “visaltyagi / Assignment-2-Jenkins”.

Assignment 2 Jenkins Repository Created
Assignment 2 Jenkins Repository Created

b. Rename automatically creates the “main” branch as “master” & creates a file inside it

Step 1: Click on “Creating a new file”.

Click Create a new file option
Click Create a new file option

Step 2: You will notice that it is asking you to create a file in the “main” branch. Click on “main” for renaming.

Click on “main”
Click on “main”

Step 3: You will land inside the “main” branch. Click on “1 Branch”.

Click on “Branch”
Click on “Branch”

Step 4: Click on “Three Vertical Dots”.

main branch
main branch
Click on Three Dots
Click on Three Dots

Step 5: Click on “Rename Branch”.

Click Rename Branch
Click Rename Branch

Step 6: Rename “main” as “master”. Click on “Rename branch”.

Rename master
Rename master
Rename as master
Rename as master

Step 7: The branch will be successfully renamed as “master”.

Renamed as “master”
Renamed as “master”

Step 8: Now, we will create a file. Click on “Add File”.

Click Add File
Click Add File

Step 9: Click on “Create new file”.

Create new file
Create new file

Step 10: Write “master.txt” as the file name & write “description” as “This is a master file”.

master.txt file
master.txt file

Step 11: Click on “Commit changes”.

Commit Changes
Commit Changes

Step 12: Again, click on “Commit changes”.

Commit master.txt file
Commit master.txt file

Step 13: A “master.txt” file has been successfully created.

master.txt created
master.txt created

c. Create a “Test” Branch & Create a File Inside It

Step 1: Click on “Master>View All Branches”.

Click View All Branches
Click View All Branches

Step 2: Click on “New branch”.

Create New Branch
Create New Branch

Step 3: Write “New branch name” as “Test”. Remain “Source” as “Test”.

Write the Branch Name & Create It
Write the Branch Name & Create It

Step 4: The “Test” branch has been successfully created.

Test Branch
Test Branch

Step 5: Click on “Test” & click on “Add file”.

Add File
Add File

Step 6: Click on “Create new file”.

Create New File
Create New File

Step 7: Write the file name as “Test.txt” & choose “Description” as “This is a test file”.

File Name and Description
File Name and Description

Step 8: Click on “Commit changes”.

Commit Changes
Commit changes

Step 9: Again, click on “Commit changes”.

Commit test.txt file
Commit test.txt file

Step 10: The “test.txt” file has been successfully created.

test.txt file created
test.txt file created

Problem 2 (a) Solution: Create a Job for Pushing to Test Repo

Step 1: Click on “New Item”.

Click on New Item
Click on New Item

Step 2: “Enter an Item Name” as “Test” & Choose “Freestyle project”.

Choose Job Name & Type
Choose Job Name & Type

Click on “OK”.

Step 3: In “General”, choose “Description” as “This is a test job.”.

Choose “GitHub Project” & “Project URL” as https://github.com/visaltyagi/Assignment-2-Jenkins.git.

Choose Job Description & Git Hub Project URL
Choose Job Description & Git Hub Project URL

Choose “Restrict where this project can be run” & Select “Test” as “Label Expression”.

Choose Label Expression
Choose Label Expression

Step 4: Choose “Source Code Management” as “Git”.

Repository URL: https://github.com/visaltyagi/Assignment-2-Jenkins.git

Credentials: visaltyagi/******(GitHub credentials)

Choose “Branch Specifier (blank for ‘any’) as “*/test” in “Branches to build”.,

Choose Branch & Repository Credentials
Choose Branch & Repository Credentials

Step 5: Choose the option “GitHub hook trigger for GITScm polling” to add “Webhook”. Click on “Save”.

Choose Git Hub Hook Trigger Option
Choose Git Hub Hook Trigger Option

Step 5: The “Test” job has been successfully created.

Test Job Created
Test Job Created

Step 6: Click “Build Now” to deploy the “Test” Repository File Here.

Build the Job
Build the Job

Step 7: “Build” has been successfully created. Click on Build #1.

Build Created Successfully
Build Created Successfully

Step 8: Go to “Console Output”. And the pipeline will be successfully created with “test.txt”.

Check Console Output
Check Console Output
test.txt created
test.txt created

Step 9: To verify that “test.txt” is successfully pushed in “Jenkins Workspace”, go to “Test” & type this command:

cd /home/ubuntu/jenkins/workspace/Test
test.txt fetched
test.txt fetched

And, do “ls” here. You will find “Test” Here.

“test.txt” has been successfully shown.

Problem 2 (b) Solution: Create a Job for Pushing to Prod Repo

Step 1: Click on “New Item”.

Add a Prod Job
Add a Prod Job

Step 2: “Enter an Item Name” as “Prod” & Choose “Freestyle project”.

Click on “OK”.

Create Prod Job
Create Prod Job

Step 3: In “General”, choose “Description” as “This is a prod job.”.

Choose “GitHub Project” & “Project URL” as https://github.com/visaltyagi/Assignment-2-Jenkins.git.

Choose Job Description & GitHub Project
Choose Job Description & GitHub Project

Choose “Prod” as “Label Expression” in “Restrict where the project can be run”.

Choose Label as “Prod”
Choose Label as “Prod”

Step 4: Choose “Source Code Management” as “Git”.

Repository URL: https://github.com/visaltyagi/Assignment-2-Jenkins.git

Credentials: visaltyagi/******(GitHub credentials)

Choose Repo URL & Credentials
Choose Repo URL & Credentials

Choose “Branch Specifier (blank for ‘any’) as “*/master” in “Branches to build”.

Put master branch here
Put master branch here

Step 5: Choose “GitHub hook trigger for GITScm polling” & Click on “Save”.

Choose Git Hub Web Hook Option
Choose Git Hub Web Hook Option

Step 6: The “Prod” job has been successfully created.

Prod Job Created
Prod Job Created

Step 7: Click “Build Now” to deploy the “Prod” Repository File Here.

Build a New Job
Build a New Job

Step 8: “Build” has been successfully created. Click on Build #1.

Build Created
Build Created

Step 9: Go to “Console Output”. And the pipeline will be successfully created with “master.txt”.

Pipeline Created
Pipeline Created
master.txt created
master.txt created

Step 10: To verify that “master.txt” is successfully pushed in “Jenkins Workspace”, go to “Prod” & type this command:

cd /home/Ubuntu/jenkins/workspace/Prod
master.txt fetched on local
master.txt fetched on local

And, do “ls” here. You will find “Prod” Here.

“master.txt” has been successfully shown.

Problem (3) Solution: Once a push is made to the test branch, copy Git files to the test server

a. Create a Webhook in the Git Hub Repository

Step 1: Go to “Settings” in “Assignment-2-Jenkins” Repository.

Repository Settings
Repository Settings

Step 2: Click on “Wehbhooks”.

Click Webhooks
Click Webhooks

Step 3: Click on “Add webhook”.

Add webhook
Add webhook

Step 4: Choose the following options here:

Payload URL: http://52.66.23.183:8080/github-webhook/

Content type: application/x-www-form-urlencoded

Secret: Leave this empty

Which events would you like to trigger this webhook?: Just the push event

Remain Status as “Active”.

Create a Webhook
Create a Webhook

Step 5: Click on “Webhook URL”.

Webhook Created
Webhook Created

Step 6: Click on “Recent Deliveries”.

Click on Recent Deliveries
Click on Recent Deliveries

Step 7: Delivery has been successfully pinged.

Delivery Pinged
Delivery Pinged

b. Create a File in “Test” Repo & Files Automatically Pushed in “Test” Job

Step 1: Go to the “test” branch in “GitHub Repository”.

Click on the “+” sign & Again, click on “Create new file”.

Create a new file in test
Create a new file in test

Step 2: Write the file name as “test1.txt” & write a description as “This is test1 file”. Click on “Commit Changes”.

Create test1.txt file
Create test1.txt file

Step 3: Again, click on “Commit changes”.

Click Commit Changes
Click Commit Changes

Step 4: “test1.txt” has been successfully created in “test”.

test1.txt created
test1.txt created

Step 5: Go to “Jenkins Dashboard” & click on “Test”.

Test Job Automatically Triggered
Test Job Automatically Triggered

Step 6: Click on “#4”.

Build Automatically Created
Build Automatically Created

Step 7: Click on “Console Output”.

Check Console Output
Check Console Output
Check Console Output
Check Console Output
test1.txt created message
test1.txt created message

Step 8: Go to the “Test” server & type the below-given command & press “Enter” from the keyboard. The “test1.txt” file has been successfully shown.

ls  
test1.txt file shown on local
test1.txt file shown on local

Problem (4) Solution: Once a push is made to the master branch, copy Git files to the prod server.

Step 1: Go to the “master” branch in “GitHub Repository”.

Click on the “+” sign & Again, click on “Create new file”.

Create a New File in the master branch
Create a New File in the master branch

Step 2: Write the file name as “master1.txt” & write a description as “This is master1 file”. Click on “Commit Changes”.

Put File Name and Description here
Put the File Name and Description here

Step 3: Again, click on “Commit changes”.

Commit master1.txt
Commit master1.txt

Step 4: “master1.txt” has been successfully created in “test”.

master1.txt created
master1.txt created

Step 5: Go to “Jenkins Dashboard” & click on “Prod”.

Prod Job Automatically Triggered
Prod Job Automatically Triggered

Step 6: Click on “#2”.

Build Automatically Created
Build Automatically Created

Step 7: Click on “Console Output”.

Check Console Output
Check Console Output
Prod Console Output
Prod Console Output
Commit Message Shown
Commit Message Shown

Step 8: Go to the “Prod” server & type the below-given command & press “Enter” from the keyboard. The “master1.txt” file has been successfully shown.

ls  
Trigger Pipeline & Fetch Content to Git Local
master1.txt shown on Local

More Jenkins Resources:

Integration of DevOps Tools with Jenkins — Jenkins Case Study

Create a Pipeline When One Job Is Successful Another Job Should Be Triggered Automatically — Jenkins Assignment 3

How to Install Jenkins on Ubuntu 24.04 in AWS EC2 Instance?

--

--