Trigger a Pipeline Using Git When Push on Develop Branch & Also Pull Git Content on Folder — Jenkins Assignment 1

Visal Tyagi
DevOps-Guides
Published in
15 min readMay 2, 2024

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

Trigger the Pipeline
Trigger the Pipeline

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”.

Search EC2
Search EC2

Step 2: Click on “Launch Instance”.

Launch Instance
Launch Instance

Step 3: Choose “Name” as “Jenkins-“ in the “Name and tags” section.

Instance Name
Instance Name

Step 4: Choose “AMI” as “Amazon Linux”.

Amazon Linux AMI
Amazon Linux AMI

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

t2.medium
t2.medium

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

Choose key pair
Choose key pair

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”.

Network Settings
Network Settings

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

Select Number of Instances
Select the Number of Instances

Step 9: Two Instances will be successfully launched. Click on “Instances” in “Breadcrumbs”.

Instances
Instances

Step 10: Rename the First “Instances” as “Jenkins-Master” & second one as “Jenkins-Slave”.

Jenkins Instances
Jenkins Instances
Rename Jenkins Instances
Rename Jenkins Instances

b. Install “Jenkins” Over the Master (Jenkins-Master) Machine

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

Select Jenkins-Master Instance
Select Jenkins-Master Instance

Step 2: Click on “Connect” Again.

Connect the Instance
Connect the Instance

Step 3: The “Jenkins-Master” machine will be opened. Run this command to update the machine:

sudo yum update
Update Command
Update Command

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.

Configure Jenkins Repo
Configure Jenkins Repo

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.

Import the Key
Import the Key

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.

Java Package
Java Package

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.

Install Jenkins
Install Jenkins

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.

Enable Jenkins
Enable Jenkins

Step 9: Now, start the “Jenkins” as a service:

sudo systemctl start jenkins

Press “Enter” from the keyboard to run this command.

Start Jenkins
Start Jenkins

Step 10: Now, check the “Jenkins service” status using this command:

sudo systemctl status jenkins

Press “Enter” from the keyboard to run this command.

Jenkins Status

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.

Unlock Jenkins
Unlock Jenkins

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

Copy the token
Copy the token

Step 13: Paste the given token in the “Administrator password” section. Click on “Continue”.

Paste the token
Paste the token

Step 14: Click on “Install Suggested Plugins”.

Install Suggested Plugins
Install Suggested Plugins

Step 15: Plugin will be started installing like this:

Plugin Installation Started
Plugin Installation Started

Step 16: Now, create your admin user. Put the following entries here:

Username: — admin

Password: — admin

Confirm Password: — admin

Create Admin User
Create Admin User

Full Name: — admin

E-mail Address: — admin@admin.com

Click on “Save and Continue”.

Save and Continue
Save and Continue

Step 17: Jenkins has been successfully installed. A configuration URL will be given. Click on “Save and Finish”.

Save and Finish
Save and Finish

Step 18: Click on “Start using Jenkins”.

Start Using Jenkins
Start Using Jenkins

Step 19: Your “Jenkins Dashboard” setup will be successfully done.

Jenkins Dashboard
Jenkins Dashboard

c. Install “Java” Over Slave (Jenkins-Slave) Machine

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

Jenkins-Slave
Jenkins-Slave

Step 2: Click on “Connect” Again.

Connect Master
Connect Master

Step 3: The “Jenkins-Slave” machine will be opened. Run this command to update the machine:

sudo yum update
Update the Slave Machine
Update the Slave Machine

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.

Java Installed
Java Installed

Step 5: “Java Installation” will be completed.

Java Installation Completed
Java Installation 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.

.ssh directory
.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.

Generate Public and Private Keys
Generate Public and Private Keys

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.

Copy the Public Key Content
Copy the Public Key Content

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.

.ssh directory
.ssh directory

Step 2: Do “ls” & “authorized_keys” will be displayed here in the .ssh directory.

authorized_keys
authorized_keys

Step 3: To edit the “authorized_keys”, use this command to open the authorized_keys file:

vim authorized_keys
edit authorized_keys file
edit authorized_keys file

Step 4: Paste the “id_rsa.pub” content here.

Paste the Public Key Content
Paste the Public Key Content

Step 5: Press “ESC” from the keyboard & type this command to save & quit the “authorized_keys” file.

wq!
Save and Quit
Save and Quit

Step 6: Your “authorized_keys” file will be successfully saved.

Save authorized_keys file
Save authorized_keys file

f. Add a Node in Jenkins-Master using “Private Key”

Step 1: Click on “Manage Jenkins”.

Manage Jenkins
Manage Jenkins

Step 2: Click on “Nodes”.

Nodes
Nodes

Step 3: Click on “New Node”.

Click on New Node
Click on New Node

Step 4: Choose “Node name” as “Slave-1” & “Type” as “Permanent Agent”. Click on “Create”.

Create Node
Create Node

Step 5: Put the following entries here:

Name: Slave1

Description: This is a slave machine.

Number of executors: 1

Choose Slave Name & Description
Choose Slave Name & Description

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 Label & Root Directory
Choose Label & Root Directory

Choose “Launch Method” as “Launch Agents via SSH”.

Host: — 172.31.40.47

Click on “Add” in “Credentials”.

Launch Agent Via SSH
Launch Agent Via SSH

Click on “Jenkins”.

Click Jenkins
Click 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

Choose ID & Description
Choose ID & Description

Username: — ec2-user

Choose “Enter Directly” in “Private Key”.

Click on “Add”.

Click on Add
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.

Copy Private Key Content
Copy Private Key Content

Go to “Jenkins” & paste the private key in the “Private Key” section.

