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

Visal Tyagi
DevOps-Guides
Published in
20 min readMay 8, 2024

Tasks to Be Performed:

1. Create a pipeline in Jenkins

2. Once a push is made to “develop” a branch in Git, trigger job “test”. This will copy Git files to test node

3. If the test job is successful, then the prod job should be triggered

4. Prod jobs should copy files to the prod node.

Jenkins Assignment 3 Pipeline Triggering
Jenkins Assignment 3 Pipeline Triggering

Github Repository for copying the command for Assignment 3, click below repository:

1. Create Jenkins Master & Two Jenkins Slave Machine

A. Create Three Amazon EC2 Instances Named As Jenkins Master & Jenkins Slave1 & Jenkins Slave2

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 “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 “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 “3”. Click on “Launch Instance”.

Launch Instance
Launch Instance

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

Instances Launched
Instances Launched

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

Instances Launched
Instances Launched
Rename Jenkins Instance
Rename Jenkins Instance

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 Jenkins-Master Instance
Connect Jenkins-Master Instance

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

sudo apt-get update
Update Master Machine
Update Master Machine

Step 4: Now, we will install Jenkins on the “Ubuntu” machine. Run this given command:

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

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, append the Debian package repository to the server’s sources.list

Run this given command:

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

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

Add Debian Repository
Add Debian Repository

Step 6: Run this command to install the “java” on “Ubuntu Machine”:

sudo apt install fontconfig openjdk-17-jre

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

Install OpenJdk
Install OpenJDK

Press “Y” to execute the “Openjdk “ installation.

Press “Y”
Press “Y”

“Java Package” will be successfully installed.

Step 7: First, update the machine & run the below-given commands to update & install the “Jenkins” over the “ubuntu machine.”:

sudo apt-get update
sudo apt-get install jenkins -y

Press “Enter” from the keyboard to run these commands.

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

