How to integrate your Selenium project with Jenkins and Git
[Selenium — Maven — Git — Jenkins]
This is a guide to integrate Selenium with Jenkins, Maven, and TestNG with GitHub. Even though there are some other tool, here I am using Selenium WebDriver, Java, TestNG, Maven, and I integrate these with Jenkins.
As Maven manage all project dependencies and ensure easy building process I have convert my java project to Maven. Before creating a maven project/converting existing java project to maven, you need to install Maven and TestNG in Eclipse IDE [Help > Install New Software]. In the maven project, POM file is to add the required dependencies. Add all your dependencies related to Selenium WebDriver, TestNG in to your Maven Project.
Let’s see how to convert your java project to Maven,
Java Project to Maven in Eclipse !!!
Right click your project and go to Configure and click Convert Maven Project.
Fill the Group Id, Artifact Id and click the Finish button
Configure pom.xml file, that was automatically created, when you create your Maven project or when you convert your java project to Maven. POM file is used to add all your dependencies, double click on pom.xml file and click on pom.xml tab.
You can get all the dependencies from Maven repository.
https://mvnrepository.com/
All my dependencies are listed below.
Then right click the project and run as a Maven build.
Execute Maven Project Using Jenkins:
You can go through below article in medium to setup jenkins.
Let’s see how to execute maven project using jenkins.
Click on New Item link to create a job on Jenkins
Enter an item name click on FreeStyle Project and click on OK
Scroll down to ‘Build‘ option. Click on ‘Add Build Step‘ and choose the value ‘Invoke top-level Maven targets‘ from the drop down list.
Enter Goals “clean install”
Enter POM path (in my case the path is D:\CH\CBlog\workspace\MavenProject\pom.xml)
Click on ‘Apply‘ and ‘Save‘
We have created a new project ‘MavenProject‘ with the configuration to execute Maven Project using Jenkins.
Let’s execute it now. Click on ‘Build Now‘ button.
Right click on Build Number (here in my case it is #1) and click on Console Output to see the result. You could see Build Status ‘Success’ on Console Output.
This way, we could execute Maven Project using Jenkins.
Configure GIT Plugin in Jenkins:
Go To Manage Plugins –> Filter list of plugins available with ‘Git Plugin’. Check the Git Plugin and click on the button ‘Install without restart’. After the installation is done, restart Jenkins by using the command in the browser.
http://localhost:8080/jenkins/restart
Once Jenkins is restarted, Git option should be available under Source Code Management when configuring a job in Jenkins.
Let’s see how to add our project to Git,
You can download and install the latest stable release “Git Binaries” from the URL- https://git-scm.com/. Once git is installed successfully, you can access the git.
Open Gitbash and type “git” and hit “Enter”. If you see below screen that means it is installed successfully
Now go to your project folder and open git bash there.
From this article I just go with the commands of Git directly. Let’s discuss more on Git from my next article.
So to initialize the repository, type below code in git bash.
git init
Then type,
git status
This will show you all the files which are not added to git. So you can add your file using below code.
git add .
And now if you type the command “git status” and hit enter, you can see that all files are added and then type below code to commit your changes.
git commit -m "initial commit"
Now you have committed your files but now you need to push these files to a remote repository. So for that you need to go to your github account and create a public repository and copy the location of the repository. Then in git bash type the below command.
git remote add origin <location of your git repository>git push -u origin master
The let’s see how to configure your jenkins job
Go to [Configure > General > Source code management] and select Git option and provide the repository URL. Then go to [Configure > General > Poll SCM] and provide your cron job pattern.
You can give any expression based on your requirement. As an example,you can give ***** as the cron job pattern. This job will check the jenkins repository every minute and if there is any change, jenkins will trigger this job.
Then Apply the changes and Save.