Trigger a Pipeline Using Git When Push on Develop Branch & Also Pull Git Content on Folder — Jenkins Assignment 1
Tasks to Be Performed:
1. Trigger a pipeline using Git when pushing on the develop branch
2. Pipeline should pull Git content to a folder
Git Hub Repository for this Assignment to Copy the Commands:
Step 1: How to Install Jenkins on AWS EC2 Instance using Amazon Linux?
a. Launch Two Servers as “Jenkins-Master” & “Jenkins-Node” on “t2.medium” Machine
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: Choose “Name” as “Jenkins-“ in the “Name and tags” section.
Step 4: Choose “AMI” as “Amazon Linux”.
Step 5: Choose “Instance type” as “t2.medium”.
Step 6: Choose “key pair (login)” as “Jenkins2”.
Step 7: In “Network settings”, choose “Firewall (security groups)” as “Select existing security group”. While we have chosen “launch-wizard-9” as the “Common security groups”.
Step 8: In “Summary”, write “Number of Instances” as “2”. Click on “Launch Instance”.
Step 9: Two Instances will be successfully launched. Click on “Instances” in “Breadcrumbs”.
Step 10: Rename the First “Instances” as “Jenkins-Master” & second one as “Jenkins-Slave”.
b. Install “Jenkins” Over the Master (Jenkins-Master) Machine
Step 1: Select the “Jenkins-Master” & click on “Connect”.
Step 2: Click on “Connect” Again.
Step 3: The “Jenkins-Master” machine will be opened. Run this command to update the machine:
sudo yum update
Step 4: Now, we will start installing Jenkins on “AWS Amazon Linux 2”. Run this given command:
sudo wget -O /etc/yum.repos.d/jenkins.repo \
https://pkg.jenkins.io/redhat-stable/jenkins.repo
Press “Enter” from the keyboard to run this command.
Jenkins Repo will be configured on the machine.
Step 5: Now, import the key from Jenkins CI to enable installation from the given package.
Run this given command:
sudo rpm - import https://pkg.jenkins.io/redhat-stable/jenkins.io-2023.key
Press “Enter” from the keyboard to run this command.
Step 6: Run this command to install the “java” on “Amazon Linux 2”:
sudo dnf install java-17-amazon-corretto -y
Press “Enter” from the keyboard to run this command.
“Java Package” will be successfully installed.
Step 7: Now, run the command to install the “Jenkins” over “Amazon Linux 2”:
sudo yum install jenkins –y
Press “Enter” from the keyboard to run this command.
Step 8: Now, enable the Jenkins to start the boot. Run the given command:
sudo systemctl enable jenkins
Press “Enter” from the keyboard to run this command.
Step 9: Now, start the “Jenkins” as a service:
sudo systemctl start jenkins
Press “Enter” from the keyboard to run this command.
Step 10: Now, check the “Jenkins service” status using this command:
sudo systemctl status jenkins
Press “Enter” from the keyboard to run this command.
Step 11: Copy the “Public IP” of “Jenkins-Master”. Put in the browser address bar with port no 8080. The “Unlock Jenkins” option will be shown.
Copy the given command.
Step 12: Put the above-given command with sudo cat in “Jenkins-Master”.
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
Press “Enter” from the keyboard.
A token will be given. Copy this token:
40c6ab111d6243289a593c03e39f597d
Step 13: Paste the given token in the “Administrator password” section. Click on “Continue”.
Step 14: Click on “Install Suggested Plugins”.
Step 15: Plugin will be started installing like this:
Step 16: Now, create your admin user. 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 17: Jenkins has been successfully installed. A configuration URL will be given. Click on “Save and Finish”.
Step 18: Click on “Start using Jenkins”.
Step 19: Your “Jenkins Dashboard” setup will be successfully done.
c. Install “Java” Over Slave (Jenkins-Slave) Machine
Step 1: Select the “Jenkins-Slave” & click on “Connect”.
Step 2: Click on “Connect” Again.
Step 3: The “Jenkins-Slave” machine will be opened. Run this command to update the machine:
sudo yum update
Step 4: Run the command to install the “java” on “Amazon Linux 2”.
sudo dnf install java-17-amazon-corretto -y
Press “Enter” from the keyboard to run this command.
“Java Package” will be successfully installed.
Step 5: “Java Installation” will be completed.
d. Create Public & Private Key on “Jenkins-Master” to Add the “Slave” Machine
Step 1: First, go to the .ssh directory using this command: cd .ssh/
You will go to the “ssh” directory.
Step 2: Run this command to create “public” & “private” keys:
ssh-keygen
Press “Enter” from the keyboard & it will start generating the keys. Press “Enter” from the keyboard three times.
Your public key (id_rsa.pub) & private key (id_rsa) will be generated.
Step 3: Run this command to find the public key:
sudo cat id_rsa.pub
Your “public key” content will be shown here. Copy the content of the keys.
e. Paste the Public Key (id_rsa.pub) in “authorized_keys” file
Step 1: Go to the “Jenkins-Slave” machine. Run this command to go inside the ssh directory.
cd .ssh/
You will land inside the ssh directory.
Step 2: Do “ls” & “authorized_keys” will be displayed here in the .ssh directory.
Step 3: To edit the “authorized_keys”, use this command to open the authorized_keys file:
vim authorized_keys
Step 4: Paste the “id_rsa.pub” content here.
Step 5: Press “ESC” from the keyboard & type this command to save & quit the “authorized_keys” file.
wq!
Step 6: Your “authorized_keys” file will be successfully saved.
f. Add a Node in Jenkins-Master using “Private Key”
Step 1: Click on “Manage Jenkins”.
Step 2: Click on “Nodes”.
Step 3: Click on “New Node”.
Step 4: Choose “Node name” as “Slave-1” & “Type” as “Permanent Agent”. Click on “Create”.
Step 5: Put the following entries here:
Name: Slave1
Description: This is a slave machine.
Number of executors: 1
Choose where you want to save the “Jenkins” data. We have installed Jenkins over “ec2-user”.
While choosing “label” as “slave”. Because Jenkins identifies the node using the “label” feature.
Usage will remain as it is.
Choose “Launch Method” as “Launch Agents via SSH”.
Host: — 172.31.40.47
Click on “Add” in “Credentials”.
Click on “Jenkins”.
Choose the following entries here:
Domain: — Global credentials (unrestricted) — Remain as it is.
Kind: — SSH username with private key
Scope: — Global (Jenkins, nodes, items, all child items, etc)
Id & Description: — pwdless
Username: — ec2-user
Choose “Enter Directly” in “Private Key”.
Click on “Add”.
Paste the “Private Key” from “Master” here.
Go to “Jenkins-Master” & run this command: cat id_rsa.
Copy the private key from here.
Go to “Jenkins” & paste the private key in the “Private Key” section.
Click on “Add”.
Choose “Credentials” as “ec2-user (pwdless)”.
While choosing “Host Key Verification Strategy” as “Non verifying Verification Strategy”.
Click on “Save”.
Step 6: Agent “Slave-1” has been successfully created in “Jenkins” using the “passwordless authentication”.
g. Verifying Node is Created or Not
Condition: If the “slave-1” is successfully “SSH” from “Jenkins-Master” then it’s verified that “Slave-1” is successfully created because “Slave-1” has the “Slave Private IP Address”.
Step 1: Go to Master and type this command: ssh slave private ip address
ssh 172.31.40.47
Press “Enter” from the keyboard.
Type “Yes” & press “Enter” from the keyboard.
Your “Jenkins Slave” has been successfully accessed from “Jenkins Master”.
Problem (1) Solution: Trigger a pipeline using Git when pushing on the develop branch
a. First Create a Repository named “Assignment1”
Step 1: Login into your “GitHub” account.
Step 2: Click on “New”.
Step 3: Write “Repository name” as “Assignment-1-Jenkins”. Write “Description” as “Repository for Performing Assignment 1”.
Step 4: Click on “Create Repository”.
Step 5: Repository (“Assignment-1-Jenkins”) has been successfully created.
b. Create the “Main” & “Develop” Branches with .txt File
Step 1: Click on “Create a new file”.
Step 2: Type the file name “main.txt” in the “main” branch.
Click on “Commit changes”.
Step 3: Again, click on “Commit changes”.
Step 4: Your “main.txt” has been successfully created in the “main” branch.
Step 5: Click on “Main” & Go to “View all branches”.
Step 6: Click on “New branch”.
Step 7: Write “New branch name” as “develop”. Click on “Create new branch”.
Step 8: Click on “main”, and you will notice that a “develop” branch has been successfully created. Click on “Develop”.
Step 9: Click on the “+” (plus) sign. And, click on “Create new file”.
Step 10: Write the file name as “develop.txt” & click on “Commit changes”.
Step 11: Again, click on “Commit changes”.
Step 12: “develop.txt” has been successfully created.
c. Create a Job in “Jenkins” to Trigger the Pipeline
Step 1: Click on “Create a job”.
Step 2: Write “Assignment1-Job” as “Enter an item name”. Choose the “Freestyle project” option here.
Step 3: It will redirect you to “General” in “Configure”. Write the Description as “This is for Assignment 1.”.
Step 4: Choose the following options here:
In “GitHub project”, choose “Project URL” as “https://github.com/visaltyagi/Assignment-1-Jenkins.git”.
Also, choose the “Restrict where this project can be run” as “Slave-1”.
Step 5: Choose “Source Code Management” as “Git”.
Step 6: Put the “Repository URL” as “https://github.com/visaltyagi/Assignment-1-Jenkins”.
Click on “Add” in “Credentials”.
Step 7: Click on “Jenkins”.
Step 8: Choose the following option here:
Kind: Username with password
Username: visaltyagi12
Password: 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 (Paste this in this section)
ID & Description: PWDLESS
Click on “Add”.
Step 9: Choose “Credentials” as “visaltyagi12/******(PWDLESS).
Step 10: Choose “Branch Specifier (blank for ‘any’) is “*/develop” in “Branches to build”.
Step 11: In “Build Triggers”, choose “GitHub hook trigger for GITscm polling”.
We have chosen this option because when we will do the change in the “develop” branch in the “Github repo”, the changes will be automatically triggered by webhooks.
Step 12: Go to the “Settings” section in the “Assignment1-Jenkins” Repository in “Git”.
Step 13: Click on “Webhooks”.
Step 14: Click on “Add webhook”.
Step 15: Put “Jenkins URL (http://13.233.77.171:8080/github-webhook/)” in the “Payload URL” section.
Remain “Secret” & “Just the push event” in the “Which events would you like to trigger this webhook” section as it is.
Step 16: Click on “Add webhook”.
Step 17: Your webhook has been successfully created with “Right tick” confirmation.
Step 18: Now, go to your “Jenkins Job” section & click on “Save”.
Step 19: Your job (Assignment 1-Job) has been successfully created.
Step 20: Click on “Build Now”. It will have started creating the job. Click on “#3”.
Step 21: Your job has been successfully created. Click on “Console Output”.
Step 22: Your output has been shown successfully.
d. Test the Jenkins Pipeline (Check whether It is triggering or not after changing in Git automatically)
Step 1: Go to the “develop” branch & click on the “+” sign.
Step 2: Click on “Create new file”.
Step 3: Type “develop1.txt” in the “develop” branch. Click on “Commit Changes”.
Step 4: Again, click on “Commit changes”.
Step 5: “develop1.txt” has been successfully created.
Step 6: Go to Jenkins “Build History”, a pipeline will be automatically created as “#4”.
Step 7: Click on “#4” & go to the “Console Output” section.
Step 8: “develop1.txt” has been shown to you in “Console Output”.
Problem (2) Solution: Pipeline should pull Git content to a folder
Step 1: Go to the “Jenkins-Slave” machine. Type the below-given command here & you will notice that “Jenkins” will be present here.
ls
Step 2: Type the below-given command to go inside the “Jenkins” folder. Do “ls” here. A “Workspace” folder will be shown to you.
cd jenkins
Step 3: Type the below-given command to go inside this directory & do “ls”, you will notice that “Assignment1-Job” has been successfully shown to you.
cd workspace
Step 4: Type the below-given command to go inside this directory & do “ls”, you will notice that “main.txt” & “develop.txt” has been successfully shown to you.
cd Assignment1-Job
It means the pipeline has been successfully triggered with Git Content.
Check the Other Jenkins Assignments
Integration of DevOps Tools with Jenkins — Jenkins Case Study
Add 2 Nodes in Jenkins & Made Push to Branches & Pull Content to Git Folder — Jenkins Assignment 2