CI/CD Pipeline
Consider you need to deliver the software to the client. Firstly you need to develop the code, build the code, test the code and deploy it into the production server.
Once you create the software then sometime, the working of the software needs to be changed or we can change its GUI or else new features should be added or been modified. Once you modify your software then you need to again build the code, test and deploy it into a server. Consider there are multiple developers who will develop the code then we need to merge their code and again we need to follow the same process like build, test and deploy.
Pipeline concept:- There will be number of stages. In each stage, the following steps will be mentioned. You can consider a stage like BUILD, TEST and DEPLOY. All the stages are connected in one line called Pipeline. The stages should be created using a tool like Jenkins, GitHub Actions, Azure Pipeline. These tools will come under CI/CD Pipeline. Stages will be run automatically when the developer develops the code. Once they create the code then they do commit under branch, push it into GitHub. After that these stages will be run automatically.
Notes:- Stages like BUILD, TEST and DEPLOY will be run continuously. We will be performing these processes on applications using Jenkins.
In this session we will be learning the theoretical part like what things we were doing earlier, then what problem will get occurred, how to resolve that problem. We will learn how to install Jenkins, how to set up Mavens and JDK. Then how to create a pipeline, what things do we need to write in each stage. As I've said the first developer develops the code, pushes it into a GitHub repo. Then we used to build (creating an exe file or bytecode), test and deploy the code. Then we will give the software to the client. If the client wants to change something in software then we need to follow the same processes manually which is time consuming, more number of errors will occur. There are multiple environments where the dev test phase will start, QA will test your software, can we give the software to the user (this thing will be decided in the UAT test) and then pre production test occurs where we test the performance of an application and check from our side. Then we deliver the project to the user in the production environment.
DEV - Developer + OPS - Build and Development process
Basically Jenkins is used to automate the Build and Development process.
CI - Continuous Integration which means that when a developer commits the code, then automatically the build, test and deploy process needs to be started in a continuous manner. If the code contains any error, it will inform the developer. In simple words, it is an automation to build and test applications whenever new commits are pushed into the branch.
CD - Continuous Delivery/Deployment which means that it will release the code (moving an application in production environment), then build the code which are integrated, then deploy the code.
Continuous Delivery contains Continuous Integration + Deploy application to production by clicking on a button
Continuous Deployment is a continuous delivery without human intervention.
We will create a Java app and deploy it on Tomcat manually. Another way is that we can deploy the app using CI/CD Pipeline by Jenkins.
Download the Tomcat on your machine. Install it.
Open the eclipse. Connect Tomcat to your eclipse by clicking on Windows - Views - load your Tomcat by browsing your Tomcat folder.
Type localhost:port-no in URL
Create a new Maven project. While creating a project you can mention Group as com.app and mention your project name (Artifact) as a JavaApp. Right click your project - Java Build Path - Select Libraries (Tomcat) by browsing it. Right click on the project - click on Run as Server.
Create a new JSP project under subfolder (webapp) - about.jsp. Make a link for this page in your home page.
Then right click on Project and click on Mavean Clean, then click on Maven Build. You will get the war file at the target folder.
We will deploy on Tomcat manually.
Right click on the Target folder on the left side, open in explorer. Copy the war file and paste it into webapp folder. Open Tomcat folder-bin, open terminal here. Then run "./startup.bat".
Type localhost:8080, you will see a Tomcat GUI.
Go to Tomcat folder - conf, set the username and password to access the manager GUI. Click on Manager App, sign in with username and password. Deploy your war file and run your project.
Using Jenkins, it will automate the deployment process.
Download Jenkins. Change your port no of Tomcat by going to Tomcat folder - server.xml
Then open the terminal (where you have downloaded Jenkins). Type command "java -jar ./filename.war"
Run the Tomcat, run the Jenkins. Type localhost:8080 for Jenkins. Unlock with password (you can see the password on path or on terminal). Install the plugins for Jenkins. Login your Jenkins, install the available plugins from Manage Jenkins option. Install the plugins on your local machine.
Login on Jenkins. Go to Tools. Add JDK path from C:/Program Files/Java/jdk
Give the path for Maven from C:/Maven
Give the path for Node js from C:/ProgramFiles/nodejs.
Now we will create a job, run the script, build and check on console.
Now go to Jenkins home page. Add a item with name "project-1-test". Select Freestyle project and click on OK.
You will get Configure page. Go to General.
Add description as "this is the basic project for learning Jenkins".
You can manage the source code either from Git or None. By default it clicks on None.
Build Trigger means when Jenkins needs to run this process
Trigger builds remotely: From another server, you can build the server remotely.
Build after other projects are built
Build periodically: you can build from time to time.
Poll SCM: You can check the commit here. If you want to run every minute then mention ' * '
Now if you commit, then it run automatically
Build Steps: click on Add Build steps. You will set all those options for whichever plugin you have installed. I have selected "Execute windows batch command".
Type the command (script)
echo "hello from Jenkins project".
Check the java (java --version), node js version (node -b)
Click on Save.
Go to your dashboard, click on Project, click on Build Now
Also if you put your program on GitHub, you can pull on Jenkins, compile it and check its output.
Create a folder called "java program Jenkins" on your machine. Create a new file called Main.java
Program:-
import java.util.Date;
public class Main (
public static void main(String[] args)
(
System.out.println("this is java program");
System.out.println("output from java program");
System.out.println(new Date()):
))
Download GitHub Desktop, click on Add local repository l, select the folder "java program Jenkins". Create a new repository. While creating you can mention "jenkin trash course" in description and click on Create. Change the code and write a commit as "initial commit" on bottom left hand side. Then push the repo (click on Publish Repository). Go to your GitHub repository and refresh it.
Now we will run through Jenkins. Create a new item called "project-java-program". Write description as "this project to run java program from github". Select Git as a Source Code Management. Enter your repository URL. Enter branch as "main". Enter Poll SCM as five star.
Select "execute window batch command" in Build Steps. Write script as
javac Main.java
java Main
Then build the script.
Now change the code. Create a commit on GitHub Desktop as "added one more file". Click on Push origin. Reload your GitHub repo as well as Jenkins.
Create a file name called Student.java
public class Student
{
public Student()
{
System.out.println("Creating student object");
}
public void sayHello()
{
System.out.println(“Hi I am creating student”);
}
Use Student object in Main.java
public class Main()
{
Student student = new Student();
student.sayHello();
Commit “added student class” in VS code
Click on Push Origin
}
Check the commit in github repo and then refresh your jenkins.