Click on “Add”.

Add the Keys
Add the Keys

Choose “Credentials” as “ec2-user (pwdless)”.

While choosing “Host Key Verification Strategy” as “Non verifying Verification Strategy”.

Click on “Save”.

Save Credentials
Save Credentials

Step 6: Agent “Slave-1” has been successfully created in “Jenkins” using the “passwordless authentication”.

Slave 1 Node Created
Slave 1 Node Created

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”.

Slave Successfully Connected
Slave Successfully Connected

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.

Login into GitHub Account
Login into GitHub Account

Step 2: Click on “New”.

Click New
Click New

Step 3: Write “Repository name” as “Assignment-1-Jenkins”. Write “Description” as “Repository for Performing Assignment 1”.

Write the Repository Name Here
Write the Repository Name Here

Step 4: Click on “Create Repository”.

Create Repository
Create Repository

Step 5: Repository (“Assignment-1-Jenkins”) has been successfully created.

Repository Created
Repository Created

b. Create the “Main” & “Develop” Branches with .txt File

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

Create a New File
Create a New File

Step 2: Type the file name “main.txt” in the “main” branch.

main.txt file
main.txt file

Click on “Commit changes”.

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

Commit Changes
Commit Changes

Step 4: Your “main.txt” has been successfully created in the “main” branch.

main.txt
main.txt

Step 5: Click on “Main” & Go to “View all branches”.

View All Branches
View All Branches

Step 6: Click on “New branch”.

New Branch
New Branch

Step 7: Write “New branch name” as “develop”. Click on “Create new branch”.

Create New Branch
Create New Branch

Step 8: Click on “main”, and you will notice that a “develop” branch has been successfully created. Click on “Develop”.

develop branch
develop branch

Step 9: Click on the “+” (plus) sign. And, click on “Create new file”.

Create a new file
Create a new file

Step 10: Write the file name as “develop.txt” & click on “Commit changes”.

develop.txt
develop.txt

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

Commit Changes
Commit Changes

Step 12: “develop.txt” has been successfully created.

develop.txt created
develop.txt created

c. Create a Job in “Jenkins” to Trigger the Pipeline

Step 1: Click on “Create a job”.

Create a Job
Create a Job

Step 2: Write “Assignment1-Job” as “Enter an item name”. Choose the “Freestyle project” option here.

Create Freestyle Project
Create Freestyle Project

Step 3: It will redirect you to “General” in “Configure”. Write the Description as “This is for Assignment 1.”.

Write Description Here
Write Description Here

Step 4: Choose the following options here:

In “GitHub project”, choose “Project URL” as “https://github.com/visaltyagi/Assignment-1-Jenkins.git”.

Choose Git Hub Project
Choose Git Hub Project

Also, choose the “Restrict where this project can be run” as “Slave-1”.

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

Choose “Git”
Choose “Git”

Step 6: Put the “Repository URL” as “https://github.com/visaltyagi/Assignment-1-Jenkins”.

Click on Add
Click on Add

Click on “Add” in “Credentials”.

Step 7: Click on “Jenkins”.

Click Jenkins
Click 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

Choose username
Choose Username
Add Credentials

Click on “Add”.

Step 9: Choose “Credentials” as “visaltyagi12/******(PWDLESS).

Choose Credentials
Choose Credentials

Step 10: Choose “Branch Specifier (blank for ‘any’) is “*/develop” in “Branches to build”.

Branches to Build the Job
Branches to Build the Job

Step 11: In “Build Triggers”, choose “GitHub hook trigger for GITscm polling”.

Choose the GitHub Hook Trigger Option
Choose the GitHub Hook Trigger Option

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”.

Settings
Settings

Step 13: Click on “Webhooks”.

Webhooks
Webhooks

Step 14: Click on “Add webhook”.

Add Webhook
Add Webhook

Step 15: Put “Jenkins URL (http://13.233.77.171:8080/github-webhook/)” in the “Payload URL” section.

Put the Payload URL
Put the Payload URL

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”.

Click Add Webhook
Click Add Webhook

Step 17: Your webhook has been successfully created with “Right tick” confirmation.

Webhook Created
Webhook Created

Step 18: Now, go to your “Jenkins Job” section & click on “Save”.

Save Jenkins Job
Save Jenkins Job

Step 19: Your job (Assignment 1-Job) has been successfully created.

Job Created Successfully
Job Created Successfully

Step 20: Click on “Build Now”. It will have started creating the job. Click on “#3”.

Build Now
Build Now

Step 21: Your job has been successfully created. Click on “Console Output”.

Console Output
Console Output

Step 22: Your output has been shown successfully.

Console Output
Console Output

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.

Click on “+” sign
Click on “+” sign

Step 2: Click on “Create new file”.

Create new file
Create new file

Step 3: Type “develop1.txt” in the “develop” branch. Click on “Commit Changes”.

Commit Changes
Commit Changes

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

Commit Changes
Commit Changes

Step 5: “develop1.txt” has been successfully created.

develop1.txt
develop1.txt

Step 6: Go to Jenkins “Build History”, a pipeline will be automatically created as “#4”.

Automatically Created Pipeline
Automatically Created Pipeline

Step 7: Click on “#4” & go to the “Console Output” section.

Console Output
Console Output

Step 8: “develop1.txt” has been shown to you in “Console Output”.

develop1.txt created
develop1.txt created

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  
Jenkins-Slave
Jenkins-Slave

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
workspace folder
workspace folder

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
Assignment1-Job
Assignment1-Job

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
Go to “Assignment1-Job” Folder
Go to “Assignment1-Job” Folder

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

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?

--

--