Add 2 Nodes in Jenkins & Made Push to Branches & Pull Content to Git Folder — Jenkins Assignment 2
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.
Git Hub Repository Link for Copying All Commands From Here:
Before checking this assignment solution, kindly check the “Jenkins 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”.
Step 2: Click on “Launch Instance”.
Step 3: Write “Jenkins-Master” in “Name” under the “Name and tags” section.
Step 4: Choose “AMI” as “ubuntu”.
Step 5: Choose “Instance type” as “t2.micro”.
Step 6: Choose “key pair (login)” as “Jenkins2”.
Step 7: In “Network settings”, choose the following options:
Firewall (security groups): — Select existing security group
Common security groups: — launch-wizard-9
Step 8: In “Summary”, put “Number of Instances” as 3. Click on “Launch Instance”.
Step 9: The instance will be successfully created. Click on “Instances” in “Breadcrumbs”.
Step 10: Your “3 Instances” has been successfully run.
Step 11: Rename the second instance as “Test”. Click on “Right Arrow” & instance will be saved as “Test”.
Step 12: Rename the third instance as “Prod”. Click on “Right Arrow” & instance will be saved as “Prod”.
Step 13: Your three instances have been successfully saved as “Jenkins-Master”, “Test” & “Prod”.
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
Step 2: Type “Y” & “Java Installation” and start running.
Step 3: Type this command to check the “java version”.
java --version
c. Install “Jenkins” Over “Jenkins-Master”
Step 1: Select the “Jenkins-Master” Instance & click on “Connect”.
Step 2: Click on “Connect” Again.
Step 3: First, update the machine using this command:
sudo apt-get update
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
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
Step 6: Run the below-given update command again & press “Enter” from the keyboard.
sudo apt-get update
Step 7: Type this command to install the “Jenkins”:
sudo apt-get install jenkins
Step 8: Type “Y” & it will start continuing to install “Jenkins”.
Step 9: To start Jenkins, run the below-given command & press “Enter” from the keyboard.
sudo systemctl enable jenkins
Step 10: To start Jenkins, run the below-given command & press “Enter” from the keyboard.
sudo systemctl start jenkins
Step 11: To view the status, type the below-given command & press “Enter” from the keyboard.
sudo systemctl status jenkins
Step 12: Type this URL (http://52.66.23.183:8080/login) & press “Enter” http://52.66.23.183:8080/login keyboard.
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.
Step 14: Paste the token in the “Unlock Jenkins” section & click on “Continue”.
Step 15: Click on “Install Suggested Plugins”.
Step 16: It will start setting up the needed “Jenkins” plugin.
Step 17: Put the following entries here:
Username: admin
Password: admin
Confirm Password: admin
Full name: admin
E-mail address: admin@admin.com
Click on “Save and continue”.
Step 18: Click on “Save and Finish”.
Step 19: Click on “Start using Jenkins”.
Step 20: “Jenkins Dashboard” will be ready.
c. Install “Java” on “Test Server”.
Step 1: Select the “Test Instance” & click on “Connect”.
Step 2: Again, click on “Connect”.
Step 3: First, update the machine using the below-given command & Press “Enter” from the keyboard.
sudo apt-get update
Step 4: Paste the below-given command over “Test” & press “Enter” from the keyboard.
sudo apt install fontconfig openjdk-17-jre
Step 5: Type “Y” & “Java Installation” and start running.
Step 6: Type the below-given command to ’check’ the “java version”.
java --version
d. Install “Java” on “Prod Server”.
Step 1: Select the “Prod Instance” & click on “Connect”.
Step 2: Again, click on “Connect”.
Step 3: First, update the machine using the below-given command & Press “Enter” from the keyboard.
sudo apt-get update
Step 4: Paste this command over “Prod” & press “Enter” from the keyboard.
sudo apt install fontconfig openjdk-17-jre
Step 5: Type “Y” & “Java Installation” and start running.
Step 6: Type the below-given command to check the “java version”:
java --version
e. Add “Test Server” to Jenkins Master
Step 1: Go to “Jenkins Master” & click on “New Node”.
Step 2: Write “Node name” as “Test”. Choose “Type” as “Permanent Agent”.
Click on “Create”.
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
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”.
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
In “Private Key”, choose “Enter Directly”. Click on “Add”.
Now, paste the. pem key content here. We have taken the pem key as “Jenkins2”.
Click on “Add”.
Step 4: Choose “Credentials” as “ec2-user (PEM)”.
Choose “Host Key Verification Strategy” as “Non verifying Verification Strategy”.
Click on “Save”.
Step 5: “Test Node” has been successfully launched like this:
f. Add “Prod Server” to Jenkins Master
Step 1: Go to “Jenkins Master” & click on “New Node”.
Step 2: Write “Node name” as “Prod”. Choose “Type” as “Copy Existing Node”. In “Copy Existing Node”, choose “Test”.
Click on “Create”.
Step 3: Fill the following options here:
Description: This is for the prod node
Labels: Prod
Host: 172.31.40.194
Remain all entries are as it is.
Click on “Save”.
Step 4: The “Prod” Node has been successfully launched.
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.
Step 2: Choose the following options to create a repository.
Repository name: — Assignment-2-Jenkins
Description (optional): — Jenkins Assignment 2 Repository
Choose “Public” Option.
Click on “Create Repository”.
Step 3: Repository has been created as “visaltyagi / Assignment-2-Jenkins”.
b. Rename automatically creates the “main” branch as “master” & creates a file inside it
Step 1: Click on “Creating a new file”.
Step 2: You will notice that it is asking you to create a file in the “main” branch. Click on “main” for renaming.
Step 3: You will land inside the “main” branch. Click on “1 Branch”.
Step 4: Click on “Three Vertical Dots”.
Step 5: Click on “Rename Branch”.
Step 6: Rename “main” as “master”. Click on “Rename branch”.
Step 7: The branch will be successfully renamed as “master”.
Step 8: Now, we will create a file. Click on “Add File”.
Step 9: Click on “Create new file”.
Step 10: Write “master.txt” as the file name & write “description” as “This is a master file”.
Step 11: Click on “Commit changes”.
Step 12: Again, click on “Commit changes”.
Step 13: A “master.txt” file has been successfully created.
c. Create a “Test” Branch & Create a File Inside It
Step 1: Click on “Master>View All Branches”.
Step 2: Click on “New branch”.
Step 3: Write “New branch name” as “Test”. Remain “Source” as “Test”.
Step 4: The “Test” branch has been successfully created.
Step 5: Click on “Test” & click on “Add file”.
Step 6: Click on “Create new file”.
Step 7: Write the file name as “Test.txt” & choose “Description” as “This is a test file”.
Step 8: Click on “Commit changes”.
Step 9: Again, click on “Commit changes”.
Step 10: The “test.txt” file has been successfully created.
Problem 2 (a) Solution: Create a Job for Pushing to Test Repo
Step 1: Click on “New Item”.
Step 2: “Enter an Item Name” as “Test” & Choose “Freestyle project”.
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 “Restrict where this project can be run” & Select “Test” as “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”.,
Step 5: Choose the option “GitHub hook trigger for GITScm polling” to add “Webhook”. Click on “Save”.
Step 5: The “Test” job has been successfully created.
Step 6: Click “Build Now” to deploy the “Test” Repository File Here.
Step 7: “Build” has been successfully created. Click on Build #1.
Step 8: Go to “Console Output”. And the pipeline will be successfully created with “test.txt”.
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
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”.
Step 2: “Enter an Item Name” as “Prod” & Choose “Freestyle project”.
Click on “OK”.
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 “Prod” as “Label Expression” in “Restrict where the project can be run”.
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 “*/master” in “Branches to build”.
Step 5: Choose “GitHub hook trigger for GITScm polling” & Click on “Save”.
Step 6: The “Prod” job has been successfully created.
Step 7: Click “Build Now” to deploy the “Prod” Repository File Here.
Step 8: “Build” has been successfully created. Click on Build #1.
Step 9: Go to “Console Output”. And the pipeline will be successfully created with “master.txt”.
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
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.
Step 2: Click on “Wehbhooks”.
Step 3: Click on “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”.
Step 5: Click on “Webhook URL”.
Step 6: Click on “Recent Deliveries”.
Step 7: Delivery has been successfully 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”.
Step 2: Write the file name as “test1.txt” & write a description as “This is test1 file”. Click on “Commit Changes”.
Step 3: Again, click on “Commit changes”.
Step 4: “test1.txt” has been successfully created in “test”.
Step 5: Go to “Jenkins Dashboard” & click on “Test”.
Step 6: Click on “#4”.
Step 7: Click on “Console Output”.
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
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”.
Step 2: Write the file name as “master1.txt” & write a description as “This is master1 file”. Click on “Commit Changes”.
Step 3: Again, click on “Commit changes”.
Step 4: “master1.txt” has been successfully created in “test”.
Step 5: Go to “Jenkins Dashboard” & click on “Prod”.
Step 6: Click on “#2”.
Step 7: Click on “Console Output”.
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
More Jenkins Resources:
Integration of DevOps Tools with Jenkins — Jenkins Case Study