Step 11: Copy the “Public IP” of “Jenkins-Master”. Put in the browser address bar with port no 8080 [http://18.117.152.173:8080/]. The “Unlock Jenkins” option will be shown.

Copy the given command
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:

46704a6d167143f392b12f89b876727a

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 Slaves (Test & Prod) Machine

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

Select Test Instance
Select Test Instance

Step 2: Click on “Connect” Again.

Connect the Test Machine
Connect the Test Machine

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

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

Step 4: Run the command to install “Java” on “Ubuntu”.

sudo apt install fontconfig openjdk-17-jre

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

Install Java on Test
Install Java on the Test

Type “Y” to install java.

Type “Y”
Type “Y”

“Java Package” will be successfully installed.

Step 5: Run this command to check the Java version:

java --version
Check the Java Version
Check the Java Version

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

Select the Prod Instance
Select the Prod Instance

Step 7: Click on “Connect” Again.

Connect the Prod Instance
Connect the Prod Instance

Step 8: The “Prod” machine will be opened. Run this command to update the machine:

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

Step 9: Run the command to install the “java” on “Ubuntu”.

sudo apt install fontconfig openjdk-17-jre

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

Install OpenJDK on Prod
Install OpenJDK on Prod

Type “Y” to install java.

Type “Y”
Type “Y”

“Java Package” will be successfully installed.

Step 10: Run this command to check the Java version:

java --version
Check the Java Version
Check the Java Version on “Prod”

d. Add Nodes (Test & Prod) in Jenkins-Master using .pem

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 “Test-Node” & “Type” as “Permanent Agent”. Click on “Create”.

Create Test Node Here
Create Test Node Here

Step 5: Put the following entries here:

Name: Test-Node

Description: This is a test-node machine.

Number of executors: 1

Choose Test Node Description Here
Choose Test Node Description Here

Choose where you want to save the “Jenkins” data. We have installed Jenkins over “ubuntu”.

While choosing “label” as “test”. Because Jenkins identifies the node using the “label” feature.

Usage will remain as it is.

Choose Root directory & Label here
Choose Root directory & Label here

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

Host: — 172.31.0.185

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: — ubuntu

Choose “Enter Directly” in “Private Key”.

Click on “Add”.

Click on Add
Click on Add

Copy the “Jenkins2.pem” content from here.

Jenkins Key
Jenkins Key

Paste the .pem key in “Enter Directly” in the “Private Key” section.

Add PEM Key
Add PEM Key

Choose “Credentials” as “ubuntu (pwdlesss)”.

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

Click on “Save”.

Save Credentials
Save Credentials

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

Test Node Added
Test Node Added

Step 7: Click on “New Node”.

Click on New Node
Click on New Node

Step 8: Choose “Node name” as “Prod-Node” & Choose “Copy Existing Node”. Select “Test-Node” here & click on “Create”

Create Prod-Node
Create Prod-Node

Step 9: Put the following entries here:

Name: Prod-Node

Description: This is a prod-node machine.

Number of executors: 1

Write Prod-Node Description Here
Write Prod-Node Description Here

Choose where you want to save the “Jenkins” data. We have installed Jenkins over “ubuntu”.

While choosing “label” as “prod”. Because Jenkins identifies the node using the “label” feature.

Usage will remain as it is.

Choose Label Here
Choose Label Here

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

Host: — 172.31.4.202

Click on “Add” in “Credentials”.

Put Prod IP Address Here
Put Prod IP Address Here

Click on “Save”.

Save Credentials
Save Credentials

Step 10: Agent “Prod-Node” has been successfully created in “Jenkins” using the “passwordless authentication”.

Prod Node Added
Prod Node Added

2. Create a Separate Git Hub Repository for this Assignment as “Assignment-3-Jenkins

A. First Create a Repository named “Assignment-3-Jenkins

Step 1: Login into your “GitHub” account.

Login to GitHub Account
Login to GitHub Account

Step 2: Click on “New”.

Click New
Click New

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

Put the Repository Name & Description here
Put the Repository Name & Description here

Step 4: Click on “Create Repository”.

Create Repository
Create Repository

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

Repository Created
Repository Created

B. Create a WebHook To Trigger the Pipeline Automatically

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

Go to Settings
Go to 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://18.117.152.173: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”.

Go to Recent Delieveries
Go to Recent Deliveries

Step 7: Delivery has been successfully pinged.

Delivery Pinged
Delivery Pinged

c. Create the Two Branches Name “develop” & “dev1” branch

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

Go to “View all branches”
Go to “View all branches”

Step 2: Click on “New branch”.

New Branch
New Branch

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

Create New Branch
Create New Branch

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

develop branch
develop branch

Step 5: Again, click on “New branch”.

Create a new branch
Create a new branch

Step 6: Choose the “New branch name” as “dev1” & click on “Create new branch”.

Create dev1 branch
Create dev1 branch

Step 7: All branches will be successfully created.

Branches
Branches

d. Create the two files named “develop.txt” & “dev1.txt”

Step 1: Click on “Code”.

Go to Code Section
Go to Code Section

Step 2: Go to the “develop” branch.

Go to develop branch
Go to develop branch
develop branch
develop branch

Step 3: Click on “Add File > Create a new file”.

Create a New File
Create a New File

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

develop.txt
develop.txt
Click on Commit Changes
Click on Commit Changes

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

Commit Changes
Commit Changes

Step 6: The “develop.txt” has been successfully created.

develop.txt created
develop.txt created

Step 2: Go to the “develop” branch.

Go to develop branch
Go to develop branch
develop branch
develop branch

Step 3: Click “Add File > Create a new file”.

Create a New File
Create a New File

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

develop.txt
develop.txt
Click on Commit Changes
Click on Commit Changes

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

Commit Changes
Commit Changes

Step 6: The “develop.txt” file has been successfully created.

develop.txt created
develop.txt created

Step 7: Go to the “dev1” branch.

Click “dev1” branch
Click “dev1” branch
dev1 branch
dev1 branch

Step 8: Click on “Add File > Create a new file”.

Create a New File
Create a New File

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

Put dev1.txt
Put dev1.txt
Click on Commit Changes
Click on Commit Changes

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

Commit Changes
Commit Changes

Step 11: The “dev1.txt” file has been successfully created.

dev1.txt created
dev1.txt created

Problem (2) Solution: Once a push is made to “develop” a branch in Git, trigger job “test”. This will copy Git files to test node

A. Create a Test Job & Put “develop” Branch Path Here

Step 1: Click on “New Item”.

Go to New Item
Go to New Item

Step 2: Choose the following options here:

Enter an item name: Test-Job

Choose “Freestyle project” here.

Click on “OK”.

Create Test-Job
Create Test-Job

Step 3: In “General”, choose “Description” as “If we commit in the “develop” branch in “GitHub” then this job should be triggered.”.

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

Choose Repo URL Here
Choose Repo URL Here

Step 4: Choose “Restrict where this project can be run” as “Test”.

Choose Restricted Node
Choose Restricted Node

Step 5: Choose the following options here:

Choose “Git” Here.

Put “Repository URL” as https://github.com/visaltyagi/Assignment-3-Jenkins.git.

Choose Git as “SCM”
Choose Git as “SCM”

Step 6: Choose “Branch Specifier” (blank for ‘any’) as “*/develop”.

Choose to develop branch specifier
Choose to develop branch specifier

Step 7: Choose “Build Triggers” as “GitHub hook trigger for GITScm polling”. Click on “Apply>Save”.

Choose GitHub Hook Trigger
Choose GitHub Hook Trigger

Step 8: The “Test-Job” will be successfully created.

Test Job
Test Job

B. Commit in the “Develop” Branch & “Test” Job Should Be Automatically Triggered

Step 1: Go to “Git-Hub Account” & click on “main”. Select the “develop” branch.

Go to the develop branch
Go to the develop branch

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

Create new file
Create new file

Step 3: Put the file name as “develop1.txt” & description as “This is a develop1 file”. Click on “Commit changes”.

Create develop1.txt file
Create develop1.txt file
Commit develop1 file
Commit develop1 file

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

Again, Commit develop1.txt file
Again, Commit develop1.txt file

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

develop1.txt file
develop1.txt file

Step 6: Go to “Jenkins Dashboard” & “Test” job will be automatically triggered. Click on “#1”.

Build Created Automatically
Build Created Automatically

Step 7: Click on “Console Output”.

Click Console Output
Click Console Output

Step 8: The “develop1.txt” has been successfully created. It means when we make changes in the “develop” branch, the “Test” job will be successfully triggered.

Console Output Message
Console Output Message

C. After Triggering the Job, Check Out that “develop1.txt” Successfully Copied on “Test Node” Automatically

Step 1: Go to the “Test” instance. Paste this command: cd /home/ubuntu/jenkins/workspace/Test-Job.

develop1 files
develop1 files

Do “ls” here & you will find that “develop1.txt” has been successfully shown here.

Problem (3) Solution: If the test job is successful, then the prod job should be triggered

A. Create a “Prod” Job & Put the “dev1” Branch Here

Step 1: Go to “Dashboard”.

Go to Dashboard
Go to Dashboard

Step 2: Click on “New Item”.

Go to “New Item”
Go to “New Item”

Step 3: Choose the following options here:

Enter an item name: Prod-Job

Choose “Freestyle project” here.

Choose Prod Job Name & Description
Choose Prod Job Name & Description

Step 4: In “Copy from”, choose “Test-Job”. Click on “OK”.

Copy Test-Job
Copy Test-Job

Step 5: Choose “Description” as ‘Triggered “Prod” Job after “Test” triggered.’

Write Prod-Job Description Here
Write Prod-Job Description Here

Step 6: Choose “Restrict where this project can be run” as “Prod-Node”.

Select Prod-Node
Select Prod-Node

Step 7: Choose “Branch Specifier (blank for ‘any’)” as “*/dev1”.

write dev1 branch here
write the dev1 branch here

Step 8: Click on “Apply > Save”.

Save Prod Job
Save Prod Job

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

Prod Job Created
Prod Job Created

B. Now, Add the “Prod” Job to “Test” for Automatically Triggering

Step 1: Click on the “Test-Job”.

Step 2: Click on “Configure”.

Go to Configure
Go to Configure

Step 3: Click on “Post-build Actions>Build other projects”.

Go to “Build Other Project”
Go to “Build Other Project”

Step 4: Choose “Projects to build” as “Prod-Job” & click on “Apply > Save”.

Save Prod-Job Here
Save Prod-Job Here

Step 5: The downstream project is shown as a “Prod-Job” in the “Test-Job”.

Prod-Job as Downstream Project
Prod-Job as Downstream Project

C. Now, Delete the “develop1.txt” file from GitHub to Trigger the Prod-Job via “Test-Job”.

When we trigger the “Test-Job” Successfully, the “Prod-Job” should be triggered & “dev1.txt” file from the “dev1” Branch will be successfully copied to the “Prod-Node”.

Step 1: Go to “GitHub Account” & click on “develop”.

develop branch
Go to “develop” branch

Step 2: Click on “develop1.txt”.

Go to “develop1.txt”
Go to “develop1.txt”

Step 3: Click on “Three Horizontal Dots>Delete File”.

Delete develop1.txt file
Delete develop1.txt file

Step 4: Click on “Commit changes”.

Do Commit Change to delete develop1.txt
Do Commit Change to delete develop1.txt

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

Commit develop1.txt
Commit develop1.txt

Step 6: Go to the “Test-Job” & you will notice that “Build #2” has been successfully build. Click on “#2”.

Build#2 Created
Build#2 Created

Step 7: Click on “Console Output”.

Go to “Console Output”
Go to “Console Output”

Step 8: The “develop1.txt” has been successfully deleted.

delete develop1.txt message
delete develop1.txt message

Step 9: Go to “Dashboard”.

Go to Dashboard
Go to Dashboard

Step 10: Click on “Prod-Job”.

Prod-Job
Prod-Job

Step 11: “Prod-Job” has been triggered after the “Test-Job. “A build” has been successfully created. Click on “#2”.

Go to “Build #2”
Go to “Build #2”

Step 12: It will show the last “Commit Message” here because we don’t have any changes in “Prod”. But it will copy the “dev1.txt” file to the “Prod” node.

Prod-Job Triggered
Prod-Job Triggered

Problem (4) Solution: Prod jobs should copy files to the prod node.

Step 1: Go to the “Prod” Instance. Run this command: cd /home/ubuntu/jenkins/workspace/Prod-Job.

dev1.txt file on Prod Instance
dev1.txt file on Prod Instance

Do “ls” here & “dev1.txt” has been successfully shown on the “Prod-Node”.

Note: We have created Prod-Node on the “Prod” instance, while “Test-Node” on the “Test” Instance.

Problem (1) Solution: Create a Pipeline in Jenkins

Step 1: Click on the “+” sign.

Click on “+” Sign
Click on “+” Sign

Step 2: Choose Name as “Assignment-3-Pipeline”. While choosing the type as “Build Pipeline View”.

Create Pipeline
Create Pipeline

Click on “Create”.

Step 3: Choose “Description” as “Assignment-3-Pipeline Testing”.

Write Pipeline Description Here
Write Pipeline Description Here

Step 4: Choose “Build Pipeline View Title” as “Assignment-3-Pipeline”.

Build Pipeline View Title
Build Pipeline View Title

Step 5: Select “Initial Job” as “Test-Job”.

Choose “Test-Job”
Choose “Test-Job”

Step 6: Click on “Apply>OK”.

Click OK
Click OK

Step 7: Pipeline has been successfully created & “Test-Job” is successfully triggering the “Prod-Job”.

Pipeline Successfully Build
Pipeline Successfully Build

Step 8: To check out the pipeline for advance proof, go to “Build Triggers” & unselect the “GitHub hook trigger for GITScm polling” options in both “Test-Job” & “Prod-Job”.

Unselect Webhook Trigger Option
Unselect Webhook Trigger Option

Step 9: Again, go to the “develop” branch & create a “develop.txt” file here.

Go to “Git-Hub Account” & click on “main”. Select the “develop” branch.

Go to develop branch
Go to develop branch

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

Create a new file
Create a new file

Step 11: Put the file name as “develop.txt” & description as “This is a develop1 file”. Click on “Commit changes”.

Create develop1.txt
Create develop1.txt
Click to Commit Changes
Click to Commit Changes

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

Commit develop1.txt
Commit develop1.txt

Step 13: The “develop1.txt” has been successfully created.

develop1.txt created
develop1.txt created

Step 14: Again, click on the “Run button” in “Test” in “Build Pipeline”.

Run the pipeline again
Run the pipeline again
Jenkins Assignment 3 Pipeline Triggering
Pipeline Start Running

Step 15: Now, the build is successfully created. In Test “#3” & in “Prod”, “#3” new builds will be created. This means, our pipeline is triggering the job successfully.

Job Triggered Successfully
Job Triggered Successfully

Step 16: Go to “Test-Job”, and click on “#3”.

Go to Build #3

A separate page will be opened & click on “Console Output”.

Go to Console Output
Go to Console Output

Step 17: The “develop1.txt” has been successfully created.

Create develop1 & trigger prod job
Create develop1 & trigger prod job

Step 18: Go to the “Test” instance. Paste this command: cd /home/ubuntu/jenkins/workspace/Test-Job.

develop1.txt on test server
develop1.txt on the test server

Do “ls” here & you will find that “develop1.txt” has been successfully shown here.

Step 19: Go to the “Prod-Job” & “Build #3” has been successfully created. Show, No Changes here. But we don’t make any changes here. So that’s why no change has happened here.

Click on “#3 Prod Job”
Click on “#3 Prod Job”
Build #3
Build #3

Step 20: When we go to prod & check the files. The same “dev1.txt” has been shown.

dev1 file
dev1 file

More Jenkins Resources:

Integration of DevOps Tools with Jenkins — Jenkins Case Study

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

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

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

--

--