Create a Pipeline When One Job Is Successful Another Job Should Be Triggered Automatically— Jenkins Assignment 3
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.
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”.
Step 2: Click on “Launch Instance”.
Step 3: Choose “Name” as “Jenkins-“ in 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 “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 “3”. Click on “Launch Instance”.
Step 9: Three Instances will be successfully launched. Click on “Instances” in “Breadcrumbs”.
Step 10: Rename the First “Instances” as “Jenkins-Master” & the second one as “Test” & the third one as “Prod”.
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 apt-get update
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.
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.
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.
Press “Y” to execute the “Openjdk “ installation.
“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.
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 [http://18.117.152.173:8080/]. The “Unlock Jenkins” option will be shown.
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
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 Slaves (Test & Prod) Machine
Step 1: Select the “Test” & click on “Connect”.
Step 2: Click on “Connect” Again.
Step 3: The “Test” machine will be opened. Run this command to update the machine:
sudo apt-get update
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.
Type “Y” to install java.
“Java Package” will be successfully installed.
Step 5: Run this command to check the Java version:
java --version
Step 6: Select the “Prod” & click on “Connect”.
Step 7: Click on “Connect” Again.
Step 8: The “Prod” machine will be opened. Run this command to update the machine:
sudo apt-get update
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.
Type “Y” to install java.
“Java Package” will be successfully installed.
Step 10: Run this command to check the Java version:
java --version
d. Add Nodes (Test & Prod) in Jenkins-Master using .pem
Step 1: Click on “Manage Jenkins”.
Step 2: Click on “Nodes”.
Step 3: Click on “New Node”.
Step 4: Choose “Node name” as “Test-Node” & “Type” as “Permanent Agent”. Click on “Create”.
Step 5: Put the following entries here:
Name: Test-Node
Description: This is a test-node machine.
Number of executors: 1
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 “Launch Method” as “Launch Agents via SSH”.
Host: — 172.31.0.185
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: — ubuntu
Choose “Enter Directly” in “Private Key”.
Click on “Add”.
Copy the “Jenkins2.pem” content from here.
Paste the .pem key in “Enter Directly” in the “Private Key” section.
Choose “Credentials” as “ubuntu (pwdlesss)”.
While choosing “Host Key Verification Strategy” as “Non verifying Verification Strategy”.
Click on “Save”.
Step 6: Agent “Test-Node” has been successfully created in “Jenkins” using the “passwordless authentication”.
Step 7: Click on “New Node”.
Step 8: Choose “Node name” as “Prod-Node” & Choose “Copy Existing Node”. Select “Test-Node” here & click on “Create”
Step 9: Put the following entries here:
Name: Prod-Node
Description: This is a prod-node machine.
Number of executors: 1
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 “Launch Method” as “Launch Agents via SSH”.
Host: — 172.31.4.202
Click on “Add” in “Credentials”.
Click on “Save”.
Step 10: Agent “Prod-Node” has been successfully created in “Jenkins” using the “passwordless authentication”.
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.
Step 2: Click on “New”.
Step 3: Write “Repository name” as “Assignment-3-Jenkins”. Write “Description” as “Repository for Performing Assignment 3”.
Step 4: Click on “Create Repository”.
Step 5: Repository (“Assignment-3-Jenkins”) has been successfully created.
B. Create a WebHook To Trigger the Pipeline Automatically
Step 1: Go to “Settings” in the “Assignment-3-Jenkins” Repository.
Step 2: Click on “Wehbhooks”.
Step 3: Click on “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”.
Step 5: Click on “Webhook URL”.
Step 6: Click on “Recent Deliveries”.
Step 7: Delivery has been successfully pinged.
c. Create the Two Branches Name “develop” & “dev1” branch
Step 1: Click on “Main” & Go to “View all branches”.
Step 2: Click on “New branch”.
Step 3: Write “New branch name” as “develop”. Click on “Create new branch”.
Step 4: The “develop” branch has been successfully created.
Step 5: Again, click on “New branch”.
Step 6: Choose the “New branch name” as “dev1” & click on “Create new branch”.
Step 7: All branches will be successfully created.
d. Create the two files named “develop.txt” & “dev1.txt”
Step 1: Click on “Code”.
Step 2: Go to the “develop” branch.
Step 3: Click on “Add File > Create a new file”.
Step 4: Write the file name as “develop.txt” & click on “Commit changes”.
Step 5: Again, click on “Commit changes”.
Step 6: The “develop.txt” has been successfully created.
Step 2: Go to the “develop” branch.
Step 3: Click “Add File > Create a new file”.
Step 4: Write the file name as “develop.txt” & click on “Commit changes”.
Step 5: Again, click on “Commit changes”.
Step 6: The “develop.txt” file has been successfully created.
Step 7: Go to the “dev1” branch.
Step 8: Click on “Add File > Create a new file”.
Step 9: Write the file name as “dev1.txt” & click on “Commit changes”.
Step 10: Again, click on “Commit changes”.
Step 11: The “dev1.txt” file has been successfully 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”.
Step 2: Choose the following options here:
Enter an item name: Test-Job
Choose “Freestyle project” here.
Click on “OK”.
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”.
Step 4: Choose “Restrict where this project can be run” as “Test”.
Step 5: Choose the following options here:
Choose “Git” Here.
Put “Repository URL” as https://github.com/visaltyagi/Assignment-3-Jenkins.git.
Step 6: Choose “Branch Specifier” (blank for ‘any’) as “*/develop”.
Step 7: Choose “Build Triggers” as “GitHub hook trigger for GITScm polling”. Click on “Apply>Save”.
Step 8: The “Test-Job” will be successfully created.
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.
Step 2: Click on the “+” sign & Again, click on “Create new file”.
Step 3: Put the file name as “develop1.txt” & description as “This is a develop1 file”. Click on “Commit changes”.
Step 4: Again, click on “Commit changes”.
Step 5: The “develop1.txt” has been successfully created.
Step 6: Go to “Jenkins Dashboard” & “Test” job will be automatically triggered. Click on “#1”.
Step 7: Click on “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.
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.
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”.
Step 2: Click on “New Item”.
Step 3: Choose the following options here:
Enter an item name: Prod-Job
Choose “Freestyle project” here.
Step 4: In “Copy from”, choose “Test-Job”. Click on “OK”.
Step 5: Choose “Description” as ‘Triggered “Prod” Job after “Test” triggered.’
Step 6: Choose “Restrict where this project can be run” as “Prod-Node”.
Step 7: Choose “Branch Specifier (blank for ‘any’)” as “*/dev1”.
Step 8: Click on “Apply > Save”.
Step 9: The “Prod” job has been successfully created.
B. Now, Add the “Prod” Job to “Test” for Automatically Triggering
Step 1: Click on the “Test-Job”.
Step 2: Click on “Configure”.
Step 3: Click on “Post-build Actions>Build other projects”.
Step 4: Choose “Projects to build” as “Prod-Job” & click on “Apply > Save”.
Step 5: The downstream project is shown as a “Prod-Job” in the “Test-Job”.
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”.
Step 2: Click on “develop1.txt”.
Step 3: Click on “Three Horizontal Dots>Delete File”.
Step 4: Click on “Commit changes”.
Step 5: Again, click on “Commit changes”.
Step 6: Go to the “Test-Job” & you will notice that “Build #2” has been successfully build. Click on “#2”.
Step 7: Click on “Console Output”.
Step 8: The “develop1.txt” has been successfully deleted.
Step 9: Go to “Dashboard”.
Step 10: Click on “Prod-Job”.
Step 11: “Prod-Job” has been triggered after the “Test-Job. “A build” has been successfully created. Click on “#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.
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.
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.
Step 2: Choose Name as “Assignment-3-Pipeline”. While choosing the type as “Build Pipeline View”.
Click on “Create”.
Step 3: Choose “Description” as “Assignment-3-Pipeline Testing”.
Step 4: Choose “Build Pipeline View Title” as “Assignment-3-Pipeline”.
Step 5: Select “Initial Job” as “Test-Job”.
Step 6: Click on “Apply>OK”.
Step 7: Pipeline has been successfully created & “Test-Job” is successfully triggering the “Prod-Job”.
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”.
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.
Step 10: Click on the “+” sign & Again, click on “Create new file”.
Step 11: Put the file name as “develop.txt” & description as “This is a develop1 file”. Click on “Commit changes”.
Step 12: Again, click on “Commit changes”.
Step 13: The “develop1.txt” has been successfully created.
Step 14: Again, click on the “Run button” in “Test” in “Build Pipeline”.
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.
Step 16: Go to “Test-Job”, and click on “#3”.
A separate page will be opened & click on “Console Output”.
Step 17: The “develop1.txt” has been successfully created.
Step 18: Go to the “Test” instance. Paste this command: cd /home/ubuntu/jenkins/workspace/Test-Job.
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.
Step 20: When we go to prod & check the files. The same “dev1.txt” has been shown.
More Jenkins Resources:
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
How to Install Jenkins on Ubuntu 24.04 in AWS EC2 Instance?